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