Annotation of parser3/src/classes/file.C, revision 1.265
1.17 paf 1: /** @file
2: Parser: @b file parser class.
3:
1.262 moko 4: Copyright (c) 2001-2017 Art. Lebedev Studio (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.47 parser 8: #include "pa_config_includes.h"
9:
1.35 paf 10: #include "classes.h"
1.111 paf 11: #include "pa_vmethod_frame.h"
12:
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.40 parser 18: #include "pa_vdate.h"
1.47 parser 19: #include "pa_dir.h"
20: #include "pa_vtable.h"
1.67 paf 21: #include "pa_charset.h"
1.109 paf 22: #include "pa_charsets.h"
1.121 paf 23: #include "pa_sql_connection.h"
1.147 misha 24: #include "pa_md5.h"
1.184 misha 25: #include "pa_vregex.h"
1.208 misha 26: #include "pa_version.h"
1.1 paf 27:
1.265 ! moko 28: volatile const char * IDENT_FILE_C="$Id: file.C,v 1.264 2018/05/10 23:05:10 moko Exp $";
1.218 moko 29:
1.32 paf 30: // defines
31:
1.90 paf 32: #define STDIN_EXEC_PARAM_NAME "stdin"
1.109 paf 33: #define CHARSET_EXEC_PARAM_NAME "charset"
1.48 parser 34:
1.131 paf 35: #define NAME_NAME "name"
1.224 misha 36: #define KEEP_EMPTY_DIRS_NAME "keep-empty-dirs"
1.225 misha 37: #define SUPPRESS_EXCEPTION_NAME "exception"
1.131 paf 38:
1.132 paf 39: // externs
40:
41: extern String sql_limit_name;
42: extern String sql_offset_name;
43:
1.227 moko 44: // helpers
45:
46: class File_list_table_template_columns: public ArrayString {
47: public:
48: File_list_table_template_columns() {
49: *this+=new String("name");
50: *this+=new String("dir");
51: *this+=new String("size");
52: *this+=new String("cdate");
53: *this+=new String("mdate");
54: *this+=new String("adate");
55: }
56: };
57:
58: Table file_list_table_template(new File_list_table_template_columns);
59:
1.111 paf 60: // class
61:
62: class MFile: public Methoded {
63: public: // VStateless_class
1.199 misha 64: Value* create_new_value(Pool&) { return new VFile(); }
1.111 paf 65: public:
66: MFile();
67: };
68:
69: // global variable
70:
1.242 moko 71: DECLARE_CLASS_VAR(file, new MFile);
1.111 paf 72:
1.83 paf 73: // consts
74:
75: /// from apache-1.3|src|support|suexec.c
1.111 paf 76: static const char* suexec_safe_env_lst[]={
1.83 paf 77: "AUTH_TYPE",
78: "CONTENT_LENGTH",
79: "CONTENT_TYPE",
80: "DATE_GMT",
81: "DATE_LOCAL",
82: "DOCUMENT_NAME",
83: "DOCUMENT_PATH_INFO",
84: "DOCUMENT_ROOT",
85: "DOCUMENT_URI",
86: "FILEPATH_INFO",
87: "GATEWAY_INTERFACE",
88: "LAST_MODIFIED",
89: "PATH_INFO",
90: "PATH_TRANSLATED",
91: "QUERY_STRING",
92: "QUERY_STRING_UNESCAPED",
93: "REMOTE_ADDR",
94: "REMOTE_HOST",
95: "REMOTE_IDENT",
96: "REMOTE_PORT",
97: "REMOTE_USER",
98: "REDIRECT_QUERY_STRING",
99: "REDIRECT_STATUS",
100: "REDIRECT_URL",
101: "REQUEST_METHOD",
102: "REQUEST_URI",
103: "SCRIPT_FILENAME",
104: "SCRIPT_NAME",
105: "SCRIPT_URI",
106: "SCRIPT_URL",
107: "SERVER_ADMIN",
108: "SERVER_NAME",
109: "SERVER_ADDR",
110: "SERVER_PORT",
111: "SERVER_PROTOCOL",
112: "SERVER_SOFTWARE",
113: "UNIQUE_ID",
114: "USER_NAME",
115: "TZ",
116: NULL
117: };
118:
1.111 paf 119: // statics
1.33 paf 120:
1.258 moko 121: static const String::Body size_name("size");
1.112 paf 122: static const String::Body adate_name("adate");
123: static const String::Body mdate_name("mdate");
124: static const String::Body cdate_name("cdate");
1.32 paf 125:
1.1 paf 126: // methods
127:
1.111 paf 128: static void _save(Request& r, MethodParams& params) {
1.215 misha 129: bool is_text=VFile::is_text_mode(params.as_string(0, MODE_MUST_NOT_BE_CODE));
1.152 misha 130: Value& vfile_name=params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE);
1.4 paf 131:
1.201 misha 132: Charset* asked_charset=0;
133: if(params.count()>2)
1.214 misha 134: if(HashStringValue* options=params.as_hash(2)){
1.202 misha 135: int valid_options=0;
1.201 misha 136: if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
1.251 moko 137: asked_charset=&pa_charsets.get(vcharset_name->as_string());
1.201 misha 138: valid_options++;
139: }
140: if(valid_options != options->count())
1.207 misha 141: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.201 misha 142: }
143:
1.7 paf 144: // save
1.201 misha 145: GET_SELF(r, VFile).save(r.charsets, r.absolute(vfile_name.as_string()), is_text, asked_charset);
1.7 paf 146: }
147:
1.111 paf 148: static void _delete(Request& r, MethodParams& params) {
1.215 misha 149: const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
1.224 misha 150: bool keep_empty_dirs=false;
1.225 misha 151: bool fail_on_problem=true;
1.224 misha 152:
153: if(params.count()>1)
154: if(HashStringValue* options=params.as_hash(1)){
155: int valid_options=0;
156: if(Value* vkeep_empty_dirs=options->get(KEEP_EMPTY_DIRS_NAME)){
1.252 moko 157: keep_empty_dirs=r.process(*vkeep_empty_dirs).as_bool();
1.224 misha 158: valid_options++;
159: }
1.225 misha 160: if(Value* vsuppress_exception=options->get(SUPPRESS_EXCEPTION_NAME)){
1.252 moko 161: fail_on_problem=r.process(*vsuppress_exception).as_bool();
1.225 misha 162: valid_options++;
163: }
1.224 misha 164: if(valid_options != options->count())
165: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
166: }
1.7 paf 167:
168: // unlink
1.225 misha 169: file_delete(r.absolute(file_name), fail_on_problem, keep_empty_dirs);
1.1 paf 170: }
171:
1.111 paf 172: static void _move(Request& r, MethodParams& params) {
173: Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");
174: Value& vto_file_name=params.as_no_junction(1, "to file name must not be code");
1.224 misha 175: bool keep_empty_dirs=false;
176:
177: if(params.count()>2)
178: if(HashStringValue* options=params.as_hash(2)){
179: int valid_options=0;
180: if(Value* vkeep_empty_dirs=options->get(KEEP_EMPTY_DIRS_NAME)){
1.252 moko 181: keep_empty_dirs=r.process(*vkeep_empty_dirs).as_bool();
1.224 misha 182: valid_options++;
183: }
184: if(valid_options != options->count())
185: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
186: }
1.45 parser 187:
1.51 parser 188: // move
1.68 paf 189: file_move(
1.45 parser 190: r.absolute(vfrom_file_name.as_string()),
1.224 misha 191: r.absolute(vto_file_name.as_string()),
192: keep_empty_dirs);
1.45 parser 193: }
194:
1.260 moko 195: static void copy_process_source(struct stat&, int from_file, const String&, void *context) {
1.148 misha 196: int& to_file=*static_cast<int *>(context);
197:
198: int nCount=0;
199: do {
200: unsigned char buffer[FILE_BUFFER_SIZE];
1.150 misha 201: nCount = file_block_read(from_file, buffer, sizeof(buffer));
1.148 misha 202: int written=write(to_file, buffer, nCount);
203: if( written < 0 )
1.179 misha 204: throw Exception("file.access",
1.148 misha 205: 0,
206: "write failed: %s (%d)", strerror(errno), errno);
207:
208: } while(nCount > 0);
209: }
210:
211: static void copy_open_target(int f, void *from_spec) {
212: String& file_spec=*static_cast<String *>(from_spec);
213: file_read_action_under_lock(file_spec, "copy", copy_process_source, &f);
1.235 moko 214: }
1.148 misha 215:
216: static void _copy(Request& r, MethodParams& params) {
217: Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");
218: Value& vto_file_name=params.as_no_junction(1, "to file name must not be code");
219:
1.264 moko 220: bool append=false;
221: if(params.count()>2)
222: if(HashStringValue* options=params.as_hash(2)){
223: int valid_options=0;
224: if(Value* vappend=options->get("append")){
225: append=r.process(*vappend).as_bool();
226: valid_options++;
227: }
228: if(valid_options != options->count())
229: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
230: }
231:
1.148 misha 232: String from_spec = r.absolute(vfrom_file_name.as_string());
233: const String& to_spec = r.absolute(vto_file_name.as_string());
234:
235: file_write_action_under_lock(
236: to_spec,
237: "copy",
238: copy_open_target,
1.264 moko 239: &from_spec,
240: false /*as text*/,
241: append);
1.148 misha 242: }
243:
1.111 paf 244: static void _load_pass_param(
1.180 misha 245: HashStringValue::key_type key,
246: HashStringValue::value_type value,
247: HashStringValue *dest) {
1.111 paf 248: dest->put(key, value);
249: }
1.180 misha 250:
1.111 paf 251: static void _load(Request& r, MethodParams& params) {
1.215 misha 252: bool as_text=VFile::is_text_mode(params.as_string(0, MODE_MUST_NOT_BE_CODE));
253: const String& lfile_name=r.absolute(params.as_string(1, FILE_NAME_MUST_NOT_BE_CODE));
1.9 paf 254:
1.180 misha 255: size_t param_index=params.count()-1;
1.215 misha 256: Value* param_value=param_index>1?¶ms.as_no_junction(param_index, "file name or options must not be code"):0;
1.180 misha 257:
1.183 misha 258: HashStringValue* options=0;
1.215 misha 259: const String* user_file_name=0;
1.183 misha 260:
261: if(param_value){
262: options=param_value->get_hash();
263: if(options || param_index>2)
264: param_index--;
265: if(param_index>1){
266: const String& luser_file_name=params.as_string(param_index, FILE_NAME_MUST_BE_STRING);
267: if(!luser_file_name.is_empty())
1.215 misha 268: user_file_name=&luser_file_name;
1.183 misha 269: }
270: }
271: if(!user_file_name)
1.215 misha 272: user_file_name=&lfile_name;
1.180 misha 273:
1.132 paf 274: size_t offset=0;
275: size_t limit=0;
1.183 misha 276:
1.180 misha 277: if(options){
1.132 paf 278: options=new HashStringValue(*options);
1.180 misha 279: if(Value *voffset=(Value *)options->get(sql_offset_name)){
1.252 moko 280: offset=r.process(*voffset).as_int();
1.132 paf 281: }
1.180 misha 282: if(Value *vlimit=(Value *)options->get(sql_limit_name)){
1.252 moko 283: limit=r.process(*vlimit).as_int();
1.132 paf 284: }
285: // no check on options count here, see file_read
286: }
1.182 misha 287: File_read_result file=file_load(r, lfile_name,
1.180 misha 288: as_text, options, true, 0, offset, limit
1.104 paf 289: );
1.9 paf 290:
1.111 paf 291: Value* vcontent_type=0;
1.168 misha 292: if(file.headers){
1.181 misha 293: if(Value* remote_content_type=file.headers->get(HTTP_CONTENT_TYPE_UPPER))
1.129 paf 294: vcontent_type=new VString(*new String(remote_content_type->as_string().cstr()));
295: }
1.221 misha 296:
1.111 paf 297: VFile& self=GET_SELF(r, VFile);
1.221 misha 298: self.set(true/*tainted*/, as_text, file.str, file.length, user_file_name, vcontent_type, &r);
1.194 misha 299:
1.168 misha 300: if(file.headers){
1.143 paf 301: file.headers->for_each<HashStringValue*>(_load_pass_param, &self.fields());
1.168 misha 302: } else {
1.258 moko 303: uint64_t size;
1.168 misha 304: time_t atime, mtime, ctime;
305:
1.169 misha 306: file_stat(lfile_name, size, atime, mtime, ctime);
1.168 misha 307:
308: HashStringValue& ff=self.fields();
1.237 moko 309: ff.put(adate_name, new VDate((pa_time_t)atime));
310: ff.put(mdate_name, new VDate((pa_time_t)mtime));
311: ff.put(cdate_name, new VDate((pa_time_t)ctime));
1.168 misha 312: }
1.9 paf 313: }
314:
1.138 paf 315: static void _create(Request& r, MethodParams& params) {
1.215 misha 316: const String* mode=0;
317: const String* file_name=0;
318: bool is_text=true;
319:
320: // new format: ^file::create[string-or-file-content[;$.mode[text|binary] $.name[...] $.content-type[...] $.charset[...] ]]
321: size_t content_index=0;
322: size_t options_index=1;
323: bool extended_options=true;
324:
325: if(params.count()>=3){
326: // old format: ^file::create[text|binary;file-name;string-or-file-content[;options]]
327: mode=¶ms.as_string(0, MODE_MUST_NOT_BE_CODE);
328: is_text=VFile::is_text_mode(*mode);
329: file_name=¶ms.as_string(1, FILE_NAME_MUST_NOT_BE_CODE);
330: content_index=2;
331: options_index=3;
332: extended_options=false;
333: }
1.203 misha 334:
1.211 misha 335: VString* vcontent_type=0;
1.245 moko 336: Charset* to_charset=0;
337: Charset* from_charset=0;
1.215 misha 338: if(params.count()>options_index)
339: if(HashStringValue* options=params.as_hash(options_index)) {
1.203 misha 340: int valid_options=0;
1.215 misha 341: if(extended_options) {
342: if(Value* vmode=options->get(MODE_NAME)) {
343: mode=&vmode->as_string();
344: is_text=VFile::is_text_mode(*mode);
345: valid_options++;
346: }
347: if(Value* vfile_name=options->get(NAME_NAME)) {
348: file_name=&vfile_name->as_string();
349: valid_options++;
350: }
351: }
1.245 moko 352: if(Value* vcharset_name=options->get("to-charset")) {
1.251 moko 353: to_charset=&pa_charsets.get(vcharset_name->as_string());
1.245 moko 354: valid_options++;
355: }
356: if(Value* vcharset_name=options->get("from-charset")) {
1.251 moko 357: from_charset=&pa_charsets.get(vcharset_name->as_string());
1.245 moko 358: valid_options++;
359: }
1.215 misha 360: if(Value* vcharset_name=options->get(PA_CHARSET_NAME)) {
1.245 moko 361: if(to_charset)
362: throw Exception(PARSER_RUNTIME, 0, "charset option can not be used with to-charset");
1.251 moko 363: to_charset=&pa_charsets.get(vcharset_name->as_string());
1.203 misha 364: valid_options++;
365: }
1.211 misha 366: if(Value* value=options->get(CONTENT_TYPE_NAME)) {
367: vcontent_type=new VString(value->as_string());
368: valid_options++;
369: }
1.203 misha 370: if(valid_options != options->count())
1.207 misha 371: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.215 misha 372: }
1.211 misha 373:
1.215 misha 374: Value& vcontent=params.as_no_junction(content_index, "content must be string or file");
1.203 misha 375:
1.138 paf 376: VFile& self=GET_SELF(r, VFile);
1.194 misha 377:
1.215 misha 378: if(const String* content_str=vcontent.get_string()){
1.229 moko 379: String::Body body=content_str->cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets); // explode content, honor tainting changes
1.222 moko 380: self.set(true/*tainted*/, is_text, body.cstrm(), body.length(), file_name, vcontent_type, &r);
1.215 misha 381: } else {
1.245 moko 382: VFile& fcontent=*vcontent.as_vfile(String::L_AS_IS); // can't be null
1.248 moko 383: if(mode){
384: self.set(fcontent, &is_text, file_name, vcontent_type, &r);
385: if(is_text && !fcontent.is_text_mode())
386: from_charset=self.detect_binary_charset(from_charset);
387: } else {
388: self.set(fcontent, 0, file_name, vcontent_type, &r);
389: is_text=fcontent.is_text_mode();
390: }
1.215 misha 391: }
392:
1.245 moko 393: if(to_charset || from_charset)
394: if(is_text)
395: self.transcode(from_charset ? *from_charset : r.charsets.source(), to_charset ? *to_charset : r.charsets.source());
396: else
397: throw Exception(PARSER_RUNTIME, 0, "charset options can not be used with binary content");
1.138 paf 398: }
399:
1.111 paf 400: static void _stat(Request& r, MethodParams& params) {
1.215 misha 401: const String& lfile_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
1.25 paf 402:
1.258 moko 403: uint64_t size;
1.40 parser 404: time_t atime, mtime, ctime;
1.245 moko 405: file_stat(r.absolute(lfile_name), size, atime, mtime, ctime);
1.25 paf 406:
1.111 paf 407: VFile& self=GET_SELF(r, VFile);
1.167 misha 408:
1.258 moko 409: self.set_binary(true/*tainted*/, 0 /*no bytes*/, 0 /*fake size*/, &lfile_name, 0, &r);
1.111 paf 410: HashStringValue& ff=self.fields();
1.259 moko 411: ff.put(size_name, new VDouble((double)size) /*real size*/);
1.237 moko 412: ff.put(adate_name, new VDate((pa_time_t)atime));
413: ff.put(mdate_name, new VDate((pa_time_t)mtime));
414: ff.put(cdate_name, new VDate((pa_time_t)ctime));
1.25 paf 415: }
416:
1.111 paf 417: static bool is_safe_env_key(const char* key) {
418: for(const char* validator=key; *validator; validator++) {
419: char c=*validator;
1.234 moko 420: if(!( (c>='A' && c<='Z') || (c>='0' && c<='9') || (c=='_' || c=='-') ))
1.111 paf 421: return false;
422: }
1.205 pretende 423: #ifdef PA_SAFE_MODE
1.88 paf 424: if(strncasecmp(key, "HTTP_", 5)==0)
1.83 paf 425: return true;
1.87 paf 426: if(strncasecmp(key, "CGI_", 4)==0)
1.83 paf 427: return true;
428: for(int i=0; suexec_safe_env_lst[i]; i++) {
1.87 paf 429: if(strcasecmp(key, suexec_safe_env_lst[i])==0)
1.83 paf 430: return true;
431: }
432: return false;
1.205 pretende 433: #else
434: return true;
435: #endif
1.83 paf 436: }
1.90 paf 437: #ifndef DOXYGEN
438: struct Append_env_pair_info {
1.141 paf 439: Request_charsets* charsets;
1.111 paf 440: HashStringString* env;
1.100 paf 441: Value* vstdin;
1.90 paf 442: };
443: #endif
1.111 paf 444: static void append_env_pair(
1.180 misha 445: HashStringValue::key_type akey,
446: HashStringValue::value_type avalue,
447: Append_env_pair_info *info) {
1.111 paf 448: if(akey==STDIN_EXEC_PARAM_NAME) {
449: info->vstdin=avalue;
450: } else if(akey==CHARSET_EXEC_PARAM_NAME) {
1.141 paf 451: // ignore, already processed
1.90 paf 452: } else {
1.111 paf 453: if(!is_safe_env_key(akey.cstr()))
1.156 misha 454: throw Exception(PARSER_RUNTIME,
1.111 paf 455: new String(akey, String::L_TAINTED),
1.90 paf 456: "not safe environment variable");
1.196 misha 457: info->env->put(akey, avalue->as_string().cstr_to_string_body_untaint(String::L_AS_IS, 0, info->charsets));
1.90 paf 458: }
1.22 paf 459: }
1.94 paf 460: #ifndef DOXYGEN
461: struct Pass_cgi_header_attribute_info {
1.111 paf 462: Charset* charset;
463: HashStringValue* fields;
464: Value* content_type;
1.94 paf 465: };
466: #endif
1.111 paf 467: static void pass_cgi_header_attribute(
1.180 misha 468: ArrayString::element_type astring,
469: Pass_cgi_header_attribute_info* info) {
1.111 paf 470: size_t colon_pos=astring->pos(':');
1.130 paf 471: if(colon_pos!=STRING_NOT_FOUND) {
1.111 paf 472: const String& key=astring->mid(0, colon_pos).change_case(
473: *info->charset, String::CC_UPPER);
1.130 paf 474: Value* value=new VString(astring->mid(colon_pos+1, astring->length()).trim());
1.111 paf 475: info->fields->put(key, value);
1.181 misha 476: if(key==HTTP_CONTENT_TYPE_UPPER)
1.111 paf 477: info->content_type=value;
1.94 paf 478: }
1.29 paf 479: }
1.155 misha 480:
481: static void append_to_argv(Request& r, ArrayString& argv, const String* str){
1.187 misha 482: if(!str->is_empty())
1.229 moko 483: argv+=new String(str->cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets), String::L_AS_IS);
1.155 misha 484: }
485:
1.90 paf 486: /// @todo fix `` in perl - they produced flipping consoles and no output to perl
1.194 misha 487: static void _exec_cgi(Request& r, MethodParams& params, bool cgi) {
1.215 misha 488: bool is_text=true;
1.194 misha 489: size_t param_index=0;
1.215 misha 490: const String& mode=params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE);
491: if(VFile::is_valid_mode(mode)) {
492: is_text=VFile::is_text_mode(mode);
1.194 misha 493: param_index++;
1.162 misha 494: }
495:
496: if(param_index>=params.count())
1.215 misha 497: throw Exception(PARSER_RUNTIME, 0, FILE_NAME_MUST_BE_SPECIFIED);
1.162 misha 498:
1.215 misha 499: const String& script_name=r.absolute(params.as_string(param_index++, FILE_NAME_MUST_NOT_BE_CODE));
1.23 paf 500:
1.111 paf 501: HashStringString env;
1.239 moko 502: #define ECSTR(name, value_cstr) if(value_cstr) env.put(#name, value_cstr);
1.233 moko 503: // passing environment
504: for(SAPI::Env::Iterator i(r.sapi_info); i; i.next() )
1.239 moko 505: env.put(i.key(), i.value() );
1.82 paf 506:
1.23 paf 507: // const
1.63 paf 508: ECSTR(GATEWAY_INTERFACE, "CGI/1.1");
1.231 moko 509: ECSTR(PARSER_VERSION, PARSER_VERSION);
1.23 paf 510: // from Request.info
1.111 paf 511: ECSTR(DOCUMENT_ROOT, r.request_info.document_root);
512: ECSTR(PATH_TRANSLATED, r.request_info.path_translated);
513: ECSTR(REQUEST_METHOD, r.request_info.method);
514: ECSTR(QUERY_STRING, r.request_info.query_string);
515: ECSTR(REQUEST_URI, r.request_info.uri);
516: ECSTR(CONTENT_TYPE, r.request_info.content_type);
1.200 misha 517: ECSTR(CONTENT_LENGTH, format(r.request_info.content_length, "%u"));
1.82 paf 518: // SCRIPT_*
1.240 moko 519: env.put("SCRIPT_NAME", script_name);
1.23 paf 520:
1.90 paf 521: // environment & stdin from param
1.256 moko 522: bool in_is_text_mode=true;
523: String::C in;
1.109 paf 524: Charset *charset=0; // default script works raw_in 'source' charset = no transcoding needed
1.162 misha 525: if(param_index < params.count()) {
1.220 misha 526: if(HashStringValue* user_env=params.as_hash(param_index++, "env")) {
1.141 paf 527: // $.charset [previewing to handle URI pieces]
528: if(Value* vcharset=user_env->get(CHARSET_EXEC_PARAM_NAME))
1.251 moko 529: charset=&pa_charsets.get(vcharset->as_string());
1.141 paf 530:
531: // $.others
532: Append_env_pair_info info={&r.charsets, &env, 0};
533: {
1.144 paf 534: // influence tainting
535: // main target -- $.QUERY_STRING -- URLencoding of tainted pieces to String::L_URI lang
1.141 paf 536: Temp_client_charset temp(r.charsets, charset? *charset: r.charsets.source());
1.143 paf 537: user_env->for_each<Append_env_pair_info*>(append_env_pair, &info);
1.141 paf 538: }
1.109 paf 539: // $.stdin
1.103 paf 540: if(info.vstdin) {
1.111 paf 541: if(const String* sstdin=info.vstdin->get_string()) {
1.213 moko 542: // untaint stdin
1.256 moko 543: in = String::C(sstdin->cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets));
1.103 paf 544: } else
1.256 moko 545: if(VFile* vfile=static_cast<VFile *>(info.vstdin->as("file"))){
546: in = String::C((const char* )vfile->value_ptr(), vfile->value_size());
547: in_is_text_mode = vfile->is_text_mode();
548: } else
549: throw Exception(PARSER_RUNTIME, 0, STDIN_EXEC_PARAM_NAME " parameter must be string or file");
1.103 paf 550: }
1.90 paf 551: }
1.21 paf 552: }
553:
1.90 paf 554: // argv from params
1.111 paf 555: ArrayString argv;
1.162 misha 556: if(param_index < params.count()) {
1.180 misha 557: // influence tainting
558: Temp_client_charset temp(r.charsets, charset? *charset: r.charsets.source());
1.154 misha 559:
1.162 misha 560: for(size_t i=param_index; i<params.count(); i++) {
1.161 misha 561: Value& param=params.as_no_junction(i, PARAM_MUST_NOT_BE_CODE);
1.154 misha 562: if(param.is_defined()){
563: if(param.is_string()){
1.155 misha 564: append_to_argv(r, argv, param.get_string());
1.154 misha 565: } else {
1.155 misha 566: Table* table=param.get_table();
567: if(table){
1.250 moko 568: for(size_t j=0; j<table->count(); j++)
569: append_to_argv(r, argv, table->get(j)->get(0));
1.154 misha 570: } else {
1.250 moko 571: throw Exception(PARSER_RUNTIME, 0, "param must be string or table");
1.154 misha 572: }
573: }
1.145 misha 574: }
1.144 paf 575: }
1.21 paf 576: }
1.90 paf 577:
1.109 paf 578: // transcode if necessary
579: if(charset) {
1.111 paf 580: Charset::transcode(env, r.charsets.source(), *charset);
581: Charset::transcode(argv, r.charsets.source(), *charset);
1.256 moko 582: if(in_is_text_mode)
583: in=Charset::transcode(in, r.charsets.source(), *charset);
1.111 paf 584: }
585: // @todo
586: // ifdef WIN32 do OEM->ANSI transcode on some(.cmd?) programs to
587: // match silent conversion in OS
588:
589: // exec!
1.256 moko 590: PA_exec_result execution=pa_exec(false/*forced_allow*/, script_name, &env, argv, in);
1.111 paf 591:
1.162 misha 592: File_read_result *file_out=&execution.out;
1.111 paf 593: String *real_err=&execution.err;
1.162 misha 594:
1.165 misha 595: // transcode err if necessary (@todo: need fix line breaks in err as well )
596: if(charset)
597: real_err=&Charset::transcode(*real_err, *charset, r.charsets.source());
598:
1.215 misha 599: if(file_out->length && is_text){
1.162 misha 600: fix_line_breaks(file_out->str, file_out->length);
601: // treat output as string
1.188 misha 602: String *real_out = new String(file_out->str);
1.162 misha 603:
1.165 misha 604: // transcode out if necessary
605: if(charset)
1.162 misha 606: real_out=&Charset::transcode(*real_out, *charset, r.charsets.source());
1.165 misha 607:
1.162 misha 608: // FIXME: unsafe cast
1.163 misha 609: file_out->str=const_cast<char *>(real_out->cstr()); // hacking a little
1.162 misha 610: file_out->length = real_out->length();
1.109 paf 611: }
612:
1.111 paf 613: VFile& self=GET_SELF(r, VFile);
1.109 paf 614:
1.162 misha 615: if(cgi) { // ^file::cgi
1.163 misha 616: const char* eol_marker=0;
617: size_t eol_marker_size;
618:
1.111 paf 619: // construct with 'out' body and header
1.165 misha 620: size_t dos_pos=(file_out->length)?strpos(file_out->str, "\r\n\r\n"):STRING_NOT_FOUND;
621: size_t unix_pos=(file_out->length)?strpos(file_out->str, "\n\n"):STRING_NOT_FOUND;
1.111 paf 622:
623: bool unix_header_break;
624: switch((dos_pos!=STRING_NOT_FOUND?10:00) + (unix_pos!=STRING_NOT_FOUND?01:00)) {
1.166 misha 625: case 10: // dos
626: unix_header_break=false;
627: break;
628: case 01: // unix
629: unix_header_break=true;
630: break;
631: case 11: // dos & unix
632: unix_header_break=unix_pos<dos_pos;
633: break;
634: default: // 00
635: unix_header_break=false; // calm down, compiler
1.179 misha 636: throw Exception("file.execute",
1.166 misha 637: 0,
638: "output does not contain CGI header; "
639: "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"",
640: execution.status,
1.194 misha 641: file_out->length, (file_out->length) ? (file_out->str) : "",
642: real_err->length(), real_err->cstr());
1.166 misha 643: break; //never reached
1.111 paf 644: }
645:
1.165 misha 646: size_t header_break_pos;
1.111 paf 647: if(unix_header_break) {
648: header_break_pos=unix_pos;
1.165 misha 649: eol_marker="\n";
650: eol_marker_size=1;
1.111 paf 651: } else {
652: header_break_pos=dos_pos;
1.165 misha 653: eol_marker="\r\n";
654: eol_marker_size=2;
1.111 paf 655: }
1.21 paf 656:
1.162 misha 657: file_out->str[header_break_pos] = 0;
1.188 misha 658: String *header=new String(file_out->str);
1.162 misha 659: unsigned long headersize = header_break_pos+eol_marker_size*2;
660: file_out->str += headersize;
661: file_out->length -= headersize;
662:
1.164 misha 663: // $body
1.221 misha 664: self.set(false/*not tainted*/, is_text, file_out->str, file_out->length);
1.164 misha 665:
1.162 misha 666: // $fields << header
1.194 misha 667: if(header) {
1.162 misha 668: ArrayString rows;
1.249 moko 669: header->split(rows, 0, eol_marker);
1.162 misha 670: Pass_cgi_header_attribute_info info={0, 0, 0};
671: info.charset=&r.charsets.source();
672: info.fields=&self.fields();
673: rows.for_each(pass_cgi_header_attribute, &info);
674: if(info.content_type)
675: self.fields().put(content_type_name, info.content_type);
676: }
1.164 misha 677: } else { // ^file::exec
1.166 misha 678: // $body
1.257 moko 679: self.set(false/*not tainted*/, is_text, file_out->str ? file_out->str : pa_strdup("") /*to distinguish from stat-ed file*/, file_out->length);
1.164 misha 680: }
1.163 misha 681:
1.42 parser 682: // $status
1.111 paf 683: self.fields().put(file_status_name, new VInt(execution.status));
1.21 paf 684:
685: // $stderr
1.187 misha 686: if(!real_err->is_empty())
1.240 moko 687: self.fields().put("stderr", new VString(*real_err));
1.21 paf 688: }
1.111 paf 689: static void _exec(Request& r, MethodParams& params) {
690: _exec_cgi(r, params, false);
1.41 parser 691: }
1.111 paf 692: static void _cgi(Request& r, MethodParams& params) {
693: _exec_cgi(r, params, true);
1.41 parser 694: }
695:
1.111 paf 696: static void _list(Request& r, MethodParams& params) {
697: Value& relative_path=params.as_no_junction(0, "path must not be code");
1.47 parser 698:
1.227 moko 699: bool stat=false;
1.191 misha 700: VRegex* vregex=0;
1.184 misha 701: VRegexCleaner vrcleaner;
1.227 moko 702:
1.184 misha 703: if(params.count()>1){
1.227 moko 704: Value& voption=params.as_no_junction(1, "option must not be code");
705: if(voption.is_defined()) {
706: Value* vfilter=0;
707: if(HashStringValue* options=voption.get_hash()) {
708: int valid_options=0;
709: if(Value* vstat=options->get("stat")) {
1.252 moko 710: stat=r.process(*vstat).as_bool();
1.227 moko 711: valid_options++;
712: }
713: if(Value* value=options->get("filter")) {
714: vfilter=value;
1.230 moko 715: valid_options++;
1.227 moko 716: }
717: if(valid_options!=options->count())
718: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.191 misha 719: } else {
1.227 moko 720: vfilter=&voption;
721: }
1.234 moko 722: if(vfilter) {
1.227 moko 723: if(Value* value=vfilter->as(VREGEX_TYPE)) {
724: vregex=static_cast<VRegex*>(value);
725: } else if(vfilter->is_string()) {
726: if(!vfilter->get_string()->trim().is_empty()) {
727: vregex=new VRegex(r.charsets.source(), &vfilter->as_string(), 0/*options*/);
1.234 moko 728: vregex->study();
729: vrcleaner.vregex=vregex;
730: }
1.227 moko 731: } else {
732: throw Exception(PARSER_RUNTIME, 0, "filter must be regex or string");
733: }
1.234 moko 734: }
1.184 misha 735: }
1.114 paf 736: }
1.47 parser 737:
1.196 misha 738: const char* absolute_path_cstr=r.absolute(relative_path.as_string()).taint_cstr(String::L_FILE_SPEC);
1.47 parser 739:
1.227 moko 740: Table::Action_options table_options;
741: Table& table=*new Table(file_list_table_template, table_options);
1.47 parser 742:
1.184 misha 743: const int ovector_size=(1/*match*/)*3;
744: int ovector[ovector_size];
745:
1.47 parser 746: LOAD_DIR(absolute_path_cstr,
1.261 moko 747: const char* file_name_cstr=ffblk.name();
1.111 paf 748: size_t file_name_size=strlen(file_name_cstr);
1.47 parser 749:
1.261 moko 750: if(!vregex || vregex->exec(file_name_cstr, file_name_size, ovector, ovector_size)>=0) {
1.111 paf 751: Table::element_type row(new ArrayString);
1.190 misha 752: *row+=new String(pa_strdup(file_name_cstr, file_name_size), String::L_TAINTED);
1.236 moko 753: *row+=new String(String::Body::Format(ffblk.is_dir(stat) ? 1 : 0), String::L_CLEAN);
1.227 moko 754: if(stat) {
755: *row+=VDouble(ffblk.size()).get_string();
1.232 misha 756: *row+=new String(String::Body::Format((int)ffblk.c_timestamp()), String::L_CLEAN);
757: *row+=new String(String::Body::Format((int)ffblk.m_timestamp()), String::L_CLEAN);
758: *row+=new String(String::Body::Format((int)ffblk.a_timestamp()), String::L_CLEAN);
1.227 moko 759: }
1.111 paf 760: table+=row;
1.47 parser 761: }
762: );
763:
1.60 parser 764: // write out result
1.255 moko 765: r.write(*new VTable(&table));
1.47 parser 766: }
1.21 paf 767:
1.69 paf 768: #ifndef DOXYGEN
769: struct Lock_execute_body_info {
1.111 paf 770: Request* r;
771: Value* body_code;
1.69 paf 772: };
773: #endif
1.235 moko 774:
1.111 paf 775: static void lock_execute_body(int , void *ainfo) {
776: Lock_execute_body_info& info=*static_cast<Lock_execute_body_info *>(ainfo);
1.69 paf 777: // execute body
1.255 moko 778: info.r->write(info.r->process(*info.body_code));
1.235 moko 779: }
780:
1.111 paf 781: static void _lock(Request& r, MethodParams& params) {
1.152 misha 782: const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));
1.116 paf 783: Lock_execute_body_info info={
784: &r,
1.117 paf 785: ¶ms.as_junction(1, "body must be code")
1.116 paf 786: };
1.69 paf 787:
1.158 misha 788: file_write_action_under_lock(
789: file_spec,
790: "lock",
791: lock_execute_body,
792: &info);
1.69 paf 793: }
794:
1.219 misha 795: static size_t afterlastslash(const String& str) {
796: size_t pos=str.strrpbrk("/\\");
797: return pos!=STRING_NOT_FOUND?pos+1:0;
798: }
799:
800: static size_t afterlastslash(const String& str, size_t right) {
801: size_t pos=str.strrpbrk("/\\", 0, right);
802: return pos!=STRING_NOT_FOUND?pos+1:0;
803: }
804:
1.111 paf 805: static void _find(Request& r, MethodParams& params) {
1.215 misha 806: const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
1.219 misha 807:
1.215 misha 808: Value* not_found_code=(params.count()==2)?¶ms.as_junction(1, "not-found param must be code"):0;
809:
1.111 paf 810: const String* file_spec;
1.90 paf 811: if(file_name.first_char()=='/')
812: file_spec=&file_name;
813: else
1.111 paf 814: file_spec=&r.relative(r.request_info.uri, file_name);
1.90 paf 815:
816: // easy way
1.142 paf 817: if(file_exist(r.absolute(*file_spec))) {
1.255 moko 818: r.write(*file_spec);
1.90 paf 819: return;
820: }
821:
822: // monkey way
1.219 misha 823: size_t last_slash=file_spec->strrpbrk("/\\");
824: const String& dirname=file_spec->mid(0, last_slash!=STRING_NOT_FOUND?last_slash:0);
825: const String& basename=file_spec->mid(last_slash!=STRING_NOT_FOUND?last_slash+1:0, file_spec->length());
826:
827: size_t rpos=dirname.is_empty()?0:dirname.length()-1;
828: while((rpos=dirname.rskipchars("/\\", 0, rpos))!=STRING_NOT_FOUND){
829: size_t slash=dirname.strrpbrk("/\\", 0, rpos);
830: if(slash==STRING_NOT_FOUND)
831: break;
1.111 paf 832: String test_name;
1.219 misha 833: test_name << dirname.mid(0, slash+1);
834: test_name << basename;
1.142 paf 835: if(file_exist(r.absolute(test_name))) {
1.255 moko 836: r.write(test_name);
1.90 paf 837: return;
838: }
1.219 misha 839: rpos=slash;
1.90 paf 840: }
841:
842: // no way, not found
1.215 misha 843: if(not_found_code)
1.255 moko 844: r.write(r.process(*not_found_code));
1.90 paf 845: }
846:
1.111 paf 847: static void _dirname(Request& r, MethodParams& params) {
1.152 misha 848: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.219 misha 849: // works as *nix dirname
850:
851: // empty > .
852: // / > /
853: // /a > /
854: // /a/ > /
1.180 misha 855: // /a/some.tar.gz > /a
1.219 misha 856: // /a/b/ > /a
857: // /a///b/ > /a
858: // /a/b/// > /a
859: // file > .
860:
861: if(file_spec.is_empty()) {
1.255 moko 862: r.write(String("."));
1.219 misha 863: return;
864: }
865:
866: size_t p;
867: size_t slash;
868: if((p=file_spec.rskipchars("/\\"))==STRING_NOT_FOUND)
1.255 moko 869: r.write(String("/"));
1.219 misha 870: else {
871: if((slash=file_spec.strrpbrk("/\\", 0, p))!=STRING_NOT_FOUND) {
872: if((p=file_spec.rskipchars("/\\", 0, slash))==STRING_NOT_FOUND)
873: p=slash;
1.255 moko 874: r.write(file_spec.mid(0, p+1));
1.219 misha 875: return;
876: }
1.255 moko 877: r.write(String("."));
1.219 misha 878: }
1.89 paf 879: }
880:
1.111 paf 881: static void _basename(Request& r, MethodParams& params) {
1.152 misha 882: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.219 misha 883: // works as *nix basename
884:
885: // empty > .
886: // / > /
887: // /a > a
888: // /a/ > a
1.180 misha 889: // /a/some.tar.gz > some.tar.gz
1.219 misha 890: // /a/b/ > b
891: // /a///b/ > b
892: // /a/b/// > b
893: // file > file
894:
895: if(file_spec.is_empty()) {
1.255 moko 896: r.write(String("."));
1.219 misha 897: return;
898: }
899:
900: size_t p=file_spec.rskipchars("/\\");
901: if(p==STRING_NOT_FOUND)
1.255 moko 902: r.write(String("/"));
1.219 misha 903: else
1.255 moko 904: r.write(file_spec.mid(afterlastslash(file_spec, p), p+1));
1.89 paf 905: }
906:
1.111 paf 907: static void _justname(Request& r, MethodParams& params) {
1.152 misha 908: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180 misha 909: // /a/some.tar.gz > some.tar
1.219 misha 910: // /a/b.c/ > empty
911: // /a/b.c > b
912: size_t pos=afterlastslash(file_spec);
913: size_t dotpos=file_spec.strrpbrk(".", pos);
1.255 moko 914: r.write(file_spec.mid(pos, dotpos!=STRING_NOT_FOUND?dotpos:file_spec.length()));
1.89 paf 915: }
1.219 misha 916:
1.111 paf 917: static void _justext(Request& r, MethodParams& params) {
1.152 misha 918: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180 misha 919: // /a/some.tar.gz > gz
1.219 misha 920: // /a/b.c/ > empty
921: size_t pos=afterlastslash(file_spec);
922: size_t dotpos=file_spec.strrpbrk(".", pos);
923: if(dotpos!=STRING_NOT_FOUND)
1.255 moko 924: r.write(file_spec.mid(dotpos+1, file_spec.length()));
1.89 paf 925: }
926:
1.111 paf 927: static void _fullpath(Request& r, MethodParams& params) {
1.152 misha 928: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.111 paf 929: const String* result;
1.102 paf 930: if(file_spec.first_char()=='/')
931: result=&file_spec;
932: else {
933: // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
934: const String& full_disk_path=r.absolute(file_spec);
1.111 paf 935: size_t document_root_length=strlen(r.request_info.document_root);
1.106 paf 936:
937: if(document_root_length>0) {
1.111 paf 938: char last_char=r.request_info.document_root[document_root_length-1];
1.106 paf 939: if(last_char == '/' || last_char == '\\')
940: --document_root_length;
941: }
1.111 paf 942: result=&full_disk_path.mid(document_root_length, full_disk_path.length());
1.102 paf 943: }
1.255 moko 944: r.write(*result);
1.102 paf 945: }
946:
1.121 paf 947: static void _sql_string(Request& r, MethodParams&) {
948: VFile& self=GET_SELF(r, VFile);
949:
950: const char *quoted=r.connection()->quote(self.value_ptr(), self.value_size());
1.255 moko 951: r.write(*new String(quoted));
1.121 paf 952: }
1.89 paf 953:
1.122 paf 954: #ifndef DOXYGEN
955: class File_sql_event_handlers: public SQL_Driver_query_event_handlers {
956: int got_columns;
957: int got_cells;
958: public:
959: String::C value;
1.131 paf 960: const String* user_file_name;
961: const String* user_content_type;
1.122 paf 962: public:
1.265 ! moko 963: File_sql_event_handlers():
1.122 paf 964: got_columns(0),
965: got_cells(0),
966: user_file_name(0),
967: user_content_type(0) {}
968:
969: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
970: if(got_columns++==3) {
1.265 ! moko 971: error=SQL_Error("result must contain not more then 3 columns");
1.122 paf 972: return true;
973: }
974: return false;
975: }
976: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
977: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
978: bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
979: try {
980: switch(got_cells++) {
981: case 0:
982: value=String::C(str, length);
983: break;
984: case 1:
1.131 paf 985: if(!user_file_name) // user not specified?
1.190 misha 986: user_file_name=new String(str, String::L_TAINTED);
1.122 paf 987: break;
988: case 2:
1.131 paf 989: if(!user_content_type) // user not specified?
1.190 misha 990: user_content_type=new String(str, String::L_TAINTED);
1.122 paf 991: break;
992: default:
1.265 ! moko 993: error=SQL_Error("result must not contain more then one row, three columns");
1.122 paf 994: return true;
995: }
996: return false;
997: } catch(...) {
1.263 moko 998: error=SQL_Error("exception occurred in File_sql_event_handlers::add_row_cell");
1.122 paf 999: return true;
1000: }
1001: }
1002: };
1003: #endif
1004: static void _sql(Request& r, MethodParams& params) {
1.131 paf 1005: Value& statement=params.as_junction(0, "statement must be code");
1.122 paf 1006:
1007: const String& statement_string=r.process_to_string(statement);
1.254 moko 1008: const char* statement_cstr=statement_string.untaint_cstr(String::L_SQL, r.connection());
1.195 misha 1009:
1.265 ! moko 1010: File_sql_event_handlers handlers;
1.131 paf 1011:
1.173 misha 1012: ulong limit=SQL_NO_LIMIT;
1.172 misha 1013: ulong offset=0;
1014:
1.131 paf 1015: if(params.count()>1)
1.220 misha 1016: if(HashStringValue* options=params.as_hash(1, "sql options")) {
1.131 paf 1017: int valid_options=0;
1018: if(Value* vfilename=options->get(NAME_NAME)) {
1019: valid_options++;
1020: handlers.user_file_name=&vfilename->as_string();
1021: }
1022: if(Value* vcontent_type=options->get(CONTENT_TYPE_NAME)) {
1023: valid_options++;
1024: handlers.user_content_type=&vcontent_type->as_string();
1025: }
1.173 misha 1026: if(Value* vlimit=options->get(sql_limit_name)) {
1027: valid_options++;
1.252 moko 1028: limit=(ulong)r.process(*vlimit).as_double();
1.173 misha 1029: }
1.172 misha 1030: if(Value* voffset=options->get(sql_offset_name)) {
1031: valid_options++;
1.252 moko 1032: offset=(ulong)r.process(*voffset).as_double();
1.172 misha 1033: }
1.131 paf 1034: if(valid_options!=options->count())
1.207 misha 1035: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.131 paf 1036: }
1037:
1038:
1.265 ! moko 1039: r.connection()->query(statement_cstr, 0, 0, offset, limit, handlers, statement_string);
1.122 paf 1040:
1.238 moko 1041: if(!handlers.value.str)
1042: throw Exception(PARSER_RUNTIME, 0, "produced no result");
1.122 paf 1043:
1.215 misha 1044: VFile& self=GET_SELF(r, VFile);
1.122 paf 1045:
1.222 moko 1046: self.set_binary(true/*tainted*/, handlers.value.str, handlers.value.length, handlers.user_file_name
1.215 misha 1047: , handlers.user_content_type ? new VString(*handlers.user_content_type) : 0
1048: , &r);
1.122 paf 1049: }
1.140 paf 1050:
1.139 paf 1051: static void _base64(Request& r, MethodParams& params) {
1.209 misha 1052: bool dynamic=!(&r.get_self() == file_class);
1053: if(dynamic) {
1.180 misha 1054: VFile& self=GET_SELF(r, VFile);
1055: if(params.count()) {
1.209 misha 1056: // decode:
1057: // ^file::base64[encoded] // backward
1.217 misha 1058: // ^file::base64[mode;user-file-name;encoded[;$.content-type[...] $.strict(true|false)]]
1.209 misha 1059: bool is_text=false;
1.217 misha 1060: bool strict=false;
1.209 misha 1061: VString* vcontent_type=0;
1.215 misha 1062: const String* user_file_name=0;
1.209 misha 1063: size_t param_index=0;
1064:
1065: if(params.count() > 1) {
1066: if(params.count() < 3)
1067: throw Exception(PARSER_RUNTIME,
1068: 0,
1.215 misha 1069: "constructor can not have less then 3 parameters (has %d parameters)",
1.209 misha 1070: params.count()); // actually it accepts 1 parameter (backward)
1071:
1.215 misha 1072: is_text=VFile::is_text_mode(params.as_string(0, MODE_MUST_NOT_BE_CODE));
1073: user_file_name=¶ms.as_string(1, FILE_NAME_MUST_BE_STRING);
1.209 misha 1074:
1075: if(params.count() == 4)
1076: if(HashStringValue* options=params.as_hash(3)) {
1077: int valid_options=0;
1078: if(Value* value=options->get(CONTENT_TYPE_NAME)) {
1079: vcontent_type=new VString(value->as_string());
1080: valid_options++;
1081: }
1.217 misha 1082: if(Value* vstrict=options->get(BASE64_STRICT_OPTION_NAME)) {
1.252 moko 1083: strict=r.process(*vstrict).as_bool();
1.217 misha 1084: valid_options++;
1085: }
1.209 misha 1086: if(valid_options!=options->count())
1087: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1088: }
1089:
1090: param_index=2;
1091: }
1092:
1093: const char* encoded=params.as_string(param_index, PARAMETER_MUST_BE_STRING).cstr();
1094:
1.180 misha 1095: char* decoded=0;
1096: size_t length=0;
1.217 misha 1097: pa_base64_decode(encoded, strlen(encoded), decoded, length, strict);
1.209 misha 1098:
1.221 misha 1099: self.set(true/*tainted*/, is_text, decoded, length, user_file_name, vcontent_type, &r);
1.180 misha 1100: } else {
1101: // encode: ^f.base64[]
1102: const char* encoded=pa_base64_encode(self.value_ptr(), self.value_size());
1.255 moko 1103: r.write(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed**/));
1.180 misha 1104: }
1.151 misha 1105: } else {
1.180 misha 1106: // encode: ^file:base64[filespec]
1.152 misha 1107: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.151 misha 1108: const char* encoded=pa_base64_encode(r.absolute(file_spec));
1.255 moko 1109: r.write(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed*/));
1.151 misha 1110: }
1.139 paf 1111: }
1.140 paf 1112:
1.146 misha 1113: static void _crc32(Request& r, MethodParams& params) {
1114: unsigned long crc32 = 0;
1115: if(&r.get_self() == file_class) {
1116: // ^file:crc32[file-name]
1117: if(params.count()) {
1.152 misha 1118: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.146 misha 1119: crc32=pa_crc32(r.absolute(file_spec));
1120: } else {
1.215 misha 1121: throw Exception(PARSER_RUNTIME, 0, FILE_NAME_MUST_BE_SPECIFIED);
1.146 misha 1122: }
1123: } else {
1124: // ^file.crc32[]
1125: VFile& self=GET_SELF(r, VFile);
1126: crc32=pa_crc32(self.value_ptr(), self.value_size());
1127: }
1.255 moko 1128: r.write(*new VInt(crc32));
1.146 misha 1129: }
1130:
1131:
1.243 moko 1132: static void file_md5_file_action(struct stat& finfo, int f, const String&, void *context)
1.147 misha 1133: {
1134: PA_MD5_CTX& md5context=*static_cast<PA_MD5_CTX *>(context);
1135: if(finfo.st_size) {
1.148 misha 1136: int nCount=0;
1.147 misha 1137: do {
1138: unsigned char buffer[FILE_BUFFER_SIZE];
1.150 misha 1139: nCount = file_block_read(f, buffer, sizeof(buffer));
1.147 misha 1140: if ( nCount ){
1141: pa_MD5Update(&md5context, (const unsigned char*)buffer, nCount);
1142: }
1.148 misha 1143: } while(nCount > 0);
1.147 misha 1144: }
1145: }
1146:
1147: const char* pa_md5(const String& file_spec)
1148: {
1149: PA_MD5_CTX context;
1150: unsigned char digest[16];
1151: pa_MD5Init(&context);
1152: file_read_action_under_lock(file_spec, "md5", file_md5_file_action, &context);
1153: pa_MD5Final(digest, &context);
1154:
1155: return hex_string(digest, sizeof(digest), false);
1156: }
1157:
1158: const char* pa_md5(const char *in, size_t in_size)
1159: {
1160: PA_MD5_CTX context;
1161: unsigned char digest[16];
1162: pa_MD5Init(&context);
1163: pa_MD5Update(&context, (const unsigned char*)in, in_size);
1164: pa_MD5Final(digest, &context);
1165:
1166: return hex_string(digest, sizeof(digest), false);
1167: }
1168:
1169: static void _md5(Request& r, MethodParams& params) {
1170: const char* md5;
1171: if(&r.get_self() == file_class) {
1172: // ^file:md5[file-name]
1173: if(params.count()) {
1.152 misha 1174: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.147 misha 1175: md5=pa_md5(r.absolute(file_spec));
1176: } else {
1.215 misha 1177: throw Exception(PARSER_RUNTIME, 0, FILE_NAME_MUST_BE_SPECIFIED);
1.147 misha 1178: }
1179: } else {
1180: // ^file.md5[]
1181: VFile& self=GET_SELF(r, VFile);
1182: md5=pa_md5(self.value_ptr(), self.value_size());
1183:
1184: }
1.255 moko 1185: r.write(*new String(md5));
1.147 misha 1186: }
1187:
1.32 paf 1188: // constructor
1189:
1.111 paf 1190: MFile::MFile(): Methoded("file") {
1.215 misha 1191: // ^file::create[text|binary;file-name;string-or-file[;options hash]]
1192: // ^file::create[string-or-file[;options hash]]
1193: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 4);
1.138 paf 1194:
1.146 misha 1195: // ^file.save[mode;file-name]
1.201 misha 1196: // ^file.save[mode;file-name;$.charset[...]]
1197: add_native_method("save", Method::CT_DYNAMIC, _save, 2, 3);
1.7 paf 1198:
1.146 misha 1199: // ^file:delete[file-name]
1.225 misha 1200: // ^file:delete[file-name;$.keep-empty-dir(true)$.exception(false)]
1.224 misha 1201: add_native_method("delete", Method::CT_STATIC, _delete, 1, 2);
1.45 parser 1202:
1.146 misha 1203: // ^file:move[from-file-name;to-file-name]
1.224 misha 1204: // ^file:move[from-file-name;to-file-name;$.keep-empty-dir(true)]
1205: add_native_method("move", Method::CT_STATIC, _move, 2, 3);
1.8 paf 1206:
1.146 misha 1207: // ^file::load[mode;disk-name]
1208: // ^file::load[mode;disk-name;user-name]
1.201 misha 1209: // ^file::load[mode;disk-name;user-name;options hash]
1210: // ^file::load[mode;disk-name;options hash]
1.180 misha 1211: add_native_method("load", Method::CT_DYNAMIC, _load, 2, 4);
1.25 paf 1212:
1.146 misha 1213: // ^file::stat[disk-name]
1.32 paf 1214: add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21 paf 1215:
1.162 misha 1216: // ^file::cgi[mode;file-name]
1217: // ^file::cgi[mode;file-name;env hash]
1218: // ^file::cgi[mode;file-name;env hash;1cmd;2line;3ar;4g;5s]
1219: add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 3+50);
1220:
1221: // ^file::exec[mode;file-name]
1222: // ^file::exec[mode;file-name;env hash]
1223: // ^file::exec[mode;file-name;env hash;1cmd;2line;3ar;4g;5s]
1224: add_native_method("exec", Method::CT_DYNAMIC, _exec, 1, 3+50);
1.47 parser 1225:
1226: // ^file:list[path]
1227: // ^file:list[path][regexp]
1.228 moko 1228: // ^file:list[path][$.filter[regexp] $.stat(true)]
1.47 parser 1229: add_native_method("list", Method::CT_STATIC, _list, 1, 2);
1.69 paf 1230:
1231: // ^file:lock[path]{code}
1232: add_native_method("lock", Method::CT_STATIC, _lock, 2, 2);
1.90 paf 1233:
1.146 misha 1234: // ^file:find[file-name]
1235: // ^file:find[file-name]{when-not-found}
1.90 paf 1236: add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.47 parser 1237:
1.201 misha 1238: // ^file:dirname[/a/some.tar.gz]=/a
1.89 paf 1239: // ^file:dirname[/a/b/]=/a
1240: add_native_method("dirname", Method::CT_STATIC, _dirname, 1, 1);
1.201 misha 1241: // ^file:basename[/a/some.tar.gz]=some.tar.gz
1242: add_native_method("basename", Method::CT_STATIC, _basename, 1, 1);
1243: // ^file:justname[/a/some.tar.gz]=some.tar
1.89 paf 1244: add_native_method("justname", Method::CT_STATIC, _justname, 1, 1);
1.201 misha 1245: // ^file:justext[/a/some.tar.gz]=gz
1.89 paf 1246: add_native_method("justext", Method::CT_STATIC, _justext, 1, 1);
1.201 misha 1247: // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
1.102 paf 1248: add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1);
1.121 paf 1249:
1.201 misha 1250: // ^file.sql-string[]
1.121 paf 1251: add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.122 paf 1252:
1.209 misha 1253: // ^file::sql{}
1.201 misha 1254: // ^file::sql{}[options hash]
1.122 paf 1255: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.139 paf 1256:
1.209 misha 1257: // encode:
1258: // ^file.base64[]
1259: // ^file:base64[file-name]
1260: // decode:
1261: // ^file::base64[encoded] // backward
1262: // ^file::base64[mode;user-file-name;encoded]
1263: // ^file::base64[mode;user-file-name;encoded;$.content-type[...]]
1264: add_native_method("base64", Method::CT_ANY, _base64, 0, 4);
1.146 misha 1265:
1266: // ^file.crc32[]
1267: // ^file:crc32[file-name]
1268: add_native_method("crc32", Method::CT_ANY, _crc32, 0, 1);
1.147 misha 1269:
1270: // ^file.md5[]
1271: // ^file:md5[file-name]
1272: add_native_method("md5", Method::CT_ANY, _md5, 0, 1);
1273:
1.148 misha 1274: // ^file:copy[from-file-name;to-file-name]
1.264 moko 1275: // ^file:copy[from-file-name;to-file-name;$.append(false)]
1276: add_native_method("copy", Method::CT_STATIC, _copy, 2, 3);
1.1 paf 1277: }
E-mail: