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