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