Annotation of parser3/src/classes/file.C, revision 1.156
1.17 paf 1: /** @file
2: Parser: @b file parser class.
3:
1.136 paf 4: Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.72 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.91 paf 6: */
1.17 paf 7:
1.156 ! misha 8: static const char * const IDENT_FILE_C="$Date: 2007/04/16 10:08:17 $";
1.47 parser 9:
10: #include "pa_config_includes.h"
11:
12: #include "pcre.h"
1.1 paf 13:
1.35 paf 14: #include "classes.h"
1.111 paf 15: #include "pa_vmethod_frame.h"
16:
1.1 paf 17: #include "pa_request.h"
18: #include "pa_vfile.h"
1.11 paf 19: #include "pa_table.h"
1.21 paf 20: #include "pa_vint.h"
1.24 paf 21: #include "pa_exec.h"
1.40 parser 22: #include "pa_vdate.h"
1.47 parser 23: #include "pa_dir.h"
24: #include "pa_vtable.h"
1.67 paf 25: #include "pa_charset.h"
1.109 paf 26: #include "pa_charsets.h"
1.121 paf 27: #include "pa_sql_connection.h"
1.147 misha 28: #include "pa_md5.h"
1.1 paf 29:
1.32 paf 30: // defines
31:
1.48 parser 32: #define TEXT_MODE_NAME "text"
1.125 paf 33: #define BINARY_MODE_NAME "binary"
1.90 paf 34: #define STDIN_EXEC_PARAM_NAME "stdin"
1.109 paf 35: #define CHARSET_EXEC_PARAM_NAME "charset"
1.48 parser 36:
1.131 paf 37: #define NAME_NAME "name"
38:
1.152 misha 39: #define FILE_NAME_MUST_BE_STRING "file name must be string"
40: #define FILE_NAME_MUST_NOT_BE_CODE "file name must not be code"
41:
1.132 paf 42: // externs
43:
44: extern String sql_limit_name;
45: extern String sql_offset_name;
46:
1.111 paf 47: // class
48:
49: class MFile: public Methoded {
50: public: // VStateless_class
51:
1.134 paf 52: Value* create_new_value(Pool&, HashStringValue&) { return new VFile(); }
1.111 paf 53:
54: public: // Methoded
55: bool used_directly() { return true; }
56:
57: public:
58: MFile();
59:
60: };
61:
62: // global variable
63:
64: DECLARE_CLASS_VAR(file, new MFile, 0);
65:
1.83 paf 66: // consts
67:
68: /// from apache-1.3|src|support|suexec.c
1.111 paf 69: static const char* suexec_safe_env_lst[]={
1.83 paf 70: "AUTH_TYPE",
71: "CONTENT_LENGTH",
72: "CONTENT_TYPE",
73: "DATE_GMT",
74: "DATE_LOCAL",
75: "DOCUMENT_NAME",
76: "DOCUMENT_PATH_INFO",
77: "DOCUMENT_ROOT",
78: "DOCUMENT_URI",
79: "FILEPATH_INFO",
80: "GATEWAY_INTERFACE",
81: "LAST_MODIFIED",
82: "PATH_INFO",
83: "PATH_TRANSLATED",
84: "QUERY_STRING",
85: "QUERY_STRING_UNESCAPED",
86: "REMOTE_ADDR",
87: "REMOTE_HOST",
88: "REMOTE_IDENT",
89: "REMOTE_PORT",
90: "REMOTE_USER",
91: "REDIRECT_QUERY_STRING",
92: "REDIRECT_STATUS",
93: "REDIRECT_URL",
94: "REQUEST_METHOD",
95: "REQUEST_URI",
96: "SCRIPT_FILENAME",
97: "SCRIPT_NAME",
98: "SCRIPT_URI",
99: "SCRIPT_URL",
100: "SERVER_ADMIN",
101: "SERVER_NAME",
102: "SERVER_ADDR",
103: "SERVER_PORT",
104: "SERVER_PROTOCOL",
105: "SERVER_SOFTWARE",
106: "UNIQUE_ID",
107: "USER_NAME",
108: "TZ",
109: NULL
110: };
111:
1.111 paf 112: // statics
1.33 paf 113:
1.112 paf 114: static const String::Body adate_name("adate");
115: static const String::Body mdate_name("mdate");
116: static const String::Body cdate_name("cdate");
1.32 paf 117:
1.1 paf 118: // methods
119:
1.125 paf 120: static bool is_text_mode(const String& mode) {
121: if(mode==TEXT_MODE_NAME)
122: return true;
123: if(mode==BINARY_MODE_NAME)
124: return false;
1.156 ! misha 125: throw Exception(PARSER_RUNTIME,
1.125 paf 126: &mode,
127: "is invalid mode, must be either '"TEXT_MODE_NAME"' or '"BINARY_MODE_NAME"'");
128: }
129:
1.111 paf 130: static void _save(Request& r, MethodParams& params) {
131: Value& vmode_name=params. as_no_junction(0, "mode must not be code");
1.152 misha 132: Value& vfile_name=params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE);
1.4 paf 133:
1.7 paf 134: // save
1.111 paf 135: GET_SELF(r, VFile).save(r.absolute(vfile_name.as_string()),
1.125 paf 136: is_text_mode(vmode_name.as_string()));
1.7 paf 137: }
138:
1.111 paf 139: static void _delete(Request& r, MethodParams& params) {
1.152 misha 140: Value& vfile_name=params.as_no_junction(0, FILE_NAME_MUST_NOT_BE_CODE);
1.7 paf 141:
142: // unlink
1.68 paf 143: file_delete(r.absolute(vfile_name.as_string()));
1.1 paf 144: }
145:
1.111 paf 146: static void _move(Request& r, MethodParams& params) {
147: Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");
148: Value& vto_file_name=params.as_no_junction(1, "to file name must not be code");
1.45 parser 149:
1.51 parser 150: // move
1.68 paf 151: file_move(
1.45 parser 152: r.absolute(vfrom_file_name.as_string()),
153: r.absolute(vto_file_name.as_string()));
154: }
155:
1.148 misha 156: static void copy_process_source(
157: struct stat& ,
158: int from_file,
159: const String& , const char* /*fname*/, bool,
160: void *context) {
161: int& to_file=*static_cast<int *>(context);
162:
163: int nCount=0;
164: do {
165: unsigned char buffer[FILE_BUFFER_SIZE];
1.150 misha 166: nCount = file_block_read(from_file, buffer, sizeof(buffer));
1.148 misha 167: int written=write(to_file, buffer, nCount);
168: if( written < 0 )
169: throw Exception(0,
170: 0,
171: "write failed: %s (%d)", strerror(errno), errno);
172:
173: } while(nCount > 0);
174: }
175:
176: static void copy_open_target(int f, void *from_spec) {
177: String& file_spec=*static_cast<String *>(from_spec);
178: file_read_action_under_lock(file_spec, "copy", copy_process_source, &f);
179: };
180:
181: static void _copy(Request& r, MethodParams& params) {
182: Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");
183: Value& vto_file_name=params.as_no_junction(1, "to file name must not be code");
184:
185: String from_spec = r.absolute(vfrom_file_name.as_string());
186: const String& to_spec = r.absolute(vto_file_name.as_string());
187:
1.153 misha 188: // create_dir_for_file(to_spec);
1.148 misha 189:
190: file_write_action_under_lock(
191: to_spec,
192: "copy",
193: copy_open_target,
1.149 misha 194: &from_spec);
1.148 misha 195: }
196:
1.111 paf 197: static void _load_pass_param(
198: HashStringValue::key_type key,
199: HashStringValue::value_type value,
200: HashStringValue *dest) {
201: dest->put(key, value);
202: }
203: static void _load(Request& r, MethodParams& params) {
204: Value& vmode_name=params. as_no_junction(0, "mode must not be code");
1.152 misha 205: const String& lfile_name=r.absolute(params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE).as_string());
1.111 paf 206: Value* third_param=params.count()>2?¶ms.as_no_junction(2, "filename or options must not be code")
207: :0;
208: HashStringValue* third_param_hash=third_param?third_param->get_hash():0;
209: size_t alt_filename_param_index=2;
1.104 paf 210: if(third_param_hash)
211: alt_filename_param_index++;
1.9 paf 212:
1.132 paf 213: HashStringValue* options=third_param_hash;
214: size_t offset=0;
215: size_t limit=0;
216: if(options) {
217: options=new HashStringValue(*options);
218: if(Value *voffset=(Value *)options->get(sql_offset_name)) {
219: offset=r.process_to_value(*voffset).as_int();
220: }
221: if(Value *vlimit=(Value *)options->get(sql_limit_name)) {
222: limit=r.process_to_value(*vlimit).as_int();
223: }
224: // no check on options count here, see file_read
225: }
1.111 paf 226: File_read_result file=file_read(r.charsets, lfile_name,
1.125 paf 227: is_text_mode(vmode_name.as_string()),
1.132 paf 228: options, true, 0, offset, limit
1.104 paf 229: );
1.9 paf 230:
1.111 paf 231: const char *user_file_name=params.count()>alt_filename_param_index?
1.152 misha 232: params.as_string(alt_filename_param_index, FILE_NAME_MUST_BE_STRING).cstr()
1.111 paf 233: :lfile_name.cstr(String::L_FILE_SPEC);
234:
235: Value* vcontent_type=0;
236: if(file.headers)
1.129 paf 237: {
238: if(Value* remote_content_type=file.headers->get("CONTENT-TYPE"))
239: vcontent_type=new VString(*new String(remote_content_type->as_string().cstr()));
240: }
1.104 paf 241: if(!vcontent_type)
1.111 paf 242: vcontent_type=new VString(r.mime_type_of(user_file_name));
1.10 paf 243:
1.111 paf 244: VFile& self=GET_SELF(r, VFile);
245: self.set(true/*tainted*/, file.str, file.length, user_file_name, vcontent_type);
246: if(file.headers)
1.143 paf 247: file.headers->for_each<HashStringValue*>(_load_pass_param, &self.fields());
1.9 paf 248: }
249:
1.138 paf 250: static void _create(Request& r, MethodParams& params) {
251: Value& vmode_name=params. as_no_junction(0, "mode must not be code");
252: if(!is_text_mode(vmode_name.as_string()))
1.156 ! misha 253: throw Exception(PARSER_RUNTIME,
1.138 paf 254: 0,
255: "only text mode is currently supported");
256:
257: const char* user_file_name_cstr=r.absolute(
1.152 misha 258: params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE).as_string()).cstr(String::L_FILE_SPEC);
1.138 paf 259:
260: const String& content=params.as_string(2, "content must be string");
261: const char* content_cstr=content.cstr(String::L_UNSPECIFIED); // explode content, honor tainting changes
262:
263: VString* vcontent_type=new VString(r.mime_type_of(user_file_name_cstr));
264:
265: VFile& self=GET_SELF(r, VFile);
266: self.set(true/*tainted*/, content_cstr, strlen(content_cstr), user_file_name_cstr, vcontent_type);
267: }
268:
1.111 paf 269: static void _stat(Request& r, MethodParams& params) {
1.152 misha 270: Value& vfile_name=params.as_no_junction(0, FILE_NAME_MUST_NOT_BE_CODE);
1.25 paf 271:
272: const String& lfile_name=vfile_name.as_string();
273:
1.40 parser 274: size_t size;
275: time_t atime, mtime, ctime;
276: file_stat(r.absolute(lfile_name),
277: size,
278: atime, mtime, ctime);
1.25 paf 279:
1.111 paf 280: VFile& self=GET_SELF(r, VFile);
281: self.set(true/*tainted*/, 0/*no bytes*/, size);
282: HashStringValue& ff=self.fields();
283: ff.put(adate_name, new VDate(atime));
284: ff.put(mdate_name, new VDate(mtime));
285: ff.put(cdate_name, new VDate(ctime));
286: ff.put(content_type_name, new VString(r.mime_type_of(lfile_name.cstr(String::L_FILE_SPEC))));
1.25 paf 287: }
288:
1.111 paf 289: static bool is_safe_env_key(const char* key) {
290: for(const char* validator=key; *validator; validator++) {
291: char c=*validator;
292: if(!(c>='A' && c<='Z' || c>='0' && c<='9' || c=='_' || c=='-'))
293: return false;
294: }
1.88 paf 295: if(strncasecmp(key, "HTTP_", 5)==0)
1.83 paf 296: return true;
1.87 paf 297: if(strncasecmp(key, "CGI_", 4)==0)
1.83 paf 298: return true;
299: for(int i=0; suexec_safe_env_lst[i]; i++) {
1.87 paf 300: if(strcasecmp(key, suexec_safe_env_lst[i])==0)
1.83 paf 301: return true;
302: }
303: return false;
304: }
1.90 paf 305: #ifndef DOXYGEN
306: struct Append_env_pair_info {
1.141 paf 307: Request_charsets* charsets;
1.111 paf 308: HashStringString* env;
1.100 paf 309: Value* vstdin;
1.90 paf 310: };
311: #endif
1.111 paf 312: static void append_env_pair(
313: HashStringValue::key_type akey,
314: HashStringValue::value_type avalue,
315: Append_env_pair_info *info) {
316: if(akey==STDIN_EXEC_PARAM_NAME) {
317: info->vstdin=avalue;
318: } else if(akey==CHARSET_EXEC_PARAM_NAME) {
1.141 paf 319: // ignore, already processed
1.90 paf 320: } else {
1.111 paf 321: if(!is_safe_env_key(akey.cstr()))
1.156 ! misha 322: throw Exception(PARSER_RUNTIME,
1.111 paf 323: new String(akey, String::L_TAINTED),
1.90 paf 324: "not safe environment variable");
1.141 paf 325: info->env->put(akey, avalue->as_string().cstr_to_string_body(String::L_UNSPECIFIED, 0, info->charsets));
1.90 paf 326: }
1.22 paf 327: }
1.94 paf 328: #ifndef DOXYGEN
329: struct Pass_cgi_header_attribute_info {
1.111 paf 330: Charset* charset;
331: HashStringValue* fields;
332: Value* content_type;
1.94 paf 333: };
334: #endif
1.111 paf 335: static void pass_cgi_header_attribute(
336: ArrayString::element_type astring,
337: Pass_cgi_header_attribute_info* info) {
338: size_t colon_pos=astring->pos(':');
1.130 paf 339: if(colon_pos!=STRING_NOT_FOUND) {
1.111 paf 340: const String& key=astring->mid(0, colon_pos).change_case(
341: *info->charset, String::CC_UPPER);
1.130 paf 342: Value* value=new VString(astring->mid(colon_pos+1, astring->length()).trim());
1.111 paf 343: info->fields->put(key, value);
1.94 paf 344: if(key=="CONTENT-TYPE")
1.111 paf 345: info->content_type=value;
1.94 paf 346: }
1.29 paf 347: }
1.155 misha 348:
349: static void append_to_argv(Request& r, ArrayString& argv, const String* str){
350: if( str->length() ){
351: argv+=new String(str->cstr_to_string_body(String::L_UNSPECIFIED, 0, &r.charsets), String::L_AS_IS);
352: }
353: }
354:
1.90 paf 355: /// @todo fix `` in perl - they produced flipping consoles and no output to perl
1.111 paf 356: static void _exec_cgi(Request& r, MethodParams& params,
1.41 parser 357: bool cgi) {
1.21 paf 358:
1.152 misha 359: Value& vfile_name=params.as_no_junction(0, FILE_NAME_MUST_NOT_BE_CODE);
1.21 paf 360:
1.23 paf 361: const String& script_name=r.absolute(vfile_name.as_string());
362:
1.111 paf 363: HashStringString env;
1.62 paf 364: #define ECSTR(name, value_cstr) \
1.111 paf 365: if(value_cstr) \
366: env.put( \
1.112 paf 367: String::Body(#name), \
368: String::Body(value_cstr, 0)); \
1.82 paf 369: // passing SAPI::environment
1.111 paf 370: if(const char *const *pairs=SAPI::environment(r.sapi_info)) {
371: while(const char* pair=*pairs++)
372: if(const char* eq_at=strchr(pair, '='))
373: if(eq_at[1]) // has value
374: env.put(
375: pa_strdup(pair, eq_at-pair),
376: pa_strdup(eq_at+1, 0));
1.82 paf 377: }
378:
1.23 paf 379: // const
1.63 paf 380: ECSTR(GATEWAY_INTERFACE, "CGI/1.1");
1.23 paf 381: // from Request.info
1.111 paf 382: ECSTR(DOCUMENT_ROOT, r.request_info.document_root);
383: ECSTR(PATH_TRANSLATED, r.request_info.path_translated);
384: ECSTR(REQUEST_METHOD, r.request_info.method);
385: ECSTR(QUERY_STRING, r.request_info.query_string);
386: ECSTR(REQUEST_URI, r.request_info.uri);
387: ECSTR(CONTENT_TYPE, r.request_info.content_type);
1.23 paf 388: char content_length_cstr[MAX_NUMBER];
1.111 paf 389: snprintf(content_length_cstr, MAX_NUMBER, "%u", r.request_info.content_length);
390: //String content_length(content_length_cstr);
1.62 paf 391: ECSTR(CONTENT_LENGTH, content_length_cstr);
1.82 paf 392: // SCRIPT_*
1.119 paf 393: env.put(String::Body("SCRIPT_NAME"), script_name);
394: //env.put(String::Body("SCRIPT_FILENAME"), ??&script_name);
1.23 paf 395:
1.111 paf 396: bool stdin_specified=false;
1.90 paf 397: // environment & stdin from param
1.111 paf 398: String *in=new String();
1.109 paf 399: Charset *charset=0; // default script works raw_in 'source' charset = no transcoding needed
1.111 paf 400: if(params.count()>1) {
401: Value& venv=params.as_no_junction(1, "env must not be code");
402: if(HashStringValue* user_env=venv.get_hash()) {
1.141 paf 403: // $.charset [previewing to handle URI pieces]
404: if(Value* vcharset=user_env->get(CHARSET_EXEC_PARAM_NAME))
405: charset=&charsets.get(vcharset->as_string()
406: .change_case(r.charsets.source(), String::CC_UPPER));
407:
408: // $.others
409: Append_env_pair_info info={&r.charsets, &env, 0};
410: {
1.144 paf 411: // influence tainting
412: // main target -- $.QUERY_STRING -- URLencoding of tainted pieces to String::L_URI lang
1.141 paf 413: Temp_client_charset temp(r.charsets, charset? *charset: r.charsets.source());
1.143 paf 414: user_env->for_each<Append_env_pair_info*>(append_env_pair, &info);
1.141 paf 415: }
1.109 paf 416: // $.stdin
1.103 paf 417: if(info.vstdin) {
1.111 paf 418: stdin_specified=true;
419: if(const String* sstdin=info.vstdin->get_string()) {
420: in->append(*sstdin, String::L_CLEAN, true);
1.103 paf 421: } else
1.111 paf 422: if(VFile* vfile=static_cast<VFile *>(info.vstdin->as("file", false)))
423: in->append_know_length((const char* )vfile->value_ptr(), vfile->value_size(), String::L_TAINTED);
1.100 paf 424: else
1.156 ! misha 425: throw Exception(PARSER_RUNTIME,
1.111 paf 426: 0,
1.100 paf 427: STDIN_EXEC_PARAM_NAME " parameter must be string or file");
1.103 paf 428: }
1.90 paf 429: }
1.21 paf 430: }
431:
1.90 paf 432: // argv from params
1.111 paf 433: ArrayString argv;
434: if(params.count()>2) {
1.144 paf 435: // influence tainting
436: // main target -- URLencoding of tainted pieces to String::L_URI lang
437: Temp_client_charset temp(r.charsets, charset? *charset: r.charsets.source());
1.154 misha 438:
1.144 paf 439: for(size_t i=2; i<params.count(); i++) {
1.154 misha 440: Value& param=params.as_no_junction(i, "parameter must not be code");
441: if(param.is_defined()){
442: if(param.is_string()){
1.155 misha 443: append_to_argv(r, argv, param.get_string());
1.154 misha 444: } else {
1.155 misha 445: Table* table=param.get_table();
446: if(table){
447: for(size_t i=0; i<table->count(); i++) {
448: append_to_argv(r, argv, table->get(i)->get(0));
1.154 misha 449: }
450: } else {
1.156 ! misha 451: throw Exception(PARSER_RUNTIME,
1.154 misha 452: 0,
453: "parameter must be string or table");
454: }
455: }
1.145 misha 456: }
1.144 paf 457: }
1.21 paf 458: }
1.90 paf 459:
1.109 paf 460: // transcode if necessary
461: if(charset) {
1.111 paf 462: Charset::transcode(env, r.charsets.source(), *charset);
463: Charset::transcode(argv, r.charsets.source(), *charset);
464: in=&Charset::transcode(*in, r.charsets.source(), *charset);
465: }
466: // @todo
467: // ifdef WIN32 do OEM->ANSI transcode on some(.cmd?) programs to
468: // match silent conversion in OS
469:
470: // exec!
471: PA_exec_result execution=
472: pa_exec(false/*forced_allow*/, script_name, &env, argv, *in);
473:
474: String *real_out=&execution.out;
475: String *real_err=&execution.err;
476: // transcode if necessary
477: if(charset) {
478: real_out=&Charset::transcode(*real_out, *charset, r.charsets.source());
479: real_err=&Charset::transcode(*real_err, *charset, r.charsets.source());
1.109 paf 480: }
481:
1.111 paf 482: VFile& self=GET_SELF(r, VFile);
1.109 paf 483:
1.111 paf 484: const String* body=real_out; // ^file:exec
485: const char* eol_marker=0; size_t eol_marker_size;
486: const String* header=0;
1.41 parser 487: if(cgi) { // ^file:cgi
1.111 paf 488: // construct with 'out' body and header
489: size_t dos_pos=real_out->pos("\r\n\r\n", 4);
490: size_t unix_pos=real_out->pos("\n\n", 2);
491:
492: bool unix_header_break;
493: switch((dos_pos!=STRING_NOT_FOUND?10:00) + (unix_pos!=STRING_NOT_FOUND?01:00)) {
494: case 10: // dos
495: unix_header_break=false;
496: break;
497: case 01: // unix
498: unix_header_break=true;
499: break;
500: case 11: // dos & unix
501: unix_header_break=unix_pos<dos_pos;
502: break;
503: default: // 00
504: unix_header_break=false; // calm down, compiler
1.74 paf 505: throw Exception(0,
1.111 paf 506: 0,
1.90 paf 507: "output does not contain CGI header; "
508: "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"",
1.111 paf 509: execution.status,
510: (uint)real_out->length(), real_out->cstr(),
511: (uint)real_err->length(), real_err->cstr());
512: break; //never reached
513: }
514:
515: int header_break_pos;
516: if(unix_header_break) {
517: header_break_pos=unix_pos;
518: eol_marker="\n"; eol_marker_size=1;
519: } else {
520: header_break_pos=dos_pos;
521: eol_marker="\r\n"; eol_marker_size=2;
522: }
1.21 paf 523:
1.109 paf 524: header=&real_out->mid(0, header_break_pos);
1.111 paf 525: body=&real_out->mid(header_break_pos+eol_marker_size*2, real_out->length());
1.29 paf 526: }
1.41 parser 527: // body
1.111 paf 528: self.set(false/*not tainted*/, body->cstr(), body->length());
1.94 paf 529:
530: // $fields << header
1.98 paf 531: if(header && eol_marker) {
1.111 paf 532: ArrayString rows;
533: size_t pos_after=0;
534: header->split(rows, pos_after, eol_marker);
1.116 paf 535: Pass_cgi_header_attribute_info info={0, 0, 0};
1.111 paf 536: info.charset=&r.charsets.source();
537: info.fields=&self.fields();
1.94 paf 538: rows.for_each(pass_cgi_header_attribute, &info);
539: if(info.content_type)
1.111 paf 540: self.fields().put(content_type_name, info.content_type);
1.94 paf 541: }
1.21 paf 542:
1.42 parser 543: // $status
1.111 paf 544: self.fields().put(file_status_name, new VInt(execution.status));
1.21 paf 545:
546: // $stderr
1.111 paf 547: if(real_err->length())
1.21 paf 548: self.fields().put(
1.119 paf 549: String::Body("stderr"),
1.111 paf 550: new VString(*real_err));
1.21 paf 551: }
1.111 paf 552: static void _exec(Request& r, MethodParams& params) {
553: _exec_cgi(r, params, false);
1.41 parser 554: }
1.111 paf 555: static void _cgi(Request& r, MethodParams& params) {
556: _exec_cgi(r, params, true);
1.41 parser 557: }
558:
1.111 paf 559: static void _list(Request& r, MethodParams& params) {
560: Value& relative_path=params.as_no_junction(0, "path must not be code");
1.47 parser 561:
1.111 paf 562: const String* regexp;
1.47 parser 563: pcre *regexp_code;
1.81 paf 564: const int ovecsize=(1/*match*/)*3;
565: int ovector[ovecsize];
1.111 paf 566: if(params.count()>1) {
567: regexp=¶ms.as_no_junction(1, "regexp must not be code").as_string();
1.47 parser 568:
1.111 paf 569: const char* pattern=regexp->cstr();
570: const char* errptr;
1.47 parser 571: int erroffset;
572: regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL,
573: &errptr, &erroffset,
1.111 paf 574: r.charsets.source().pcre_tables);
1.47 parser 575:
576: if(!regexp_code)
1.74 paf 577: throw Exception(0,
1.111 paf 578: ®exp->mid(erroffset, regexp->length()),
1.47 parser 579: "regular expression syntax error - %s", errptr);
1.114 paf 580: } else {
581: regexp=0; // not used, just to calm down compiler
1.47 parser 582: regexp_code=0;
1.114 paf 583: }
1.47 parser 584:
585:
1.111 paf 586: const char* absolute_path_cstr=r.absolute(relative_path.as_string()).cstr(String::L_FILE_SPEC);
1.47 parser 587:
1.111 paf 588: Table::columns_type columns(new ArrayString);
589: *columns+=new String("name");
590: Table& table=*new Table(columns);
1.47 parser 591:
592: LOAD_DIR(absolute_path_cstr,
1.111 paf 593: const char* file_name_cstr=ffblk.ff_name;
594: size_t file_name_size=strlen(file_name_cstr);
1.47 parser 595: bool suits=true;
596: if(regexp_code) {
597: int exec_result=pcre_exec(regexp_code, 0,
598: ffblk.ff_name, file_name_size, 0,
599: 0, ovector, ovecsize);
600:
601: if(exec_result==PCRE_ERROR_NOMATCH)
602: suits=false;
603: else if(exec_result<0) {
604: (*pcre_free)(regexp_code);
1.74 paf 605: throw Exception(0,
1.47 parser 606: regexp,
607: "regular expression execute (%d)",
608: exec_result);
609: }
610: }
611:
612: if(suits) {
1.111 paf 613: Table::element_type row(new ArrayString);
614: *row+=new String(pa_strdup(file_name_cstr, file_name_size), file_name_size, true);
615: table+=row;
1.47 parser 616: }
617: );
618:
619: if(regexp_code)
1.111 paf 620: pcre_free(regexp_code);
1.47 parser 621:
1.60 parser 622: // write out result
1.111 paf 623: r.write_no_lang(*new VTable(&table));
1.47 parser 624: }
1.21 paf 625:
1.69 paf 626: #ifndef DOXYGEN
627: struct Lock_execute_body_info {
1.111 paf 628: Request* r;
629: Value* body_code;
1.69 paf 630: };
631: #endif
1.111 paf 632: static void lock_execute_body(int , void *ainfo) {
633: Lock_execute_body_info& info=*static_cast<Lock_execute_body_info *>(ainfo);
1.69 paf 634: // execute body
1.78 paf 635: info.r->write_assign_lang(info.r->process(*info.body_code));
1.69 paf 636: };
1.111 paf 637: static void _lock(Request& r, MethodParams& params) {
1.152 misha 638: const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));
1.116 paf 639: Lock_execute_body_info info={
640: &r,
1.117 paf 641: ¶ms.as_junction(1, "body must be code")
1.116 paf 642: };
1.69 paf 643:
1.70 paf 644: file_write_action_under_lock(file_spec, "lock", lock_execute_body, &info);
1.69 paf 645: }
646:
1.111 paf 647: static int lastposafter(const String& s, size_t after, const char* substr, size_t substr_size, bool beforelast=false) {
1.114 paf 648: size_t size=0; // just to calm down compiler
1.89 paf 649: if(beforelast)
1.111 paf 650: size=s.length();
1.116 paf 651: size_t at;
1.112 paf 652: while((at=s.pos(String::Body(substr, substr_size), after))!=STRING_NOT_FOUND) {
1.89 paf 653: size_t newafter=at+substr_size/*skip substr*/;
654: if(beforelast && newafter==size)
655: break;
656: after=newafter;
657: }
658:
659: return after;
660: }
661:
1.111 paf 662: static void _find(Request& r, MethodParams& params) {
1.152 misha 663: const String& file_name=params.as_no_junction(0, FILE_NAME_MUST_NOT_BE_CODE).as_string();
1.111 paf 664: const String* file_spec;
1.90 paf 665: if(file_name.first_char()=='/')
666: file_spec=&file_name;
667: else
1.111 paf 668: file_spec=&r.relative(r.request_info.uri, file_name);
1.90 paf 669:
670: // easy way
1.142 paf 671: if(file_exist(r.absolute(*file_spec))) {
1.96 paf 672: r.write_assign_lang(*file_spec);
1.90 paf 673: return;
674: }
675:
676: // monkey way
677: int after_base_slash=lastposafter(*file_spec, 0, "/", 1);
1.111 paf 678: const String* dirname=&file_spec->mid(0, after_base_slash);
679: const String& basename=file_spec->mid(after_base_slash, file_spec->length());
1.90 paf 680:
681: int after_monkey_slash;
682: while((after_monkey_slash=lastposafter(*dirname, 0, "/", 1, true))>0) {
1.111 paf 683: String test_name;
684: test_name<<*(dirname=&dirname->mid(0, after_monkey_slash));
685: test_name<<basename;
1.142 paf 686: if(file_exist(r.absolute(test_name))) {
1.111 paf 687: r.write_assign_lang(test_name);
1.90 paf 688: return;
689: }
690: }
691:
692: // no way, not found
1.111 paf 693: if(params.count()==2) {
694: Value& not_found_code=params.as_junction(1, "not-found param must be code");
1.90 paf 695: r.write_pass_lang(r.process(not_found_code));
696: }
697: }
698:
1.111 paf 699: static void _dirname(Request& r, MethodParams& params) {
1.152 misha 700: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.89 paf 701: // /a/some.tar.gz > /a
702: // /a/b/ > /a
703: int afterslash=lastposafter(file_spec, 0, "/", 1, true);
704: if(afterslash>0)
705: r.write_assign_lang(file_spec.mid(0, afterslash==1?1:afterslash-1));
706: else
1.111 paf 707: r.write_assign_lang(String(".", 1));
1.89 paf 708: }
709:
1.111 paf 710: static void _basename(Request& r, MethodParams& params) {
1.152 misha 711: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.89 paf 712: // /a/some.tar.gz > some.tar.gz
713: int afterslash=lastposafter(file_spec, 0, "/", 1);
1.111 paf 714: r.write_assign_lang(file_spec.mid(afterslash, file_spec.length()));
1.89 paf 715: }
716:
1.111 paf 717: static void _justname(Request& r, MethodParams& params) {
1.152 misha 718: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.89 paf 719: // /a/some.tar.gz > some.tar
720: int afterslash=lastposafter(file_spec, 0, "/", 1);
721: int afterdot=lastposafter(file_spec, afterslash, ".", 1);
1.111 paf 722: r.write_assign_lang(file_spec.mid(afterslash, afterdot!=afterslash?afterdot-1:file_spec.length()));
1.89 paf 723: }
1.111 paf 724: static void _justext(Request& r, MethodParams& params) {
1.152 misha 725: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.89 paf 726: // /a/some.tar.gz > gz
727: int afterdot=lastposafter(file_spec, 0, ".", 1);
728: if(afterdot>0)
1.111 paf 729: r.write_assign_lang(file_spec.mid(afterdot, file_spec.length()));
1.89 paf 730: }
731:
1.111 paf 732: static void _fullpath(Request& r, MethodParams& params) {
1.152 misha 733: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.111 paf 734: const String* result;
1.102 paf 735: if(file_spec.first_char()=='/')
736: result=&file_spec;
737: else {
738: // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
739: const String& full_disk_path=r.absolute(file_spec);
1.111 paf 740: size_t document_root_length=strlen(r.request_info.document_root);
1.106 paf 741:
742: if(document_root_length>0) {
1.111 paf 743: char last_char=r.request_info.document_root[document_root_length-1];
1.106 paf 744: if(last_char == '/' || last_char == '\\')
745: --document_root_length;
746: }
1.111 paf 747: result=&full_disk_path.mid(document_root_length, full_disk_path.length());
1.102 paf 748: }
749: r.write_assign_lang(*result);
750: }
751:
1.121 paf 752: static void _sql_string(Request& r, MethodParams&) {
753: VFile& self=GET_SELF(r, VFile);
754:
755: const char *quoted=r.connection()->quote(self.value_ptr(), self.value_size());
756: r.write_assign_lang(*new String(quoted));
757: }
1.89 paf 758:
1.122 paf 759: #ifndef DOXYGEN
760: class File_sql_event_handlers: public SQL_Driver_query_event_handlers {
761: const String& statement_string; const char* statement_cstr;
762: int got_columns;
763: int got_cells;
764: public:
765: String::C value;
1.131 paf 766: const String* user_file_name;
767: const String* user_content_type;
1.122 paf 768: public:
769: File_sql_event_handlers(
770: const String& astatement_string, const char* astatement_cstr):
771: statement_string(astatement_string), statement_cstr(astatement_cstr),
772: got_columns(0),
773: got_cells(0),
774: user_file_name(0),
775: user_content_type(0) {}
776:
777: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
778: if(got_columns++==3) {
1.156 ! misha 779: error=SQL_Error(PARSER_RUNTIME, "result must contain not more then 3 columns");
1.122 paf 780: return true;
781: }
782: return false;
783: }
784: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
785: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
786: bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
787: try {
788: switch(got_cells++) {
789: case 0:
790: value=String::C(str, length);
791: break;
792: case 1:
1.131 paf 793: if(!user_file_name) // user not specified?
794: user_file_name=new String(str, length, true);
1.122 paf 795: break;
796: case 2:
1.131 paf 797: if(!user_content_type) // user not specified?
798: user_content_type=new String(str, length, true);
1.122 paf 799: break;
800: default:
1.156 ! misha 801: error=SQL_Error(PARSER_RUNTIME, "result must not contain more then one row, three rows");
1.122 paf 802: return true;
803: }
804: return false;
805: } catch(...) {
806: error=SQL_Error("exception occured in File_sql_event_handlers::add_row_cell");
807: return true;
808: }
809: }
810: };
811: #endif
812: static void _sql(Request& r, MethodParams& params) {
1.131 paf 813: Value& statement=params.as_junction(0, "statement must be code");
1.122 paf 814:
815: Temp_lang temp_lang(r, String::L_SQL);
816: const String& statement_string=r.process_to_string(statement);
817: const char* statement_cstr=
818: statement_string.cstr(String::L_UNSPECIFIED, r.connection());
819: File_sql_event_handlers handlers(statement_string, statement_cstr);
1.131 paf 820:
821: if(params.count()>1)
822: if(HashStringValue* options=
823: params.as_no_junction(1, "param must not be code").get_hash()) {
824: int valid_options=0;
825: if(Value* vfilename=options->get(NAME_NAME)) {
826: valid_options++;
827: handlers.user_file_name=&vfilename->as_string();
828: }
829: if(Value* vcontent_type=options->get(CONTENT_TYPE_NAME)) {
830: valid_options++;
831: handlers.user_content_type=&vcontent_type->as_string();
832: }
833: if(valid_options!=options->count())
1.156 ! misha 834: throw Exception(PARSER_RUNTIME,
1.131 paf 835: 0,
836: "called with invalid option");
837: }
838:
839:
1.122 paf 840: r.connection()->query(
1.123 paf 841: statement_cstr,
842: 0, 0,
843: 0, 0,
1.122 paf 844: handlers,
845: statement_string);
846:
847: if(!handlers.value)
1.156 ! misha 848: throw Exception(PARSER_RUNTIME,
1.122 paf 849: 0,
850: "produced no result");
851:
1.131 paf 852: const char* user_file_name_cstr=handlers.user_file_name? handlers.user_file_name->cstr(): 0;
1.122 paf 853:
854: VString* vcontent_type=handlers.user_content_type?
855: new VString(*handlers.user_content_type)
856: : user_file_name_cstr?
857: new VString(r.mime_type_of(user_file_name_cstr))
858: : 0;
859: VFile& self=GET_SELF(r, VFile);
860: self.set(true/*tainted*/, handlers.value.str, handlers.value.length, user_file_name_cstr, vcontent_type);
861: }
1.140 paf 862:
1.139 paf 863: static void _base64(Request& r, MethodParams& params) {
1.151 misha 864: bool dynamic = !(&r.get_self() == file_class);
865: if ( dynamic ){
1.140 paf 866: VFile& self=GET_SELF(r, VFile);
1.139 paf 867: if(params.count()) {
1.140 paf 868: // decode
1.139 paf 869: const char* cstr=params.as_string(0, "parameter must be string").cstr();
1.140 paf 870: char* decoded_cstr=0;
1.139 paf 871: size_t decoded_size=0;
872: pa_base64_decode(cstr, strlen(cstr), decoded_cstr, decoded_size);
873: if(decoded_cstr && decoded_size)
1.140 paf 874: self.set(true/*tainted*/, decoded_cstr, decoded_size);
875: } else {
876: // encode
877: const char* encoded=pa_base64_encode(self.value_ptr(), self.value_size());
878: r.write_assign_lang(*new String(encoded, 0, true/*once ?param=base64(something) was needed*/));
1.139 paf 879: }
1.151 misha 880: } else {
881: // encode
1.152 misha 882: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.151 misha 883: const char* encoded=pa_base64_encode(r.absolute(file_spec));
884: r.write_assign_lang(*new String(encoded, 0, true/*once ?param=base64(something) was needed*/));
885: }
1.139 paf 886: }
1.140 paf 887:
1.146 misha 888: static void _crc32(Request& r, MethodParams& params) {
889: unsigned long crc32 = 0;
890: if(&r.get_self() == file_class) {
891: // ^file:crc32[file-name]
892: if(params.count()) {
1.152 misha 893: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.146 misha 894: crc32=pa_crc32(r.absolute(file_spec));
895: } else {
1.156 ! misha 896: throw Exception(PARSER_RUNTIME,
1.146 misha 897: 0,
898: "file name must be defined");
899: }
900: } else {
901: // ^file.crc32[]
902: VFile& self=GET_SELF(r, VFile);
903: crc32=pa_crc32(self.value_ptr(), self.value_size());
904: }
905: r.write_no_lang(*new VInt(crc32));
906: }
907:
908:
1.147 misha 909: static void file_md5_file_action(
910: struct stat& finfo,
911: int f,
912: const String& , const char* /*fname*/, bool,
913: void *context)
914: {
915: PA_MD5_CTX& md5context=*static_cast<PA_MD5_CTX *>(context);
916: if(finfo.st_size) {
1.148 misha 917: int nCount=0;
1.147 misha 918: do {
919: unsigned char buffer[FILE_BUFFER_SIZE];
1.150 misha 920: nCount = file_block_read(f, buffer, sizeof(buffer));
1.147 misha 921: if ( nCount ){
922: pa_MD5Update(&md5context, (const unsigned char*)buffer, nCount);
923: }
1.148 misha 924: } while(nCount > 0);
1.147 misha 925: }
926: }
927:
928: const char* pa_md5(const String& file_spec)
929: {
930: PA_MD5_CTX context;
931: unsigned char digest[16];
932: pa_MD5Init(&context);
933: file_read_action_under_lock(file_spec, "md5", file_md5_file_action, &context);
934: pa_MD5Final(digest, &context);
935:
936: return hex_string(digest, sizeof(digest), false);
937: }
938:
939: const char* pa_md5(const char *in, size_t in_size)
940: {
941: PA_MD5_CTX context;
942: unsigned char digest[16];
943: pa_MD5Init(&context);
944: pa_MD5Update(&context, (const unsigned char*)in, in_size);
945: pa_MD5Final(digest, &context);
946:
947: return hex_string(digest, sizeof(digest), false);
948: }
949:
950: static void _md5(Request& r, MethodParams& params) {
951: const char* md5;
952: if(&r.get_self() == file_class) {
953: // ^file:md5[file-name]
954: if(params.count()) {
1.152 misha 955: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.147 misha 956: md5=pa_md5(r.absolute(file_spec));
957: } else {
1.156 ! misha 958: throw Exception(PARSER_RUNTIME,
1.147 misha 959: 0,
960: "file name must be defined");
961: }
962: } else {
963: // ^file.md5[]
964: VFile& self=GET_SELF(r, VFile);
965: md5=pa_md5(self.value_ptr(), self.value_size());
966:
967: }
968: r.write_no_lang(*new String(md5));
969: }
970:
1.32 paf 971: // constructor
972:
1.111 paf 973: MFile::MFile(): Methoded("file") {
1.146 misha 974: // ^file::create[text;user-name;string]
975: // ^file::create[binary;user-name;SOMEDAY SOMETHING]
1.138 paf 976: add_native_method("create", Method::CT_DYNAMIC, _create, 3, 3);
977:
1.146 misha 978: // ^file.save[mode;file-name]
1.48 parser 979: add_native_method("save", Method::CT_DYNAMIC, _save, 2, 2);
1.7 paf 980:
1.146 misha 981: // ^file:delete[file-name]
1.32 paf 982: add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.45 parser 983:
1.146 misha 984: // ^file:move[from-file-name;to-file-name]
1.45 parser 985: add_native_method("move", Method::CT_STATIC, _move, 2, 2);
1.8 paf 986:
1.146 misha 987: // ^file::load[mode;disk-name]
988: // ^file::load[mode;disk-name;user-name]
1.48 parser 989: add_native_method("load", Method::CT_DYNAMIC, _load, 2, 3);
1.25 paf 990:
1.146 misha 991: // ^file::stat[disk-name]
1.32 paf 992: add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21 paf 993:
1.146 misha 994: // ^file::cgi[file-name]
995: // ^file::cgi[file-name;env hash]
996: // ^file::cgi[file-name;env hash;1cmd;2line;3ar;4g;5s]
1.135 paf 997: add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+50);
1.41 parser 998:
1.146 misha 999: // ^file::exec[file-name]
1000: // ^file::exec[file-name;env hash]
1001: // ^file::exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
1.135 paf 1002: add_native_method("exec", Method::CT_DYNAMIC, _exec, 1, 2+50);
1.47 parser 1003:
1004: // ^file:list[path]
1005: // ^file:list[path][regexp]
1006: add_native_method("list", Method::CT_STATIC, _list, 1, 2);
1.69 paf 1007:
1008: // ^file:lock[path]{code}
1009: add_native_method("lock", Method::CT_STATIC, _lock, 2, 2);
1.90 paf 1010:
1.146 misha 1011: // ^file:find[file-name]
1012: // ^file:find[file-name]{when-not-found}
1.90 paf 1013: add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.47 parser 1014:
1.89 paf 1015: // ^file:dirname[/a/some.tar.gz]=/a
1016: // ^file:dirname[/a/b/]=/a
1017: add_native_method("dirname", Method::CT_STATIC, _dirname, 1, 1);
1018: // ^file:basename[/a/some.tar.gz]=some.tar.gz
1019: add_native_method("basename", Method::CT_STATIC, _basename, 1, 1);
1020: // ^file:justname[/a/some.tar.gz]=some.tar
1021: add_native_method("justname", Method::CT_STATIC, _justname, 1, 1);
1022: // ^file:justext[/a/some.tar.gz]=gz
1023: add_native_method("justext", Method::CT_STATIC, _justext, 1, 1);
1.102 paf 1024: // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
1025: add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1);
1.121 paf 1026:
1027: // ^file.sql-string[]
1028: add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.122 paf 1029:
1030: // ^file::sql[[alt_name]]{}
1031: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.139 paf 1032:
1.146 misha 1033: // ^file::base64[string] << decode
1.139 paf 1034: // ^file.base64[] << encode
1.151 misha 1035: // ^file:base64[file-name] << encode
1036: add_native_method("base64", Method::CT_ANY, _base64, 0, 1);
1.146 misha 1037:
1038: // ^file.crc32[]
1039: // ^file:crc32[file-name]
1040: add_native_method("crc32", Method::CT_ANY, _crc32, 0, 1);
1.147 misha 1041:
1042: // ^file.md5[]
1043: // ^file:md5[file-name]
1044: add_native_method("md5", Method::CT_ANY, _md5, 0, 1);
1045:
1.148 misha 1046: // ^file:copy[from-file-name;to-file-name]
1047: add_native_method("copy", Method::CT_STATIC, _copy, 2, 2);
1.1 paf 1048: }
E-mail: