Annotation of parser3/src/classes/file.C, revision 1.21
1.17 paf 1: /** @file
2: Parser: @b file parser class.
3:
1.1 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.17 paf 5:
1.1 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.21 ! paf 8: $Id: file.C,v 1.20 2001/04/08 13:11:15 paf Exp $
1.1 paf 9: */
10:
11: #include "pa_request.h"
12: #include "_file.h"
13: #include "pa_vfile.h"
1.11 paf 14: #include "pa_table.h"
1.21 ! paf 15: #include "pa_vint.h"
1.1 paf 16:
1.9 paf 17: // consts
18:
19: const int FIND_MONKEY_MAX_HOPS=10;
20:
1.1 paf 21: // global var
22:
1.3 paf 23: VStateless_class *file_class;
1.1 paf 24:
25: // methods
26:
27: static void _save(Request& r, const String& method_name, Array *params) {
1.4 paf 28: Pool& pool=r.pool();
1.8 paf 29: Value& vfile_name=*static_cast<Value *>(params->get(0));
1.1 paf 30: // forcing
1.4 paf 31: // ^save[this body type]
1.8 paf 32: r.fail_if_junction_(true, vfile_name,
1.19 paf 33: method_name, "file name must not be code");
1.4 paf 34:
1.7 paf 35: // save
1.18 paf 36: static_cast<VFile *>(r.self)->save(r.absolute(vfile_name.as_string()));
1.7 paf 37: }
38:
39: static void _delete(Request& r, const String& method_name, Array *params) {
40: Pool& pool=r.pool();
1.8 paf 41: Value& vfile_name=*static_cast<Value *>(params->get(0));
1.7 paf 42: // forcing
43: // ^delete[this body type]
1.8 paf 44: r.fail_if_junction_(true, vfile_name,
1.19 paf 45: method_name, "file name must not be code");
1.7 paf 46:
47: // unlink
1.18 paf 48: file_delete(pool, r.absolute(vfile_name.as_string()));
1.1 paf 49: }
50:
1.8 paf 51: static void _find(Request& r, const String& method_name, Array *params) {
52: Pool& pool=r.pool();
53: Value& vfile_name=*static_cast<Value *>(params->get(0));
54: // forcing
55: // ^delete[this body type]
56: r.fail_if_junction_(true, vfile_name,
1.19 paf 57: method_name, "file name must not be code");
1.8 paf 58:
1.18 paf 59: const String &lfile_name=vfile_name.as_string();
1.8 paf 60:
61: // passed file name simply exists in current dir
62: if(file_readable(r.absolute(lfile_name))) {
63: r.write_no_lang(*new(pool) VString(lfile_name));
64: return;
65: }
66:
67: // scan .. dirs for result
1.9 paf 68: for(int i=0; i<FIND_MONKEY_MAX_HOPS; i++) {
1.8 paf 69: String test_name(pool);
70: for(int j=0; j<i; j++)
71: test_name.APPEND_CONST("../");
1.15 paf 72: test_name.append(lfile_name, String::UL_CLEAN);
1.8 paf 73: if(file_readable(r.absolute(test_name))) {
74: r.write_no_lang(*new(pool) VString(*new(pool) String(test_name)));
75: return;
76: }
77: }
78:
79: // not found
80: if(params->size()==2) {
81: // forcing ..{this body type}
82: Value& not_found_code=*static_cast<Value *>(params->get(1));
83: r.fail_if_junction_(false, not_found_code,
1.19 paf 84: method_name, "not-found param must be code");
1.8 paf 85: r.write_pass_lang(r.process(not_found_code));
86: }
87: }
88:
1.9 paf 89: static void _load(Request& r, const String& method_name, Array *params) {
90: Pool& pool=r.pool();
91: Value& vfile_name=*static_cast<Value *>(params->get(0));
92:
1.10 paf 93: // forcing ^load[this body type]
1.9 paf 94: r.fail_if_junction_(true, vfile_name,
1.19 paf 95: method_name, "file name must not be code");
1.9 paf 96:
1.18 paf 97: const String& lfile_name=vfile_name.as_string();
1.9 paf 98:
1.12 paf 99: void *data; size_t size;
1.9 paf 100: file_read(pool, r.absolute(lfile_name), data, size, false/*binary*/);
101:
1.18 paf 102: char *user_file_name=params->size()==1?lfile_name.cstr(String::UL_FILE_NAME)
1.9 paf 103: :static_cast<Value *>(params->get(1))->as_string().cstr();
1.10 paf 104:
1.21 ! paf 105: static_cast<VFile *>(r.self)->set(true/*tainted*/, data, size,
1.20 paf 106: user_file_name, &r.mime_type_of(user_file_name));
1.9 paf 107: }
108:
1.21 ! paf 109: /// ^exec[file-name]
! 110: /// ^exec[file-name;env hash]
! 111: /// ^exec[file-name;env hash;cmd;line;arg;s]
! 112: /// @test header to $fields. waits for header '\' tricks
! 113: static void _cgi(Request& r, const String& method_name, Array *params) {
! 114: Pool& pool=r.pool();
! 115:
! 116: Value& vfile_name=*static_cast<Value *>(params->get(0));
! 117: // forcing [this param type]
! 118: r.fail_if_junction_(true, vfile_name,
! 119: method_name, "file name must not be code");
! 120:
! 121: Hash *env=0;
! 122: if(params->size()>1) {
! 123: Value& venv=*static_cast<Value *>(params->get(1));
! 124: // forcing [this param type]
! 125: r.fail_if_junction_(true, venv,
! 126: method_name, "env must not be code");
! 127: env=venv.get_hash();
! 128: if(!env)
! 129: PTHROW(0, 0,
! 130: &method_name,
! 131: "env must be hash");
! 132: }
! 133:
! 134: Array *argv=0;
! 135: if(params->size()>2) {
! 136: argv=new(pool) Array(pool, params->size()-2);
! 137: for(int i=2; i<params->size(); i++)
! 138: *argv+=&static_cast<Value *>(params->get(i))->as_string();
! 139: }
! 140:
! 141: const String in(pool, r.post_data, r.post_size);
! 142: String out(pool);
! 143: String err(pool);
! 144: int exit_code=SAPI::execute(r.absolute(vfile_name.as_string()), env, argv,
! 145: in, out, err);
! 146:
! 147: VFile& self=*static_cast<VFile *>(r.self);
! 148: // construct with 'out' body and header
! 149: int delim_size;
! 150: int pos=out.pos("\n\n", delim_size=2);
! 151: if(pos<0)
! 152: pos=out.pos("\r\n\r\n", delim_size=4);
! 153: if(pos<0) {
! 154: delim_size=0; // calm, compiler
! 155: PTHROW(0, 0,
! 156: &method_name,
! 157: "output does not contain CGI header");
! 158: }
! 159:
! 160: const String& header=out.mid(0, pos);
! 161: const String& body=out.mid(pos+delim_size, out.size());
! 162:
! 163: // body
! 164: self.set(false/*not tainted*/, body.cstr(String::UL_AS_IS), body.size());
! 165:
! 166: // todo header to $fields. waits for header '\' tricks
! 167:
! 168: // $exit-code
! 169: self.fields().put(
! 170: *new(pool) String(pool, "exit-code"),
! 171: new(pool) VInt(pool, exit_code));
! 172:
! 173: // $stderr
! 174: if(err.size()) {
! 175: self.fields().put(
! 176: *new(pool) String(pool, "stderr"),
! 177: new(pool) VString(err));
! 178:
! 179: SAPI::log(pool, "cgi: %s", err.cstr());
! 180: }
! 181: }
! 182:
1.1 paf 183: // initialize
184:
1.3 paf 185: void initialize_file_class(Pool& pool, VStateless_class& vclass) {
1.1 paf 186: // ^save[file-name]
1.14 paf 187: vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.7 paf 188:
189: // ^delete[file-name]
1.14 paf 190: vclass.add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.8 paf 191:
192: // ^find[file-name]
193: // ^find[file-name]{when-not-found}
1.14 paf 194: vclass.add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.9 paf 195:
196: // ^load[disk-name]
197: // ^load[disk-name;user-name]
1.20 paf 198: vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.21 ! paf 199:
! 200: // ^exec[file-name]
! 201: // ^exec[file-name;env hash]
! 202: // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
! 203: vclass.add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+5);
1.1 paf 204: }
E-mail: