|
|
| version 1.18, 2001/04/03 17:01:01 | version 1.22, 2001/04/09 13:21:08 |
|---|---|
| Line 12 | Line 12 |
| #include "_file.h" | #include "_file.h" |
| #include "pa_vfile.h" | #include "pa_vfile.h" |
| #include "pa_table.h" | #include "pa_table.h" |
| #include "pa_vint.h" | |
| // consts | // consts |
| Line 29 static void _save(Request& r, const Stri | Line 30 static void _save(Request& r, const Stri |
| // forcing | // forcing |
| // ^save[this body type] | // ^save[this body type] |
| r.fail_if_junction_(true, vfile_name, | r.fail_if_junction_(true, vfile_name, |
| method_name, "file name must not be junction"); | method_name, "file name must not be code"); |
| // save | // save |
| static_cast<VFile *>(r.self)->save(r.absolute(vfile_name.as_string())); | static_cast<VFile *>(r.self)->save(r.absolute(vfile_name.as_string())); |
| Line 41 static void _delete(Request& r, const St | Line 42 static void _delete(Request& r, const St |
| // forcing | // forcing |
| // ^delete[this body type] | // ^delete[this body type] |
| r.fail_if_junction_(true, vfile_name, | r.fail_if_junction_(true, vfile_name, |
| method_name, "file name must not be junction"); | method_name, "file name must not be code"); |
| // unlink | // unlink |
| file_delete(pool, r.absolute(vfile_name.as_string())); | file_delete(pool, r.absolute(vfile_name.as_string())); |
| Line 53 static void _find(Request& r, const Stri | Line 54 static void _find(Request& r, const Stri |
| // forcing | // forcing |
| // ^delete[this body type] | // ^delete[this body type] |
| r.fail_if_junction_(true, vfile_name, | r.fail_if_junction_(true, vfile_name, |
| method_name, "file name must not be junction"); | method_name, "file name must not be code"); |
| const String &lfile_name=vfile_name.as_string(); | const String &lfile_name=vfile_name.as_string(); |
| Line 80 static void _find(Request& r, const Stri | Line 81 static void _find(Request& r, const Stri |
| // forcing ..{this body type} | // forcing ..{this body type} |
| Value& not_found_code=*static_cast<Value *>(params->get(1)); | Value& not_found_code=*static_cast<Value *>(params->get(1)); |
| r.fail_if_junction_(false, not_found_code, | r.fail_if_junction_(false, not_found_code, |
| method_name, "not-found param must be junction"); | method_name, "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 91 static void _load(Request& r, const Stri | Line 92 static void _load(Request& r, const Stri |
| // forcing ^load[this body type] | // forcing ^load[this body type] |
| r.fail_if_junction_(true, vfile_name, | r.fail_if_junction_(true, vfile_name, |
| method_name, "file name must not be junction"); | method_name, "file name must not be code"); |
| const String& lfile_name=vfile_name.as_string(); | const String& lfile_name=vfile_name.as_string(); |
| Line 101 static void _load(Request& r, const Stri | Line 102 static void _load(Request& r, const Stri |
| char *user_file_name=params->size()==1?lfile_name.cstr(String::UL_FILE_NAME) | char *user_file_name=params->size()==1?lfile_name.cstr(String::UL_FILE_NAME) |
| :static_cast<Value *>(params->get(1))->as_string().cstr(); | :static_cast<Value *>(params->get(1))->as_string().cstr(); |
| const String *mime_type=0; | static_cast<VFile *>(r.self)->set(true/*tainted*/, data, size, |
| if(params->size()==3) | user_file_name, &r.mime_type_of(user_file_name)); |
| mime_type=&static_cast<Value *>(params->get(2))->as_string(); | } |
| else { | |
| if(r.mime_types) { | static void append_env_pair(const Hash::Key& key, Hash::Val *value, void *info) { |
| if(char *cext=strrchr(user_file_name, '.')) { | Hash& hash=*static_cast<Hash *>(info); |
| cext++; | hash.put(key, &static_cast<Value *>(value)->as_string()); |
| String sext(pool, cext); | } |
| if(r.mime_types->locate(0, sext)) | /// ^exec[file-name] |
| if(!(mime_type=r.mime_types->item(1))) | /// ^exec[file-name;env hash] |
| PTHROW(0, 0, | /// ^exec[file-name;env hash;cmd;line;arg;s] |
| r.mime_types->origin_string(), | /// @test header to $fields. waits for header '\' tricks |
| "MIME-TYPE table column elements must not be empty"); | static void _cgi(Request& r, const String& method_name, Array *params) { |
| } | Pool& pool=r.pool(); |
| } | |
| Value& vfile_name=*static_cast<Value *>(params->get(0)); | |
| // forcing [this param type] | |
| r.fail_if_junction_(true, vfile_name, | |
| method_name, "file name must not be code"); | |
| Hash *env=0; | |
| if(params->size()>1) { | |
| Value& venv=*static_cast<Value *>(params->get(1)); | |
| // forcing [this param type] | |
| r.fail_if_junction_(true, venv, | |
| method_name, "env must not be code"); | |
| if(Hash *user_env=venv.get_hash()) { | |
| env=new(pool) Hash(pool); | |
| user_env->for_each(append_env_pair, env); | |
| } else | |
| PTHROW(0, 0, | |
| &method_name, | |
| "env must be hash"); | |
| } | } |
| if(!mime_type) | Array *argv=0; |
| mime_type=new(pool) String(pool, "application/octet-stream"); | if(params->size()>2) { |
| argv=new(pool) Array(pool, params->size()-2); | |
| for(int i=2; i<params->size(); i++) | |
| *argv+=&static_cast<Value *>(params->get(i))->as_string(); | |
| } | |
| static_cast<VFile *>(r.self)->set(data, size, user_file_name, mime_type); | const String in(pool, r.post_data, r.post_size); |
| String out(pool); | |
| String err(pool); | |
| int exit_code=SAPI::execute(r.absolute(vfile_name.as_string()), env, argv, | |
| in, out, err); | |
| VFile& self=*static_cast<VFile *>(r.self); | |
| // construct with 'out' body and header | |
| int delim_size; | |
| int pos=out.pos("\n\n", delim_size=2); | |
| if(pos<0) | |
| pos=out.pos("\r\n\r\n", delim_size=4); | |
| if(pos<0) { | |
| delim_size=0; // calm down, compiler | |
| PTHROW(0, 0, | |
| &method_name, | |
| "output does not contain CGI header"); | |
| } | |
| const String& header=out.mid(0, pos); | |
| const String& body=out.mid(pos+delim_size, out.size()); | |
| // body | |
| self.set(false/*not tainted*/, body.cstr(String::UL_AS_IS), body.size()); | |
| // todo header to $fields. waits for header '\' tricks | |
| // $exit-code | |
| self.fields().put( | |
| *new(pool) String(pool, "exit-code"), | |
| new(pool) VInt(pool, exit_code)); | |
| // $stderr | |
| if(err.size()) { | |
| self.fields().put( | |
| *new(pool) String(pool, "stderr"), | |
| new(pool) VString(err)); | |
| SAPI::log(pool, "cgi: %s", err.cstr()); | |
| } | |
| } | } |
| // initialize | // initialize |
| Line 139 void initialize_file_class(Pool& pool, V | Line 201 void initialize_file_class(Pool& pool, V |
| // ^load[disk-name] | // ^load[disk-name] |
| // ^load[disk-name;user-name] | // ^load[disk-name;user-name] |
| // ^load[disk-name;user-name;mime-type] | vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2); |
| vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3); | |
| // ^exec[file-name] | |
| // ^exec[file-name;env hash] | |
| // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s] | |
| vclass.add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+5); | |
| } | } |