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