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