--- parser3/src/classes/file.C 2002/08/01 11:41:12 1.92 +++ parser3/src/classes/file.C 2002/10/21 08:22:17 1.102 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_FILE_C="$Date: 2002/08/01 11:41:12 $"; +static const char* IDENT_FILE_C="$Date: 2002/10/21 08:22:17 $"; #include "pa_config_includes.h" @@ -95,7 +95,7 @@ static void _save(Request& r, const Stri Value& vfile_name=params->as_no_junction(1, "file name must not be code"); // save - static_cast(r.self)->save(r.absolute(vfile_name.as_string()), + static_cast(r.get_self())->save(r.absolute(vfile_name.as_string()), vmode_name.as_string()==TEXT_MODE_NAME); } @@ -119,19 +119,17 @@ static void _move(Request& r, const Stri 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"); - Value& vfile_name=params->as_no_junction(1, "file name must not be code"); - - const String& lfile_name=vfile_name.as_string(); + const String& lfile_name=r.absolute(params->as_no_junction(1, "file name must not be code").as_string()); void *data; size_t size; - file_read(pool, r.absolute(lfile_name), data, size, + file_read(pool, lfile_name, data, size, vmode_name.as_string()==TEXT_MODE_NAME); char *user_file_name=params->size()>2? params->as_string(2, "filename must be string").cstr(String::UL_FILE_SPEC) :lfile_name.cstr(String::UL_FILE_SPEC); - static_cast(r.self)->set(true/*tainted*/, data, size, + static_cast(r.get_self())->set(true/*tainted*/, data, size, user_file_name, new(pool) VString(r.mime_type_of(user_file_name))); } @@ -147,12 +145,13 @@ static void _stat(Request& r, const Stri size, atime, mtime, ctime); - VFile& vfile=*static_cast(r.self); + VFile& vfile=*static_cast(r.get_self()); vfile.set(true/*tainted*/, 0/*no bytes*/, size); Hash& ff=vfile.fields(); ff.put(*new(pool) String(pool, "adate"), new(pool) VDate(pool, atime)); ff.put(*new(pool) String(pool, "mdate"), new(pool) VDate(pool, mtime)); ff.put(*new(pool) String(pool, "cdate"), new(pool) VDate(pool, ctime)); + 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) { @@ -169,30 +168,41 @@ static bool is_safe_env_key(const char * #ifndef DOXYGEN struct Append_env_pair_info { Hash* hash; - const String* sstdin; + Value* vstdin; }; #endif -static void append_env_pair(const Hash::Key& key, Hash::Val *value, void *info) { +static void append_env_pair(const Hash::Key& key, Hash::Val *avalue, void *info) { Append_env_pair_info& pi=*static_cast(info); - const String& svalue=static_cast(value)->as_string(); + Value& value=*static_cast(avalue); if(key==STDIN_EXEC_PARAM_NAME) { - pi.sstdin=&svalue; + pi.vstdin=&value; } else { if(!is_safe_env_key(key.cstr())) throw Exception("parser.runtime", &key, "not safe environment variable"); - pi.hash->put(key, &svalue); + pi.hash->put(key, &value.as_string()); } } -static void pass_cgi_header_attribute(Array::Item *value, void *info) { +#ifndef DOXYGEN +struct Pass_cgi_header_attribute_info { + Hash *hash; + Value *content_type; +}; +#endif +static void pass_cgi_header_attribute(Array::Item *value, void *ainfo) { String& string=*static_cast(value); - Hash& hash=*static_cast(info); + Pool& pool=string.pool(); + Pass_cgi_header_attribute_info& info=*static_cast(ainfo); int colon_pos=string.pos(":", 1); - if(colon_pos>0) - hash.put(string.mid(0, colon_pos), - new(string.pool()) VString(string.mid(colon_pos+1, string.size()))); + if(colon_pos>0) { + const String& key=string.mid(0, colon_pos).change_case(pool, String::CC_UPPER); + Value *value=new(pool) VString(string.mid(colon_pos+1, string.size())); + info.hash->put(key, value); + if(key=="CONTENT-TYPE") + info.content_type=value; + } } /// @todo fix `` in perl - they produced flipping consoles and no output to perl static void _exec_cgi(Request& r, const String& method_name, MethodParams *params, @@ -245,8 +255,17 @@ static void _exec_cgi(Request& r, const if(Hash *user_env=venv.get_hash(&method_name)) { Append_env_pair_info info={&env}; user_env->for_each(append_env_pair, &info); - if(info.sstdin) - in.append(*info.sstdin, String::UL_CLEAN, true); + if(info.vstdin) + if(info.vstdin->is_string()) + in.append(*info.vstdin->get_string(), 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(), + "$.stdin[assigned]", 0); + else + throw Exception("parser.runtime", + &method_name, + STDIN_EXEC_PARAM_NAME " parameter must be string or file"); } } @@ -267,20 +286,30 @@ static void _exec_cgi(Request& r, const String& err=*new(pool) String(pool); int status=pa_exec(false/*forced_allow*/, script_name, &env, argv, in, out, err); - VFile& self=*static_cast(r.self); + VFile& self=*static_cast(r.get_self()); const String *body=&out; // ^file:exec + Value *content_type=0; + 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 delim_size; - const char *eol_marker="\r\n"; size_t eol_marker_size=2; - int pos=out.pos("\r\n\r\n", delim_size=4); - if(pos<0) { - eol_marker="\n"; eol_marker_size=1; - pos=out.pos("\n\n", delim_size=2); - } - if(pos<0) { - delim_size=0; // calm down, compiler + int dos_pos=out.pos("\r\n\r\n", 4); + int unix_pos=out.pos("\n\n", 2); + + bool unix_header_break; + switch((dos_pos >= 0?10:00) + (unix_pos >= 0?01:00)) { + case 10: // dos + unix_header_break=false; + break; + case 01: // unix + unix_header_break=true; + break; + case 11: // dos & unix + unix_header_break=unix_poscstr(), body->size()); + // $fields << header + if(header && eol_marker) { + Array rows(pool); + header->split(rows, 0, eol_marker, eol_marker_size); + Pass_cgi_header_attribute_info info={&self.fields()}; + rows.for_each(pass_cgi_header_attribute, &info); + if(info.content_type) + self.fields().put(*content_type_name, info.content_type); + } + // $status self.fields().put( *new(pool) String(pool, "status"), @@ -313,8 +355,6 @@ static void _exec_cgi(Request& r, const self.fields().put( *new(pool) String(pool, "stderr"), new(pool) VString(err)); - - SAPI::log(pool, "file:%s: %s", cgi?"cgi":"exec", err.cstr()); } } static void _exec(Request& r, const String& method_name, MethodParams *params) { @@ -381,7 +421,7 @@ static void _list(Request& r, const Stri char *file_name_cstr=(char *)pool.malloc(file_name_size); memcpy(file_name_cstr, ffblk.ff_name, file_name_size); String &file_name=*new(pool) String(pool); - file_name.APPEND(file_name_cstr, file_name_size, String::UL_FILE_SPEC, + file_name.APPEND_TAINTED(file_name_cstr, file_name_size, method_name.origin().file, method_name.origin().line); Array& row=*new(pool) Array(pool); @@ -444,7 +484,7 @@ static void _find(Request& r, const Stri // easy way if(file_readable(r.absolute(*file_spec))) { - r.write_no_lang(*file_spec); + r.write_assign_lang(*file_spec); return; } @@ -459,7 +499,7 @@ static void _find(Request& r, const Stri local_test_name<<*(dirname=&dirname->mid(0, after_monkey_slash)); local_test_name<as_string(0, "file name must be string"); + const String *result; + if(file_spec.first_char()=='/') + result=&file_spec; + else { + // /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; + result=&full_disk_path.mid(document_root_length, full_disk_path.size()); + } + r.write_assign_lang(*result); +} + // constructor @@ -555,6 +611,8 @@ MFile::MFile(Pool& apool) : Methoded(apo add_native_method("justname", Method::CT_STATIC, _justname, 1, 1); // ^file:justext[/a/some.tar.gz]=gz add_native_method("justext", Method::CT_STATIC, _justext, 1, 1); + // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif + add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1); } // global variable