Annotation of parser3/src/classes/file.C, revision 1.22
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.22 ! paf 8: $Id: file.C,v 1.21 2001/04/09 11:30:35 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.22 ! paf 109: static void append_env_pair(const Hash::Key& key, Hash::Val *value, void *info) {
! 110: Hash& hash=*static_cast<Hash *>(info);
! 111: hash.put(key, &static_cast<Value *>(value)->as_string());
! 112: }
1.21 paf 113: /// ^exec[file-name]
114: /// ^exec[file-name;env hash]
115: /// ^exec[file-name;env hash;cmd;line;arg;s]
116: /// @test header to $fields. waits for header '\' tricks
117: static void _cgi(Request& r, const String& method_name, Array *params) {
118: Pool& pool=r.pool();
119:
120: Value& vfile_name=*static_cast<Value *>(params->get(0));
121: // forcing [this param type]
122: r.fail_if_junction_(true, vfile_name,
123: method_name, "file name must not be code");
124:
125: Hash *env=0;
126: if(params->size()>1) {
127: Value& venv=*static_cast<Value *>(params->get(1));
128: // forcing [this param type]
129: r.fail_if_junction_(true, venv,
130: method_name, "env must not be code");
1.22 ! paf 131: if(Hash *user_env=venv.get_hash()) {
! 132: env=new(pool) Hash(pool);
! 133: user_env->for_each(append_env_pair, env);
! 134: } else
1.21 paf 135: PTHROW(0, 0,
136: &method_name,
137: "env must be hash");
138: }
139:
140: Array *argv=0;
141: if(params->size()>2) {
142: argv=new(pool) Array(pool, params->size()-2);
143: for(int i=2; i<params->size(); i++)
144: *argv+=&static_cast<Value *>(params->get(i))->as_string();
145: }
146:
147: const String in(pool, r.post_data, r.post_size);
148: String out(pool);
149: String err(pool);
150: int exit_code=SAPI::execute(r.absolute(vfile_name.as_string()), env, argv,
151: in, out, err);
152:
153: VFile& self=*static_cast<VFile *>(r.self);
154: // construct with 'out' body and header
155: int delim_size;
156: int pos=out.pos("\n\n", delim_size=2);
157: if(pos<0)
158: pos=out.pos("\r\n\r\n", delim_size=4);
159: if(pos<0) {
1.22 ! paf 160: delim_size=0; // calm down, compiler
1.21 paf 161: PTHROW(0, 0,
162: &method_name,
163: "output does not contain CGI header");
164: }
165:
166: const String& header=out.mid(0, pos);
167: const String& body=out.mid(pos+delim_size, out.size());
168:
169: // body
170: self.set(false/*not tainted*/, body.cstr(String::UL_AS_IS), body.size());
171:
172: // todo header to $fields. waits for header '\' tricks
173:
174: // $exit-code
175: self.fields().put(
176: *new(pool) String(pool, "exit-code"),
177: new(pool) VInt(pool, exit_code));
178:
179: // $stderr
180: if(err.size()) {
181: self.fields().put(
182: *new(pool) String(pool, "stderr"),
183: new(pool) VString(err));
184:
185: SAPI::log(pool, "cgi: %s", err.cstr());
186: }
187: }
188:
1.1 paf 189: // initialize
190:
1.3 paf 191: void initialize_file_class(Pool& pool, VStateless_class& vclass) {
1.1 paf 192: // ^save[file-name]
1.14 paf 193: vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.7 paf 194:
195: // ^delete[file-name]
1.14 paf 196: vclass.add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.8 paf 197:
198: // ^find[file-name]
199: // ^find[file-name]{when-not-found}
1.14 paf 200: vclass.add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.9 paf 201:
202: // ^load[disk-name]
203: // ^load[disk-name;user-name]
1.20 paf 204: vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.21 paf 205:
206: // ^exec[file-name]
207: // ^exec[file-name;env hash]
208: // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
209: vclass.add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+5);
1.1 paf 210: }
E-mail: