--- parser3/src/classes/file.C 2002/11/20 10:01:01 1.103 +++ parser3/src/classes/file.C 2003/01/31 12:34:26 1.107.2.3 @@ -1,11 +1,11 @@ /** @file Parser: @b file parser class. - Copyright (c) 2001, 2002 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: 2002/11/20 10:01:01 $"; +static const char* IDENT_FILE_C="$Date: 2003/01/31 12:34:26 $"; #include "pa_config_includes.h" @@ -30,7 +30,7 @@ static const char* IDENT_FILE_C="$Date: // 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 +78,7 @@ static const char *suexec_safe_env_lst[] class MFile : public Methoded { public: // VStateless_class - Value *create_new_value(Pool& pool) { return new(pool) VFile(pool); } + ValuePtr create_new_value() { return ValuePtr(new VFile()); } public: // Methoded bool used_directly() { return true; } @@ -116,21 +116,42 @@ static void _move(Request& r, const Stri r.absolute(vto_file_name.as_string())); } +static void _load_pass_param(const Hash::Key& key, Hash::Val *value, void *info) { + Hash& dest=*static_cast(info); + dest.put(key, value); +} static void _load(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); Value& vmode_name=params-> as_no_junction(0, "mode must not be code"); const String& lfile_name=r.absolute(params->as_no_junction(1, "file name must not be code").as_string()); + Value *third_param=params->size()>2?¶ms->as_no_junction(2, "filename or options must not be code"):0; + Hash *third_param_hash=third_param?third_param->get_hash(&method_name):0; + int alt_filename_param_index=2; + if(third_param_hash) + alt_filename_param_index++; void *data; size_t size; + Hash *fields=0; file_read(pool, lfile_name, data, size, - vmode_name.as_string()==TEXT_MODE_NAME); + vmode_name.as_string()==TEXT_MODE_NAME, + third_param_hash, + &fields + ); - char *user_file_name=params->size()>2? - params->as_string(2, "filename must be string").cstr(String::UL_FILE_SPEC) + char *user_file_name=params->size()>alt_filename_param_index? + params->as_string(alt_filename_param_index, "filename must be string") :lfile_name.cstr(String::UL_FILE_SPEC); + + Value *vcontent_type=0; + if(fields) + vcontent_type=static_cast(fields->get(*content_type_name)); + if(!vcontent_type) + vcontent_type=new(pool) VString(r.mime_type_of(user_file_name)); - static_cast(r.get_self())->set(true/*tainted*/, data, size, - user_file_name, new(pool) VString(r.mime_type_of(user_file_name))); + VFile& self=*static_cast(r.get_self()); + self.set(true/*tainted*/, data, size, user_file_name, vcontent_type); + if(fields) + fields->for_each(_load_pass_param, &self.fields()); } static void _stat(Request& r, const String& method_name, MethodParams *params) { @@ -154,7 +175,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) @@ -222,9 +243,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); @@ -262,7 +283,7 @@ static void _exec_cgi(Request& r, const 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(), + in.APPEND_TAINTED((const char* )vfile->value_ptr(), vfile->value_size(), "$.stdin[assigned]", 0); else throw Exception("parser.runtime", @@ -293,7 +314,7 @@ static void _exec_cgi(Request& r, const const String *body=&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 @@ -350,7 +371,7 @@ static void _exec_cgi(Request& r, const // $status self.fields().put( - *new(pool) String(pool, "status"), + *file_status_name, new(pool) VInt(pool, status)); // $stderr @@ -379,8 +400,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, @@ -461,7 +482,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(); @@ -557,8 +578,12 @@ static void _fullpath(Request& r, const // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif const String& full_disk_path=r.absolute(file_spec); size_t document_root_length=strlen(r.info.document_root); - if(document_root_length>0) - --document_root_length; + + if(document_root_length>0) { + char last_char=r.info.document_root[document_root_length-1]; + if(last_char == '/' || last_char == '\\') + --document_root_length; + } result=&full_disk_path.mid(document_root_length, full_disk_path.size()); } r.write_assign_lang(*result);