|
|
| version 1.107.2.16.2.2, 2003/03/18 16:00:06 | version 1.107.2.16.2.5, 2003/03/21 13:42:26 |
|---|---|
| Line 34 static const char* IDENT_FILE_C="$Date$" | Line 34 static const char* IDENT_FILE_C="$Date$" |
| class MFile: public Methoded { | class MFile: public Methoded { |
| public: // VStateless_class | public: // VStateless_class |
| ValuePtr create_new_value() { return ValuePtr(new VFile()); } | Value* create_new_value() { return Value*(new VFile()); } |
| public: // Methoded | public: // Methoded |
| bool used_directly() { return true; } | bool used_directly() { return true; } |
| Line 103 static const String& cdate_name(new Stri | Line 103 static const String& cdate_name(new Stri |
| // methods | // methods |
| static void _save(Request& r, const String& /*method_name*/, MethodParams* params) { | static void _save(Request& r, const String& /*method_name*/, MethodParams* params) { |
| ValuePtr 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"); |
| ValuePtr 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 |
| GET_SELF(r, VFile).save(r.absolute(vfile_name->as_string(&pool)), | GET_SELF(r, VFile).save(r.absolute(vfile_name->as_string()), |
| *vmode_name->as_string(&pool)==TEXT_MODE_NAME); | *vmode_name->as_string()==TEXT_MODE_NAME); |
| } | } |
| static void _delete(Request& r, const String& /*method_name*/, MethodParams* params) { | static void _delete(Request& r, const String& /*method_name*/, MethodParams* params) { |
| ValuePtr vfile_name=params->as_no_junction(0, "file name must not be code"); | Value* vfile_name=params->as_no_junction(0, "file name must not be code"); |
| // unlink | // unlink |
| file_delete(r.absolute(vfile_name->as_string(&pool))); | file_delete(r.absolute(vfile_name->as_string())); |
| } | } |
| static void _move(Request& r, const String& /*method_name*/, MethodParams* params) { | static void _move(Request& r, const String& /*method_name*/, MethodParams* params) { |
| ValuePtr vfrom_file_name=params->as_no_junction(0, "from file name must not be code"); | Value* vfrom_file_name=params->as_no_junction(0, "from file name must not be code"); |
| ValuePtr vto_file_name=params->as_no_junction(1, "to file name must not be code"); | Value* vto_file_name=params->as_no_junction(1, "to file name must not be code"); |
| // move | // move |
| file_move( | file_move( |
| r.absolute(vfrom_file_name->as_string(&pool)), | r.absolute(vfrom_file_name->as_string()), |
| r.absolute(vto_file_name->as_string(&pool))); | r.absolute(vto_file_name->as_string())); |
| } | } |
| static void _load_pass_param( | static void _load_pass_param( |
| Line 135 static void _load_pass_param( | Line 135 static void _load_pass_param( |
| dest->put(key, value); | 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) { |
| ValuePtr 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(&pool)); | const String& lfile_name=r.absolute(params->as_no_junction(1, "file name must not be code")->as_string()); |
| ValuePtr third_param=params->count()>2?params->as_no_junction(2, "filename or options must not be code") | Value* third_param=params->count()>2?params->as_no_junction(2, "filename or options must not be code") |
| :ValuePtr(0); | :0; |
| HashStringValue* third_param_hash=third_param?third_param->get_hash(method_name):0; | HashStringValue* third_param_hash=third_param?third_param->get_hash(method_name):0; |
| int alt_filename_param_index=2; | int alt_filename_param_index=2; |
| if(third_param_hash) | if(third_param_hash) |
| alt_filename_param_index++; | alt_filename_param_index++; |
| File_read_result file=file_read(pool, r.charsets.source(), lfile_name, | File_read_result file=file_read(r.charsets.source(), lfile_name, |
| *vmode_name->as_string(&pool)==TEXT_MODE_NAME, | *vmode_name->as_string()==TEXT_MODE_NAME, |
| third_param_hash | third_param_hash |
| ); | ); |
| char *user_file_name=params->count()>alt_filename_param_index? | char *user_file_name=params->count()>alt_filename_param_index? |
| params->as_string(alt_filename_param_index, "filename must be string")->cstr() | params->as_string(alt_filename_param_index, "filename must be string")->cstr() |
| :lfile_name->cstr(pool, String::UL_FILE_SPEC); | :lfile_name->cstr(String::L_FILE_SPEC); |
| ValuePtr vcontent_type(0); | Value* vcontent_type(0); |
| if(file.headers) | if(file.headers) |
| vcontent_type=file.headers->get(content_type_name); | vcontent_type=file.headers->get(content_type_name); |
| if(!vcontent_type) | if(!vcontent_type) |
| vcontent_type=ValuePtr(new VString(r.mime_type_of(user_file_name))); | vcontent_type=Value*(new VString(r.mime_type_of(user_file_name))); |
| VFile& self=GET_SELF(r, VFile); | VFile& self=GET_SELF(r, VFile); |
| self.set(pool, true/*tainted*/, file.data, file.size, user_file_name, vcontent_type); | self.set(true/*tainted*/, file.data, file.size, user_file_name, vcontent_type); |
| if(file.headers) | if(file.headers) |
| file.headers->for_each(_load_pass_param, &self.fields()); | file.headers->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) { |
| ValuePtr vfile_name=params->as_no_junction(0, "file name must not be code"); | Value* vfile_name=params->as_no_junction(0, "file name must not be code"); |
| const String& lfile_name=vfile_name->as_string(&pool); | const String& lfile_name=vfile_name->as_string(); |
| size_t size; | size_t size; |
| time_t atime, mtime, ctime; | time_t atime, mtime, ctime; |
| Line 177 static void _stat(Request& r, const Stri | Line 177 static void _stat(Request& r, const Stri |
| atime, mtime, ctime); | atime, mtime, ctime); |
| VFile& self=GET_SELF(r, VFile); | VFile& self=GET_SELF(r, VFile); |
| self.set(pool, true/*tainted*/, 0/*no bytes*/, size); | self.set(true/*tainted*/, 0/*no bytes*/, size); |
| HashStringValue& ff=self.fields(); | HashStringValue& ff=self.fields(); |
| ff.put(adate_name, ValuePtr(new VDate(atime))); | ff.put(adate_name, Value*(new VDate(atime))); |
| ff.put(mdate_name, ValuePtr(new VDate(mtime))); | ff.put(mdate_name, Value*(new VDate(mtime))); |
| ff.put(cdate_name, ValuePtr(new VDate(ctime))); | ff.put(cdate_name, Value*(new VDate(ctime))); |
| ff.put(content_type_name, ValuePtr(new VString(r.mime_type_of(lfile_name->cstr(String::UL_FILE_SPEC))))); | ff.put(content_type_name, Value*(new VString(r.mime_type_of(lfile_name->cstr(String::L_FILE_SPEC))))); |
| } | } |
| static bool is_safe_env_key(const char* key) { | static bool is_safe_env_key(const char* key) { |
| Line 200 static bool is_safe_env_key(const char* | Line 200 static bool is_safe_env_key(const char* |
| struct Append_env_pair_info { | struct Append_env_pair_info { |
| ; | ; |
| HashStringString* env; | HashStringString* env; |
| ValuePtr vstdin; | Value* vstdin; |
| }; | }; |
| #endif | #endif |
| static void append_env_pair( | static void append_env_pair( |
| Line 222 struct Pass_cgi_header_attribute_info { | Line 222 struct Pass_cgi_header_attribute_info { |
| ; | ; |
| Charset* charset; | Charset* charset; |
| HashStringValue* fields; | HashStringValue* fields; |
| ValuePtr content_type; | Value* content_type; |
| }; | }; |
| #endif | #endif |
| static void pass_cgi_header_attribute( | static void pass_cgi_header_attribute( |
| Line 231 static void pass_cgi_header_attribute( | Line 231 static void pass_cgi_header_attribute( |
| int colon_pos=astring->pos(":", 1); | int colon_pos=astring->pos(":", 1); |
| if(colon_pos>0) { | if(colon_pos>0) { |
| const String& key(astring->mid(0, colon_pos)->change_case( | const String& key(astring->mid(0, colon_pos)->change_case( |
| *info->pool, *info->charset, String::CC_UPPER)); | *info->*info->charset, String::CC_UPPER)); |
| ValuePtr value(new VString(astring->mid(colon_pos+1, astring->size()))); | Value* value(new VString(astring->mid(colon_pos+1, astring->length()))); |
| info->fields->put(key, value); | info->fields->put(key, value); |
| if(*key=="CONTENT-TYPE") | if(*key=="CONTENT-TYPE") |
| info->content_type=value; | info->content_type=value; |
| Line 242 static void pass_cgi_header_attribute( | Line 242 static void pass_cgi_header_attribute( |
| static void _exec_cgi(Request& r, const String& method_name, MethodParams* params, | static void _exec_cgi(Request& r, const String& method_name, MethodParams* params, |
| bool cgi) { | bool cgi) { |
| ValuePtr vfile_name=params->as_no_junction(0, "file name must not be code"); | Value* vfile_name=params->as_no_junction(0, "file name must not be code"); |
| const String& script_name=r.absolute(vfile_name->as_string(&pool)); | const String& script_name=r.absolute(vfile_name->as_string()); |
| HashStringString env; | HashStringString env; |
| #define ECSTR(name, value_cstr) \ | #define ECSTR(name, value_cstr) \ |
| Line 276 static void _exec_cgi(Request& r, const | Line 276 static void _exec_cgi(Request& r, const |
| ECSTR(CONTENT_LENGTH, content_length_cstr); | ECSTR(CONTENT_LENGTH, content_length_cstr); |
| // SCRIPT_* | // SCRIPT_* |
| env.put(String* (new String("SCRIPT_NAME")), script_name); | env.put(String* (new String("SCRIPT_NAME")), script_name); |
| //env.put(*new String(pool, "SCRIPT_FILENAME"), ??&script_name); | //env.put(*new String("SCRIPT_FILENAME"), ??&script_name); |
| bool stdin_specified=false; | bool stdin_specified=false; |
| // environment & stdin from param | // environment & stdin from param |
| String in; | String in; |
| if(params->count()>1) { | if(params->count()>1) { |
| ValuePtr venv=params->as_no_junction(1, "env must not be code"); | Value* venv=params->as_no_junction(1, "env must not be code"); |
| if(HashStringValue* user_env=venv->get_hash(method_name)) { | if(HashStringValue* user_env=venv->get_hash(method_name)) { |
| Append_env_pair_info info; | Append_env_pair_info info; |
| info.pool=&pool; | info.pool=&pool; |
| Line 290 static void _exec_cgi(Request& r, const | Line 290 static void _exec_cgi(Request& r, const |
| user_env->for_each(append_env_pair, &info); | user_env->for_each(append_env_pair, &info); |
| if(info.vstdin) { | if(info.vstdin) { |
| stdin_specified=true; | stdin_specified=true; |
| if(const String& sstdin=info.vstdin->get_string(&pool)) { | if(const String& sstdin=info.vstdin->get_string()) { |
| in.append(*sstdin, String::UL_CLEAN, true); | in.append(*sstdin, String::L_CLEAN, true); |
| } else | } else |
| if(VFile *vfile=static_cast<VFile *>(info.vstdin->as("file", false))) | if(VFile *vfile=static_cast<VFile *>(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(), |
| Line 313 static void _exec_cgi(Request& r, const | Line 313 static void _exec_cgi(Request& r, const |
| // passing POST data | // passing POST data |
| if(!stdin_specified) // if $.stdin[...] not specified | if(!stdin_specified) // if $.stdin[...] not specified |
| in.APPEND(r.request_info.post_data, r.request_info.post_size, String::UL_CLEAN, | in.APPEND(r.request_info.post_data, r.request_info.post_size, String::L_CLEAN, |
| "POST data (passed)", 0); | "POST data (passed)", 0); |
| // exec! | // exec! |
| PA_exec_result execution= | PA_exec_result execution= |
| pa_exec(pool, false/*forced_allow*/, script_name, &env, argv, in); | pa_exec(false/*forced_allow*/, script_name, &env, argv, in); |
| VFile& self=GET_SELF(r, VFile); | VFile& self=GET_SELF(r, VFile); |
| const String& body=execution.out; // ^file:exec | const String& body=execution.out; // ^file:exec |
| ValuePtr content_type(0); | 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); | const String& header(0); |
| if(cgi) { // ^file:cgi | if(cgi) { // ^file:cgi |
| Line 349 static void _exec_cgi(Request& r, const | Line 349 static void _exec_cgi(Request& r, const |
| "output does not contain CGI header; " | "output does not contain CGI header; " |
| "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"", | "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"", |
| execution.status, | execution.status, |
| (uint)execution.out->size(), execution.out->cstr().get(), | (uint)execution.out->length(), execution.out->cstr().get(), |
| (uint)execution.err->size(), execution.err->cstr().get()); | (uint)execution.err->length(), execution.err->cstr().get()); |
| break; //never reached | break; //never reached |
| } | } |
| Line 364 static void _exec_cgi(Request& r, const | Line 364 static void _exec_cgi(Request& r, const |
| } | } |
| header=execution.out->mid(0, header_break_pos); | header=execution.out->mid(0, header_break_pos); |
| body=execution.out->mid(header_break_pos+eol_marker_size*2, execution.out->size()); | body=execution.out->mid(header_break_pos+eol_marker_size*2, execution.out->length()); |
| } | } |
| // body | // body |
| self.set(pool, false/*not tainted*/, body->cstr(), body->size()); | self.set(false/*not tainted*/, body->cstr(), body->length()); |
| // $fields << header | // $fields << header |
| if(header && eol_marker) { | if(header && eol_marker) { |
| Line 383 static void _exec_cgi(Request& r, const | Line 383 static void _exec_cgi(Request& r, const |
| } | } |
| // $status | // $status |
| self.fields().put(file_status_name, ValuePtr(new VInt(execution.status))); | self.fields().put(file_status_name, Value*(new VInt(execution.status))); |
| // $stderr | // $stderr |
| if(execution.err->size()) | if(execution.err->length()) |
| self.fields().put( | self.fields().put( |
| String* (new String("stderr")), | String* (new String("stderr")), |
| ValuePtr(new VString(execution.err))); | Value*(new VString(execution.err))); |
| } | } |
| static void _exec(Request& r, const String& method_name, MethodParams* params) { | static void _exec(Request& r, const String& method_name, MethodParams* params) { |
| _exec_cgi(r, method_name, params, false); | _exec_cgi(r, method_name, params, false); |
| Line 400 static void _cgi(Request& r, const Strin | Line 400 static void _cgi(Request& r, const Strin |
| static void _list(Request& r, const String& method_name, MethodParams* params) { | static void _list(Request& r, const String& method_name, MethodParams* params) { |
| ValuePtr relative_path=params->as_no_junction(0, "path must not be code"); | Value* relative_path=params->as_no_junction(0, "path must not be code"); |
| const String& regexp; | const String& regexp; |
| pcre *regexp_code; | pcre *regexp_code; |
| const int ovecsize=(1/*match*/)*3; | const int ovecsize=(1/*match*/)*3; |
| int ovector[ovecsize]; | int ovector[ovecsize]; |
| if(params->count()>1) { | if(params->count()>1) { |
| regexp=params->as_no_junction(1, "regexp must not be code")->as_string(&pool); | regexp=params->as_no_junction(1, "regexp must not be code")->as_string(); |
| const char* pattern=regexp->cstr(); | const char* pattern=regexp->cstr(); |
| const char* errptr; | const char* errptr; |
| Line 418 static void _list(Request& r, const Stri | Line 418 static void _list(Request& r, const Stri |
| if(!regexp_code) | if(!regexp_code) |
| throw Exception(0, | throw Exception(0, |
| regexp->mid(erroffset, regexp->size()), | regexp->mid(erroffset, regexp->length()), |
| "regular expression syntax error - %s", errptr); | "regular expression syntax error - %s", errptr); |
| } else | } else |
| regexp_code=0; | regexp_code=0; |
| const char* absolute_path_cstr=r.absolute(relative_path->as_string(&pool))-> | const char* absolute_path_cstr=r.absolute(relative_path->as_string())-> |
| cstr(String::UL_FILE_SPEC); | cstr(String::L_FILE_SPEC); |
| Table::columns_type columns(new ArrayString); | Table::columns_type columns(new ArrayString); |
| *columns+=String* (new String("name")); | *columns+=String* (new String("name")); |
| Line 466 static void _list(Request& r, const Stri | Line 466 static void _list(Request& r, const Stri |
| pcre_free(regexp_code); | pcre_free(regexp_code); |
| // write out result | // write out result |
| r.write_no_lang(ValuePtr(new VTable(table))); | r.write_no_lang(Value*(new VTable(table))); |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct Lock_execute_body_info { | struct Lock_execute_body_info { |
| Request* r; | Request* r; |
| ValuePtr body_code; | Value* body_code; |
| }; | }; |
| #endif | #endif |
| static void lock_execute_body(int , void *ainfo) { | static void lock_execute_body(int , void *ainfo) { |
| Line 492 static void _lock(Request& r, const Stri | Line 492 static void _lock(Request& r, const Stri |
| 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; | size_t size; |
| if(beforelast) | if(beforelast) |
| size=s.size(); | size=s.length(); |
| int at; | int at; |
| while((at=s.pos(substr, substr_size, after))>=0) { | while((at=s.pos(substr, substr_size, after))>=0) { |
| size_t newafter=at+substr_size/*skip substr*/; | size_t newafter=at+substr_size/*skip substr*/; |
| Line 505 static int lastposafter(const String& s, | Line 505 static int lastposafter(const String& s, |
| } | } |
| static void _find(Request& r, const String& method_name, MethodParams* params) { | static void _find(Request& r, const String& method_name, MethodParams* params) { |
| const String& file_name=params->as_no_junction(0, "file name must not be code")->as_string(&pool); | const String& file_name=params->as_no_junction(0, "file name must not be code")->as_string(); |
| const String& file_spec; | const String& file_spec; |
| if(file_name->first_char()=='/') | if(file_name->first_char()=='/') |
| file_spec=file_name; | file_spec=file_name; |
| Line 521 static void _find(Request& r, const Stri | Line 521 static void _find(Request& r, const Stri |
| // monkey way | // monkey way |
| int after_base_slash=lastposafter(*file_spec, 0, "/", 1); | int after_base_slash=lastposafter(*file_spec, 0, "/", 1); |
| const String& dirname=file_spec->mid(0, after_base_slash); | const String& dirname=file_spec->mid(0, after_base_slash); |
| const String& basename=file_spec->mid(after_base_slash, file_spec->size()); | const String& basename=file_spec->mid(after_base_slash, file_spec->length()); |
| int after_monkey_slash; | int after_monkey_slash; |
| while((after_monkey_slash=lastposafter(*dirname, 0, "/", 1, true))>0) { | while((after_monkey_slash=lastposafter(*dirname, 0, "/", 1, true))>0) { |
| Line 536 static void _find(Request& r, const Stri | Line 536 static void _find(Request& r, const Stri |
| // no way, not found | // no way, not found |
| if(params->count()==2) { | if(params->count()==2) { |
| ValuePtr not_found_code=params->as_junction(1, "not-found param must be code"); | Value* not_found_code=params->as_junction(1, "not-found param must be code"); |
| r.write_pass_lang(r.process(not_found_code)); | r.write_pass_lang(r.process(not_found_code)); |
| } | } |
| } | } |
| Line 556 static void _basename(Request& r, const | Line 556 static void _basename(Request& r, const |
| const String& file_spec=params->as_string(0, "file name must be string"); | const String& file_spec=params->as_string(0, "file name must be string"); |
| // /a/some.tar.gz > some.tar.gz | // /a/some.tar.gz > some.tar.gz |
| int afterslash=lastposafter(*file_spec, 0, "/", 1); | int afterslash=lastposafter(*file_spec, 0, "/", 1); |
| r.write_assign_lang(*file_spec->mid(afterslash, file_spec->size())); | r.write_assign_lang(*file_spec->mid(afterslash, file_spec->length())); |
| } | } |
| static void _justname(Request& r, const String& method_name, MethodParams* params) { | static void _justname(Request& r, const String& method_name, MethodParams* params) { |
| Line 564 static void _justname(Request& r, const | Line 564 static void _justname(Request& r, const |
| // /a/some.tar.gz > some.tar | // /a/some.tar.gz > some.tar |
| int afterslash=lastposafter(*file_spec, 0, "/", 1); | int afterslash=lastposafter(*file_spec, 0, "/", 1); |
| int afterdot=lastposafter(*file_spec, afterslash, ".", 1); | int afterdot=lastposafter(*file_spec, afterslash, ".", 1); |
| r.write_assign_lang(*file_spec->mid(afterslash, afterdot!=afterslash?afterdot-1:file_spec->size())); | r.write_assign_lang(*file_spec->mid(afterslash, afterdot!=afterslash?afterdot-1:file_spec->length())); |
| } | } |
| static void _justext(Request& r, const String& method_name, MethodParams* params) { | static void _justext(Request& r, const String& method_name, MethodParams* params) { |
| const String& file_spec=params->as_string(0, "file name must be string"); | const String& file_spec=params->as_string(0, "file name must be string"); |
| // /a/some.tar.gz > gz | // /a/some.tar.gz > gz |
| int afterdot=lastposafter(*file_spec, 0, ".", 1); | int afterdot=lastposafter(*file_spec, 0, ".", 1); |
| if(afterdot>0) | if(afterdot>0) |
| r.write_assign_lang(*file_spec->mid(afterdot, file_spec->size())); | r.write_assign_lang(*file_spec->mid(afterdot, file_spec->length())); |
| } | } |
| static void _fullpath(Request& r, const String& method_name, MethodParams* params) { | static void _fullpath(Request& r, const String& method_name, MethodParams* params) { |
| Line 589 static void _fullpath(Request& r, const | Line 589 static void _fullpath(Request& r, const |
| if(last_char == '/' || last_char == '\\') | if(last_char == '/' || last_char == '\\') |
| --document_root_length; | --document_root_length; |
| } | } |
| result=full_disk_path->mid(document_root_length, full_disk_path->size()); | result=full_disk_path->mid(document_root_length, full_disk_path->length()); |
| } | } |
| r.write_assign_lang(*result); | r.write_assign_lang(*result); |
| } | } |