Annotation of parser3/src/classes/file.C, revision 1.36
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.36 ! paf 8: $Id: file.C,v 1.35 2001/04/28 15:22:38 paf Exp $
1.1 paf 9: */
10:
1.35 paf 11: #include "classes.h"
1.1 paf 12: #include "pa_request.h"
13: #include "pa_vfile.h"
1.11 paf 14: #include "pa_table.h"
1.21 paf 15: #include "pa_vint.h"
1.24 paf 16: #include "pa_exec.h"
1.1 paf 17:
1.33 paf 18: // consts
19:
1.32 paf 20: // defines
21:
22: #define FILE_CLASS_NAME "file"
23:
24: // class
25:
26: class MFile : public Methoded {
27: public: // VStateless_class
28:
29: Value *create_new_value(Pool& pool) { return new(pool) VFile(pool); }
30:
1.33 paf 31: public: // Methoded
32: bool used_directly() { return true; }
33:
1.32 paf 34: public:
35: MFile(Pool& pool);
1.33 paf 36:
1.32 paf 37: };
38:
1.9 paf 39: // consts
40:
41: const int FIND_MONKEY_MAX_HOPS=10;
42:
1.1 paf 43: // methods
44:
1.26 paf 45: static void _save(Request& r, const String&, MethodParams *params) {
46: Value& vfile_name=params->get_no_junction(0, "file name must not be code");
1.4 paf 47:
1.7 paf 48: // save
1.18 paf 49: static_cast<VFile *>(r.self)->save(r.absolute(vfile_name.as_string()));
1.7 paf 50: }
51:
1.26 paf 52: static void _delete(Request& r, const String&, MethodParams *params) {
1.7 paf 53: Pool& pool=r.pool();
1.26 paf 54: Value& vfile_name=params->get_no_junction(0, "file name must not be code");
1.7 paf 55:
56: // unlink
1.18 paf 57: file_delete(pool, r.absolute(vfile_name.as_string()));
1.1 paf 58: }
59:
1.26 paf 60: static void _find(Request& r, const String& method_name, MethodParams *params) {
1.8 paf 61: Pool& pool=r.pool();
1.26 paf 62: Value& vfile_name=params->get_no_junction(0, "file name must not be code");
1.8 paf 63:
1.18 paf 64: const String &lfile_name=vfile_name.as_string();
1.8 paf 65:
66: // passed file name simply exists in current dir
67: if(file_readable(r.absolute(lfile_name))) {
68: r.write_no_lang(*new(pool) VString(lfile_name));
69: return;
70: }
71:
72: // scan .. dirs for result
1.9 paf 73: for(int i=0; i<FIND_MONKEY_MAX_HOPS; i++) {
1.8 paf 74: String test_name(pool);
75: for(int j=0; j<i; j++)
76: test_name.APPEND_CONST("../");
1.15 paf 77: test_name.append(lfile_name, String::UL_CLEAN);
1.8 paf 78: if(file_readable(r.absolute(test_name))) {
79: r.write_no_lang(*new(pool) VString(*new(pool) String(test_name)));
80: return;
81: }
82: }
83:
84: // not found
85: if(params->size()==2) {
1.26 paf 86: Value& not_found_code=params->get_junction(1, "not-found param must be code");
1.8 paf 87: r.write_pass_lang(r.process(not_found_code));
88: }
89: }
90:
1.26 paf 91: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.9 paf 92: Pool& pool=r.pool();
1.26 paf 93: Value& vfile_name=params->get_no_junction(0, "file name must not be code");
1.9 paf 94:
1.18 paf 95: const String& lfile_name=vfile_name.as_string();
1.9 paf 96:
1.12 paf 97: void *data; size_t size;
1.9 paf 98: file_read(pool, r.absolute(lfile_name), data, size, false/*binary*/);
99:
1.18 paf 100: char *user_file_name=params->size()==1?lfile_name.cstr(String::UL_FILE_NAME)
1.26 paf 101: :params->get(1).as_string().cstr();
1.10 paf 102:
1.21 paf 103: static_cast<VFile *>(r.self)->set(true/*tainted*/, data, size,
1.20 paf 104: user_file_name, &r.mime_type_of(user_file_name));
1.9 paf 105: }
106:
1.26 paf 107: static void _stat(Request& r, const String& method_name, MethodParams *params) {
1.25 paf 108: Pool& pool=r.pool();
1.26 paf 109: Value& vfile_name=params->get_no_junction(0, "file name must not be code");
1.25 paf 110:
111: const String& lfile_name=vfile_name.as_string();
112:
113: size_t size=file_size(r.absolute(lfile_name));
114:
115: static_cast<VFile *>(r.self)->set(true/*tainted*/, 0/*no bytes*/, size);
116: }
117:
1.22 paf 118: static void append_env_pair(const Hash::Key& key, Hash::Val *value, void *info) {
119: Hash& hash=*static_cast<Hash *>(info);
120: hash.put(key, &static_cast<Value *>(value)->as_string());
121: }
1.29 paf 122:
123: static void pass_cgi_header_attribute(Array::Item *value, void *info) {
124: String& string=*static_cast<String *>(value);
125: Hash& hash=*static_cast<Hash *>(info);
126: int colon_pos=string.pos(":", 1);
127: if(colon_pos>0)
1.30 paf 128: hash.put(string.mid(0, colon_pos),
129: new(string.pool()) VString(string.mid(colon_pos+1, string.size())));
1.29 paf 130: }
1.36 ! paf 131: /// @todo fix `` in perl - they produced flipping consoles and no output to perl
1.26 paf 132: static void _cgi(Request& r, const String& method_name, MethodParams *params) {
1.21 paf 133: Pool& pool=r.pool();
134:
1.26 paf 135: Value& vfile_name=params->get_no_junction(0, "file name must not be code");
1.21 paf 136:
1.23 paf 137: const String& script_name=r.absolute(vfile_name.as_string());
138:
139: Hash env(pool);
140: #define PASS(key) \
141: String key(pool); \
142: if(const char *value=SAPI::get_env(pool, #key)) { \
143: key.APPEND_CONST(value); \
144: env.put(String(pool, #key), &key); \
145: }
146: #define INFO(key, value) \
147: String value(pool); \
148: if(r.info.value) { \
149: value.APPEND_CONST(r.info.value); \
150: env.put(String(pool, key), &value); \
151: }
152:
153: // const
154: String gateway_interface(pool, "CGI/1.1");
155: env.put(String(pool, "GATEWAY_INTERFACE"), &gateway_interface);
156: // from Request.info
157: INFO("DOCUMENT_ROOT", document_root);
158: INFO("PATH_TRANSLATED", path_translated);
159: INFO("SERVER_PROTOCOL", method);
160: INFO("QUERY_STRING", query_string);
161: INFO("REQUEST_URI", uri);
162: INFO("CONTENT_TYPE", content_type);
163: char content_length_cstr[MAX_NUMBER];
164: snprintf(content_length_cstr, MAX_NUMBER, "%u", r.info.content_length);
165: String content_length(pool, content_length_cstr);
166: env.put(String(pool, "CONTENT_LENGTH"), &content_length);
167: INFO("HTTP_COOKIE", cookie);
168: INFO("HTTP_USER_AGENT", user_agent);
169: // passing some SAPI:get_env-s
170: PASS(SERVER_NAME);
171: PASS(SERVER_PORT);
172: PASS(HTTP_REFERER);
173: PASS(REMOTE_ADDR);
174: PASS(REMOTE_HOST);
175: PASS(REMOTE_USER);
176: // SCRIPT_NAME
177: env.put(String(pool, "SCRIPT_NAME"), &script_name);
1.27 paf 178: #ifdef WIN32
179: // WIN32 shell
180: PASS(COMSPEC);
181: #endif
1.23 paf 182:
1.21 paf 183: if(params->size()>1) {
1.26 paf 184: Value& venv=params->get_no_junction(1, "env must not be code");
1.23 paf 185: if(Hash *user_env=venv.get_hash())
186: user_env->for_each(append_env_pair, &env);
187: else
1.21 paf 188: PTHROW(0, 0,
189: &method_name,
190: "env must be hash");
191: }
192:
193: Array *argv=0;
194: if(params->size()>2) {
195: argv=new(pool) Array(pool, params->size()-2);
196: for(int i=2; i<params->size(); i++)
1.26 paf 197: *argv+=¶ms->get(i).as_string();
1.21 paf 198: }
199:
200: const String in(pool, r.post_data, r.post_size);
201: String out(pool);
1.31 paf 202: //out.APPEND_CONST("content-type:text/plain\nheader:test-header\n\ntest-body");
203: //out<<in;
1.27 paf 204: String& err=*new(pool) String(pool);
1.30 paf 205: int exit_code=pa_exec(script_name, &env, argv, in, out, err);
1.21 paf 206:
207: VFile& self=*static_cast<VFile *>(r.self);
208: // construct with 'out' body and header
209: int delim_size;
1.30 paf 210: const char *eol_marker="\r\n"; size_t eol_marker_size=2;
211: int pos=out.pos("\r\n\r\n", delim_size=4);
1.29 paf 212: if(pos<0) {
1.30 paf 213: eol_marker="\n"; eol_marker_size=1;
214: pos=out.pos("\n\n", delim_size=2);
1.29 paf 215: }
1.21 paf 216: if(pos<0) {
1.22 paf 217: delim_size=0; // calm down, compiler
1.21 paf 218: PTHROW(0, 0,
219: &method_name,
1.27 paf 220: "output does not contain CGI header; exit code=%d; size=%u; text: \"%s\"",
221: exit_code, (uint)out.size(), out.cstr());
1.21 paf 222: }
223:
224: const String& header=out.mid(0, pos);
225: const String& body=out.mid(pos+delim_size, out.size());
226:
1.30 paf 227: // body
228: self.set(false/*not tainted*/, body.cstr(String::UL_AS_IS), body.size());
229:
230: // header to $fields
1.29 paf 231: {
232: Array rows(pool);
1.30 paf 233: header.split(rows, 0, eol_marker, eol_marker_size, String::UL_CLEAN);
1.29 paf 234: rows.for_each(pass_cgi_header_attribute, &self.fields());
235: }
1.21 paf 236:
237: // $exit-code
238: self.fields().put(
239: *new(pool) String(pool, "exit-code"),
240: new(pool) VInt(pool, exit_code));
241:
242: // $stderr
243: if(err.size()) {
244: self.fields().put(
245: *new(pool) String(pool, "stderr"),
246: new(pool) VString(err));
247:
248: SAPI::log(pool, "cgi: %s", err.cstr());
249: }
250: }
251:
1.32 paf 252: // constructor
253:
254: MFile::MFile(Pool& apool) : Methoded(apool) {
255: set_name(*NEW String(pool(), FILE_CLASS_NAME));
256:
1.1 paf 257:
258: // ^save[file-name]
1.32 paf 259: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.7 paf 260:
261: // ^delete[file-name]
1.32 paf 262: add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.8 paf 263:
264: // ^find[file-name]
265: // ^find[file-name]{when-not-found}
1.32 paf 266: add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.9 paf 267:
268: // ^load[disk-name]
269: // ^load[disk-name;user-name]
1.32 paf 270: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.25 paf 271:
272: // ^stat[disk-name]
1.32 paf 273: add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21 paf 274:
1.36 ! paf 275: // ^cgi[file-name]
! 276: // ^cgi[file-name;env hash]
! 277: // ^cgi[file-name;env hash;1cmd;2line;3ar;4g;5s]
1.32 paf 278: add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+5);
279: }
280:
281: // global variable
282:
283: Methoded *file_class;
284:
285: // creator
286:
287: Methoded *MFile_create(Pool& pool) {
288: return file_class=new(pool) MFile(pool);
1.1 paf 289: }
E-mail: