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