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