--- parser3/src/classes/file.C 2003/01/31 12:34:26 1.107.2.3 +++ parser3/src/classes/file.C 2003/03/21 09:43:48 1.109 @@ -1,11 +1,11 @@ /** @file Parser: @b file parser class. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_FILE_C="$Date: 2003/01/31 12:34:26 $"; +static const char* IDENT_FILE_C="$Date: 2003/03/21 09:43:48 $"; #include "pa_config_includes.h" @@ -21,16 +21,18 @@ static const char* IDENT_FILE_C="$Date: #include "pa_dir.h" #include "pa_vtable.h" #include "pa_charset.h" +#include "pa_charsets.h" // defines #define TEXT_MODE_NAME "text" #define STDIN_EXEC_PARAM_NAME "stdin" +#define CHARSET_EXEC_PARAM_NAME "charset" // consts /// from apache-1.3|src|support|suexec.c -static const char* suexec_safe_env_lst[]={ +static const char *suexec_safe_env_lst[]={ "AUTH_TYPE", "CONTENT_LENGTH", "CONTENT_TYPE", @@ -78,7 +80,7 @@ static const char* suexec_safe_env_lst[] class MFile : public Methoded { public: // VStateless_class - ValuePtr create_new_value() { return ValuePtr(new VFile()); } + Value *create_new_value(Pool& pool) { return new(pool) VFile(pool); } public: // Methoded bool used_directly() { return true; } @@ -139,7 +141,7 @@ static void _load(Request& r, const Stri ); char *user_file_name=params->size()>alt_filename_param_index? - params->as_string(alt_filename_param_index, "filename must be string") + params->as_string(alt_filename_param_index, "filename must be string").cstr(String::UL_FILE_SPEC) :lfile_name.cstr(String::UL_FILE_SPEC); Value *vcontent_type=0; @@ -175,7 +177,7 @@ static void _stat(Request& r, const Stri ff.put(*content_type_name, new(pool) VString(r.mime_type_of(lfile_name.cstr(String::UL_FILE_SPEC)))); } -static bool is_safe_env_key(const char* key) { +static bool is_safe_env_key(const char *key) { if(strncasecmp(key, "HTTP_", 5)==0) return true; if(strncasecmp(key, "CGI_", 4)==0) @@ -190,6 +192,7 @@ static bool is_safe_env_key(const char* struct Append_env_pair_info { Hash* hash; Value* vstdin; + Value* vcharset; }; #endif static void append_env_pair(const Hash::Key& key, Hash::Val *avalue, void *info) { @@ -198,6 +201,8 @@ static void append_env_pair(const Hash:: if(key==STDIN_EXEC_PARAM_NAME) { pi.vstdin=&value; + } else if(key==CHARSET_EXEC_PARAM_NAME) { + pi.vcharset=&value; } else { if(!is_safe_env_key(key.cstr())) throw Exception("parser.runtime", @@ -243,9 +248,9 @@ static void _exec_cgi(Request& r, const env.put(name##key, &name##value); \ } // passing SAPI::environment - if(const char* const *pairs=SAPI::environment(pool)) { - while(const char* pair=*pairs++) - if(const char* eq_at=strchr(pair, '=')) { + if(const char *const *pairs=SAPI::environment(pool)) { + while(const char *pair=*pairs++) + if(const char *eq_at=strchr(pair, '=')) { String& key=*new(pool) String(pool, pair, eq_at-pair); String& value=*new(pool) String(pool, eq_at+1); env.put(key, &value); @@ -269,27 +274,30 @@ static void _exec_cgi(Request& r, const env.put(*new(pool) String(pool, "SCRIPT_NAME"), &script_name); //env.put(*new(pool) String(pool, "SCRIPT_FILENAME"), ??&script_name); - bool stdin_specified=false; // environment & stdin from param - String in(pool); + String raw_in(pool); + Charset *charset=0; // default script works raw_in 'source' charset = no transcoding needed if(params->size()>1) { Value& venv=params->as_no_junction(1, "env must not be code"); if(Hash *user_env=venv.get_hash(&method_name)) { Append_env_pair_info info={&env}; user_env->for_each(append_env_pair, &info); + // $.stdin if(info.vstdin) { - stdin_specified=true; if(const String *sstdin=info.vstdin->get_string()) { - in.append(*sstdin, String::UL_CLEAN, true); + raw_in.append(*sstdin, String::UL_CLEAN, true); } else if(VFile *vfile=static_cast(info.vstdin->as("file", false))) - in.APPEND_TAINTED((const char* )vfile->value_ptr(), vfile->value_size(), + raw_in.APPEND_TAINTED((const char *)vfile->value_ptr(), vfile->value_size(), "$.stdin[assigned]", 0); else throw Exception("parser.runtime", &method_name, STDIN_EXEC_PARAM_NAME " parameter must be string or file"); } + // $.charset + if(info.vcharset) + charset=&charsets->get_charset(info.vcharset->as_string()); } } @@ -301,25 +309,40 @@ static void _exec_cgi(Request& r, const *argv+=¶ms->as_string(i, "parameter must be string"); } - // passing POST data - if(!stdin_specified) // if $.stdin[...] not specified - in.APPEND(r.post_data, r.post_size, String::UL_CLEAN, "POST data (passed)", 0); + // transcode if necessary + String* real_in=&raw_in; + if(charset) { + Charset::transcode(pool, pool.get_source_charset(), *charset, env); + if(argv) + Charset::transcode(pool, pool.get_source_charset(), *charset, *argv); + real_in=&Charset::transcode(pool, pool.get_source_charset(), *charset, raw_in); + } // exec! - String out(pool); - String& err=*new(pool) String(pool); - int status=pa_exec(false/*forced_allow*/, script_name, &env, argv, in, out, err); + String raw_out(pool); + String& raw_err=*new(pool) String(pool); + int status=pa_exec(false/*forced_allow*/, script_name, &env, argv, *real_in, raw_out, raw_err); + + String *real_out=&raw_out; + String *real_err=&raw_err; + + // transcode if necessary + if(charset) { + real_out=&Charset::transcode(pool, *charset, pool.get_source_charset(), raw_out); + real_err=&Charset::transcode(pool, *charset, pool.get_source_charset(), raw_err); + } + VFile& self=*static_cast(r.get_self()); - const String *body=&out; // ^file:exec + const String *body=real_out; // ^file:exec Value *content_type=0; - const char* eol_marker=0; size_t eol_marker_size; + const char *eol_marker=0; size_t eol_marker_size; const String *header=0; if(cgi) { // ^file:cgi // construct with 'out' body and header - int dos_pos=out.pos("\r\n\r\n", 4); - int unix_pos=out.pos("\n\n", 2); + int dos_pos=real_out->pos("\r\n\r\n", 4); + int unix_pos=real_out->pos("\n\n", 2); bool unix_header_break; switch((dos_pos >= 0?10:00) + (unix_pos >= 0?01:00)) { @@ -339,8 +362,8 @@ static void _exec_cgi(Request& r, const "output does not contain CGI header; " "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"", status, - (uint)out.size(), out.cstr(), - (uint)err.size(), err.cstr()); + (uint)real_out->size(), real_out->cstr(), + (uint)real_err->size(), real_err->cstr()); break; //never reached } @@ -353,8 +376,8 @@ static void _exec_cgi(Request& r, const eol_marker="\r\n"; eol_marker_size=2; } - header=&out.mid(0, header_break_pos); - body=&out.mid(header_break_pos+eol_marker_size*2, out.size()); + header=&real_out->mid(0, header_break_pos); + body=&real_out->mid(header_break_pos+eol_marker_size*2, real_out->size()); } // body self.set(false/*not tainted*/, body->cstr(), body->size()); @@ -375,10 +398,10 @@ static void _exec_cgi(Request& r, const new(pool) VInt(pool, status)); // $stderr - if(err.size()) { + if(real_err->size()) { self.fields().put( *new(pool) String(pool, "stderr"), - new(pool) VString(err)); + new(pool) VString(*real_err)); } } static void _exec(Request& r, const String& method_name, MethodParams *params) { @@ -400,8 +423,8 @@ static void _list(Request& r, const Stri if(params->size()>1) { regexp=¶ms->as_no_junction(1, "regexp must not be code").as_string(); - const char* pattern=regexp->cstr(); - const char* errptr; + const char *pattern=regexp->cstr(); + const char *errptr; int erroffset; regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL, &errptr, &erroffset, @@ -482,7 +505,7 @@ static void _lock(Request& r, const Stri file_write_action_under_lock(file_spec, "lock", lock_execute_body, &info); } -static int lastposafter(const String& s, int after, const char* substr, size_t substr_size, bool beforelast=false) { +static int lastposafter(const String& s, int after, const char *substr, size_t substr_size, bool beforelast=false) { size_t size; if(beforelast) size=s.size();