Annotation of parser3/src/classes/file.C, revision 1.33
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.33 ! paf 8: $Id: file.C,v 1.32 2001/04/28 08:43:47 paf Exp $
1.1 paf 9: */
10:
1.32 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.28 paf 131: /**
132: ^exec[file-name]
133: ^exec[file-name;env hash]
134: ^exec[file-name;env hash;cmd;line;arg;s]
135: @todo fix `` in perl - they produced flipping consoles and no output to perl
136: */
1.26 paf 137: static void _cgi(Request& r, const String& method_name, MethodParams *params) {
1.21 paf 138: Pool& pool=r.pool();
139:
1.26 paf 140: Value& vfile_name=params->get_no_junction(0, "file name must not be code");
1.21 paf 141:
1.23 paf 142: const String& script_name=r.absolute(vfile_name.as_string());
143:
144: Hash env(pool);
145: #define PASS(key) \
146: String key(pool); \
147: if(const char *value=SAPI::get_env(pool, #key)) { \
148: key.APPEND_CONST(value); \
149: env.put(String(pool, #key), &key); \
150: }
151: #define INFO(key, value) \
152: String value(pool); \
153: if(r.info.value) { \
154: value.APPEND_CONST(r.info.value); \
155: env.put(String(pool, key), &value); \
156: }
157:
158: // const
159: String gateway_interface(pool, "CGI/1.1");
160: env.put(String(pool, "GATEWAY_INTERFACE"), &gateway_interface);
161: // from Request.info
162: INFO("DOCUMENT_ROOT", document_root);
163: INFO("PATH_TRANSLATED", path_translated);
164: INFO("SERVER_PROTOCOL", method);
165: INFO("QUERY_STRING", query_string);
166: INFO("REQUEST_URI", uri);
167: INFO("CONTENT_TYPE", content_type);
168: char content_length_cstr[MAX_NUMBER];
169: snprintf(content_length_cstr, MAX_NUMBER, "%u", r.info.content_length);
170: String content_length(pool, content_length_cstr);
171: env.put(String(pool, "CONTENT_LENGTH"), &content_length);
172: INFO("HTTP_COOKIE", cookie);
173: INFO("HTTP_USER_AGENT", user_agent);
174: // passing some SAPI:get_env-s
175: PASS(SERVER_NAME);
176: PASS(SERVER_PORT);
177: PASS(HTTP_REFERER);
178: PASS(REMOTE_ADDR);
179: PASS(REMOTE_HOST);
180: PASS(REMOTE_USER);
181: // SCRIPT_NAME
182: env.put(String(pool, "SCRIPT_NAME"), &script_name);
1.27 paf 183: #ifdef WIN32
184: // WIN32 shell
185: PASS(COMSPEC);
186: #endif
1.23 paf 187:
1.21 paf 188: if(params->size()>1) {
1.26 paf 189: Value& venv=params->get_no_junction(1, "env must not be code");
1.23 paf 190: if(Hash *user_env=venv.get_hash())
191: user_env->for_each(append_env_pair, &env);
192: else
1.21 paf 193: PTHROW(0, 0,
194: &method_name,
195: "env must be hash");
196: }
197:
198: Array *argv=0;
199: if(params->size()>2) {
200: argv=new(pool) Array(pool, params->size()-2);
201: for(int i=2; i<params->size(); i++)
1.26 paf 202: *argv+=¶ms->get(i).as_string();
1.21 paf 203: }
204:
205: const String in(pool, r.post_data, r.post_size);
206: String out(pool);
1.31 paf 207: //out.APPEND_CONST("content-type:text/plain\nheader:test-header\n\ntest-body");
208: //out<<in;
1.27 paf 209: String& err=*new(pool) String(pool);
1.30 paf 210: int exit_code=pa_exec(script_name, &env, argv, in, out, err);
1.21 paf 211:
212: VFile& self=*static_cast<VFile *>(r.self);
213: // construct with 'out' body and header
214: int delim_size;
1.30 paf 215: const char *eol_marker="\r\n"; size_t eol_marker_size=2;
216: int pos=out.pos("\r\n\r\n", delim_size=4);
1.29 paf 217: if(pos<0) {
1.30 paf 218: eol_marker="\n"; eol_marker_size=1;
219: pos=out.pos("\n\n", delim_size=2);
1.29 paf 220: }
1.21 paf 221: if(pos<0) {
1.22 paf 222: delim_size=0; // calm down, compiler
1.21 paf 223: PTHROW(0, 0,
224: &method_name,
1.27 paf 225: "output does not contain CGI header; exit code=%d; size=%u; text: \"%s\"",
226: exit_code, (uint)out.size(), out.cstr());
1.21 paf 227: }
228:
229: const String& header=out.mid(0, pos);
230: const String& body=out.mid(pos+delim_size, out.size());
231:
1.30 paf 232: // body
233: self.set(false/*not tainted*/, body.cstr(String::UL_AS_IS), body.size());
234:
235: // header to $fields
1.29 paf 236: {
237: Array rows(pool);
1.30 paf 238: header.split(rows, 0, eol_marker, eol_marker_size, String::UL_CLEAN);
1.29 paf 239: rows.for_each(pass_cgi_header_attribute, &self.fields());
240: }
1.21 paf 241:
242: // $exit-code
243: self.fields().put(
244: *new(pool) String(pool, "exit-code"),
245: new(pool) VInt(pool, exit_code));
246:
247: // $stderr
248: if(err.size()) {
249: self.fields().put(
250: *new(pool) String(pool, "stderr"),
251: new(pool) VString(err));
252:
253: SAPI::log(pool, "cgi: %s", err.cstr());
254: }
255: }
256:
1.32 paf 257: // constructor
258:
259: MFile::MFile(Pool& apool) : Methoded(apool) {
260: set_name(*NEW String(pool(), FILE_CLASS_NAME));
261:
1.1 paf 262:
263: // ^save[file-name]
1.32 paf 264: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.7 paf 265:
266: // ^delete[file-name]
1.32 paf 267: add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.8 paf 268:
269: // ^find[file-name]
270: // ^find[file-name]{when-not-found}
1.32 paf 271: add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.9 paf 272:
273: // ^load[disk-name]
274: // ^load[disk-name;user-name]
1.32 paf 275: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.25 paf 276:
277: // ^stat[disk-name]
1.32 paf 278: add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21 paf 279:
280: // ^exec[file-name]
281: // ^exec[file-name;env hash]
282: // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
1.32 paf 283: add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+5);
284: }
285:
286: // global variable
287:
288: Methoded *file_class;
289:
290: // creator
291:
292: Methoded *MFile_create(Pool& pool) {
293: return file_class=new(pool) MFile(pool);
1.1 paf 294: }
E-mail: