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