|
|
| version 1.95, 2002/08/08 09:35:22 | version 1.107, 2003/01/21 15:51:06 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b file parser class. | 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 <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 95 static void _save(Request& r, const Stri | Line 95 static void _save(Request& r, const Stri |
| Value& vfile_name=params->as_no_junction(1, "file name must not be code"); | Value& vfile_name=params->as_no_junction(1, "file name must not be code"); |
| // save | // save |
| static_cast<VFile *>(r.self)->save(r.absolute(vfile_name.as_string()), | static_cast<VFile *>(r.get_self())->save(r.absolute(vfile_name.as_string()), |
| vmode_name.as_string()==TEXT_MODE_NAME); | vmode_name.as_string()==TEXT_MODE_NAME); |
| } | } |
| Line 116 static void _move(Request& r, const Stri | Line 116 static void _move(Request& r, const Stri |
| r.absolute(vto_file_name.as_string())); | 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<Hash *>(info); | |
| dest.put(key, value); | |
| } | |
| static void _load(Request& r, const String& method_name, MethodParams *params) { | static void _load(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& vmode_name=params-> as_no_junction(0, "mode must not be code"); | 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()); | 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; | void *data; size_t size; |
| Hash *fields=0; | |
| file_read(pool, lfile_name, data, size, | 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? | char *user_file_name=params->size()>alt_filename_param_index? |
| params->as_string(2, "filename must be string").cstr(String::UL_FILE_SPEC) | params->as_string(alt_filename_param_index, "filename must be string").cstr(String::UL_FILE_SPEC) |
| :lfile_name.cstr(String::UL_FILE_SPEC); | :lfile_name.cstr(String::UL_FILE_SPEC); |
| Value *vcontent_type=0; | |
| if(fields) | |
| vcontent_type=static_cast<Value *>(fields->get(*content_type_name)); | |
| if(!vcontent_type) | |
| vcontent_type=new(pool) VString(r.mime_type_of(user_file_name)); | |
| static_cast<VFile *>(r.self)->set(true/*tainted*/, data, size, | VFile& self=*static_cast<VFile *>(r.get_self()); |
| user_file_name, new(pool) VString(r.mime_type_of(user_file_name))); | 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) { | static void _stat(Request& r, const String& method_name, MethodParams *params) { |
| Line 145 static void _stat(Request& r, const Stri | Line 166 static void _stat(Request& r, const Stri |
| size, | size, |
| atime, mtime, ctime); | atime, mtime, ctime); |
| VFile& vfile=*static_cast<VFile *>(r.self); | VFile& vfile=*static_cast<VFile *>(r.get_self()); |
| vfile.set(true/*tainted*/, 0/*no bytes*/, size); | vfile.set(true/*tainted*/, 0/*no bytes*/, size); |
| Hash& ff=vfile.fields(); | Hash& ff=vfile.fields(); |
| ff.put(*new(pool) String(pool, "adate"), new(pool) VDate(pool, atime)); | ff.put(*new(pool) String(pool, "adate"), new(pool) VDate(pool, atime)); |
| Line 168 static bool is_safe_env_key(const char * | Line 189 static bool is_safe_env_key(const char * |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct Append_env_pair_info { | struct Append_env_pair_info { |
| Hash* hash; | Hash* hash; |
| const String* sstdin; | Value* vstdin; |
| }; | }; |
| #endif | #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<Append_env_pair_info *>(info); | Append_env_pair_info& pi=*static_cast<Append_env_pair_info *>(info); |
| const String& svalue=static_cast<Value *>(value)->as_string(); | Value& value=*static_cast<Value *>(avalue); |
| if(key==STDIN_EXEC_PARAM_NAME) { | if(key==STDIN_EXEC_PARAM_NAME) { |
| pi.sstdin=&svalue; | pi.vstdin=&value; |
| } else { | } else { |
| if(!is_safe_env_key(key.cstr())) | if(!is_safe_env_key(key.cstr())) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| &key, | &key, |
| "not safe environment variable"); | "not safe environment variable"); |
| pi.hash->put(key, &svalue); | pi.hash->put(key, &value.as_string()); |
| } | } |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| Line 248 static void _exec_cgi(Request& r, const | Line 269 static void _exec_cgi(Request& r, const |
| env.put(*new(pool) String(pool, "SCRIPT_NAME"), &script_name); | env.put(*new(pool) String(pool, "SCRIPT_NAME"), &script_name); |
| //env.put(*new(pool) String(pool, "SCRIPT_FILENAME"), ??&script_name); | //env.put(*new(pool) String(pool, "SCRIPT_FILENAME"), ??&script_name); |
| bool stdin_specified=false; | |
| // environment & stdin from param | // environment & stdin from param |
| String in(pool); | String in(pool); |
| if(params->size()>1) { | if(params->size()>1) { |
| Line 255 static void _exec_cgi(Request& r, const | Line 277 static void _exec_cgi(Request& r, const |
| if(Hash *user_env=venv.get_hash(&method_name)) { | if(Hash *user_env=venv.get_hash(&method_name)) { |
| Append_env_pair_info info={&env}; | Append_env_pair_info info={&env}; |
| user_env->for_each(append_env_pair, &info); | user_env->for_each(append_env_pair, &info); |
| if(info.sstdin) | if(info.vstdin) { |
| in.append(*info.sstdin, String::UL_CLEAN, true); | stdin_specified=true; |
| if(const String *sstdin=info.vstdin->get_string()) { | |
| in.append(*sstdin, String::UL_CLEAN, true); | |
| } else | |
| if(VFile *vfile=static_cast<VFile *>(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"); | |
| } | |
| } | } |
| } | } |
| Line 269 static void _exec_cgi(Request& r, const | Line 302 static void _exec_cgi(Request& r, const |
| } | } |
| // passing POST data | // passing POST data |
| if(in.is_empty()) // if $.stdin[...] not specified | if(!stdin_specified) // if $.stdin[...] not specified |
| in.APPEND(r.post_data, r.post_size, String::UL_CLEAN, "POST data (passed)", 0); | in.APPEND(r.post_data, r.post_size, String::UL_CLEAN, "POST data (passed)", 0); |
| // exec! | // exec! |
| Line 277 static void _exec_cgi(Request& r, const | Line 310 static void _exec_cgi(Request& r, const |
| String& err=*new(pool) String(pool); | String& err=*new(pool) String(pool); |
| int status=pa_exec(false/*forced_allow*/, script_name, &env, argv, in, out, err); | int status=pa_exec(false/*forced_allow*/, script_name, &env, argv, in, out, err); |
| VFile& self=*static_cast<VFile *>(r.self); | VFile& self=*static_cast<VFile *>(r.get_self()); |
| const String *body=&out; // ^file:exec | const String *body=&out; // ^file:exec |
| Value *content_type=0; | Value *content_type=0; |
| const char *eol_marker="\r\n"; size_t eol_marker_size=2; | const char *eol_marker=0; size_t eol_marker_size; |
| const String *header=0; | const String *header=0; |
| if(cgi) { // ^file:cgi | if(cgi) { // ^file:cgi |
| // construct with 'out' body and header | // construct with 'out' body and header |
| int delim_size; | int dos_pos=out.pos("\r\n\r\n", 4); |
| int pos=out.pos("\r\n\r\n", delim_size=4); | int unix_pos=out.pos("\n\n", 2); |
| if(pos<0) { | |
| eol_marker="\n"; eol_marker_size=1; | bool unix_header_break; |
| pos=out.pos("\n\n", delim_size=2); | switch((dos_pos >= 0?10:00) + (unix_pos >= 0?01:00)) { |
| } | case 10: // dos |
| if(pos<0) { | unix_header_break=false; |
| delim_size=0; // calm down, compiler | break; |
| case 01: // unix | |
| unix_header_break=true; | |
| break; | |
| case 11: // dos & unix | |
| unix_header_break=unix_pos<dos_pos; | |
| break; | |
| default: // 00 | |
| unix_header_break=false; // calm down, compiler | |
| throw Exception(0, | throw Exception(0, |
| &method_name, | &method_name, |
| "output does not contain CGI header; " | "output does not contain CGI header; " |
| Line 300 static void _exec_cgi(Request& r, const | Line 341 static void _exec_cgi(Request& r, const |
| status, | status, |
| (uint)out.size(), out.cstr(), | (uint)out.size(), out.cstr(), |
| (uint)err.size(), err.cstr()); | (uint)err.size(), err.cstr()); |
| break; //never reached | |
| } | } |
| header=&out.mid(0, pos); | int header_break_pos; |
| body=&out.mid(pos+delim_size, out.size()); | if(unix_header_break) { |
| header_break_pos=unix_pos; | |
| eol_marker="\n"; eol_marker_size=1; | |
| } else { | |
| header_break_pos=dos_pos; | |
| 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()); | |
| } | } |
| // body | // body |
| self.set(false/*not tainted*/, body->cstr(), body->size()); | self.set(false/*not tainted*/, body->cstr(), body->size()); |
| // $fields << header | // $fields << header |
| if(header) { | if(header && eol_marker) { |
| Array rows(pool); | Array rows(pool); |
| header->split(rows, 0, eol_marker, eol_marker_size); | header->split(rows, 0, eol_marker, eol_marker_size); |
| Pass_cgi_header_attribute_info info={&self.fields()}; | Pass_cgi_header_attribute_info info={&self.fields()}; |
| Line 320 static void _exec_cgi(Request& r, const | Line 371 static void _exec_cgi(Request& r, const |
| // $status | // $status |
| self.fields().put( | self.fields().put( |
| *new(pool) String(pool, "status"), | *file_status_name, |
| new(pool) VInt(pool, status)); | new(pool) VInt(pool, status)); |
| // $stderr | // $stderr |
| Line 328 static void _exec_cgi(Request& r, const | Line 379 static void _exec_cgi(Request& r, const |
| self.fields().put( | self.fields().put( |
| *new(pool) String(pool, "stderr"), | *new(pool) String(pool, "stderr"), |
| new(pool) VString(err)); | 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) { | static void _exec(Request& r, const String& method_name, MethodParams *params) { |
| Line 396 static void _list(Request& r, const Stri | Line 445 static void _list(Request& r, const Stri |
| char *file_name_cstr=(char *)pool.malloc(file_name_size); | char *file_name_cstr=(char *)pool.malloc(file_name_size); |
| memcpy(file_name_cstr, ffblk.ff_name, file_name_size); | memcpy(file_name_cstr, ffblk.ff_name, file_name_size); |
| String &file_name=*new(pool) String(pool); | 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); | method_name.origin().file, method_name.origin().line); |
| Array& row=*new(pool) Array(pool); | Array& row=*new(pool) Array(pool); |
| Line 459 static void _find(Request& r, const Stri | Line 508 static void _find(Request& r, const Stri |
| // easy way | // easy way |
| if(file_readable(r.absolute(*file_spec))) { | if(file_readable(r.absolute(*file_spec))) { |
| r.write_no_lang(*file_spec); | r.write_assign_lang(*file_spec); |
| return; | return; |
| } | } |
| Line 474 static void _find(Request& r, const Stri | Line 523 static void _find(Request& r, const Stri |
| local_test_name<<*(dirname=&dirname->mid(0, after_monkey_slash)); | local_test_name<<*(dirname=&dirname->mid(0, after_monkey_slash)); |
| local_test_name<<basename; | local_test_name<<basename; |
| if(file_readable(r.absolute(local_test_name))) { | if(file_readable(r.absolute(local_test_name))) { |
| r.write_no_lang(*new(pool) String(local_test_name)); | r.write_assign_lang(*new(pool) String(local_test_name)); |
| return; | return; |
| } | } |
| } | } |
| Line 520 static void _justext(Request& r, const S | Line 569 static void _justext(Request& r, const S |
| r.write_assign_lang(file_spec.mid(afterdot, file_spec.size())); | r.write_assign_lang(file_spec.mid(afterdot, file_spec.size())); |
| } | } |
| static void _fullpath(Request& r, const String& method_name, MethodParams *params) { | |
| const String& file_spec=params->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) { | |
| 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); | |
| } | |
| // constructor | // constructor |
| Line 570 MFile::MFile(Pool& apool) : Methoded(apo | Line 639 MFile::MFile(Pool& apool) : Methoded(apo |
| add_native_method("justname", Method::CT_STATIC, _justname, 1, 1); | add_native_method("justname", Method::CT_STATIC, _justname, 1, 1); |
| // ^file:justext[/a/some.tar.gz]=gz | // ^file:justext[/a/some.tar.gz]=gz |
| add_native_method("justext", Method::CT_STATIC, _justext, 1, 1); | 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 | // global variable |