Annotation of parser3/src/classes/file.C, revision 1.217
1.17 paf 1: /** @file
2: Parser: @b file parser class.
3:
1.217 ! misha 4: Copyright (c) 2001-2012 ArtLebedev Group (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.217 ! misha 8: static const char * const IDENT_FILE_C="$Date: 2012-02-27 11:08:28 $";
1.47 parser 9:
10: #include "pa_config_includes.h"
11:
1.35 paf 12: #include "classes.h"
1.111 paf 13: #include "pa_vmethod_frame.h"
14:
1.1 paf 15: #include "pa_request.h"
16: #include "pa_vfile.h"
1.11 paf 17: #include "pa_table.h"
1.21 paf 18: #include "pa_vint.h"
1.24 paf 19: #include "pa_exec.h"
1.40 parser 20: #include "pa_vdate.h"
1.47 parser 21: #include "pa_dir.h"
22: #include "pa_vtable.h"
1.67 paf 23: #include "pa_charset.h"
1.109 paf 24: #include "pa_charsets.h"
1.121 paf 25: #include "pa_sql_connection.h"
1.147 misha 26: #include "pa_md5.h"
1.184 misha 27: #include "pa_vregex.h"
1.208 misha 28: #include "pa_version.h"
1.1 paf 29:
1.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.10 paf 237:
1.111 paf 238: VFile& self=GET_SELF(r, VFile);
1.215 misha 239: self.set(true/*tainted*/, file.str, file.length, user_file_name, vcontent_type, &r);
1.168 misha 240:
1.194 misha 241: self.set_mode(as_text);
242:
1.168 misha 243: if(file.headers){
1.143 paf 244: file.headers->for_each<HashStringValue*>(_load_pass_param, &self.fields());
1.168 misha 245: } else {
246: size_t size;
247: time_t atime, mtime, ctime;
248:
1.169 misha 249: file_stat(lfile_name, size, atime, mtime, ctime);
1.168 misha 250:
251: HashStringValue& ff=self.fields();
252: ff.put(adate_name, new VDate(atime));
253: ff.put(mdate_name, new VDate(mtime));
254: ff.put(cdate_name, new VDate(ctime));
255: }
1.9 paf 256: }
257:
1.138 paf 258: static void _create(Request& r, MethodParams& params) {
1.215 misha 259: const String* mode=0;
260: const String* file_name=0;
261: bool is_text=true;
262:
263: // new format: ^file::create[string-or-file-content[;$.mode[text|binary] $.name[...] $.content-type[...] $.charset[...] ]]
264: size_t content_index=0;
265: size_t options_index=1;
266: bool extended_options=true;
267:
268: if(params.count()>=3){
269: // old format: ^file::create[text|binary;file-name;string-or-file-content[;options]]
270: mode=¶ms.as_string(0, MODE_MUST_NOT_BE_CODE);
271: is_text=VFile::is_text_mode(*mode);
272: file_name=¶ms.as_string(1, FILE_NAME_MUST_NOT_BE_CODE);
273: content_index=2;
274: options_index=3;
275: extended_options=false;
276: }
1.203 misha 277:
1.211 misha 278: VString* vcontent_type=0;
1.215 misha 279: Charset* asked_charset=0;
280: if(params.count()>options_index)
281: if(HashStringValue* options=params.as_hash(options_index)) {
1.203 misha 282: int valid_options=0;
1.215 misha 283: if(extended_options) {
284: if(Value* vmode=options->get(MODE_NAME)) {
285: mode=&vmode->as_string();
286: is_text=VFile::is_text_mode(*mode);
287: valid_options++;
288: }
289: if(Value* vfile_name=options->get(NAME_NAME)) {
290: file_name=&vfile_name->as_string();
291: valid_options++;
292: }
293: }
294: if(Value* vcharset_name=options->get(PA_CHARSET_NAME)) {
1.203 misha 295: asked_charset=&::charsets.get(vcharset_name->as_string().change_case(r.charsets.source(), String::CC_UPPER));
296: valid_options++;
297: }
1.211 misha 298: if(Value* value=options->get(CONTENT_TYPE_NAME)) {
299: vcontent_type=new VString(value->as_string());
300: valid_options++;
301: }
1.203 misha 302: if(valid_options != options->count())
1.207 misha 303: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.215 misha 304: }
1.211 misha 305:
1.215 misha 306: Value& vcontent=params.as_no_junction(content_index, "content must be string or file");
1.203 misha 307:
1.138 paf 308: VFile& self=GET_SELF(r, VFile);
1.194 misha 309:
1.215 misha 310: if(const String* content_str=vcontent.get_string()){
311: String::Body body=content_str->cstr_to_string_body_untaint(String::L_AS_IS); // explode content, honor tainting changes
312: if(asked_charset && is_text)
313: body=Charset::transcode(body, r.charsets.source(), *asked_charset);
314: self.set(true/*tainted*/, body.cstr(), body.length());
315: self.set_mode(is_text);
316: } else {
317: if(asked_charset)
318: throw Exception(PARSER_RUNTIME, 0, "charset option can not be used with file-content");
319: self.set(*vcontent.as_vfile(String::L_AS_IS));
320: if(mode)
321: self.set_mode(is_text);
322: }
323:
324: self.set_name(file_name);
325:
326: self.set_content_type(vcontent_type, file_name, &r);
1.138 paf 327: }
328:
1.111 paf 329: static void _stat(Request& r, MethodParams& params) {
1.215 misha 330: const String& lfile_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
1.25 paf 331:
1.40 parser 332: size_t size;
333: time_t atime, mtime, ctime;
334: file_stat(r.absolute(lfile_name),
335: size,
336: atime, mtime, ctime);
1.25 paf 337:
1.111 paf 338: VFile& self=GET_SELF(r, VFile);
1.167 misha 339:
1.215 misha 340: self.set(true/*tainted*/, 0/*no bytes*/, size, &lfile_name, 0, &r);
1.111 paf 341: HashStringValue& ff=self.fields();
342: ff.put(adate_name, new VDate(atime));
343: ff.put(mdate_name, new VDate(mtime));
344: ff.put(cdate_name, new VDate(ctime));
1.25 paf 345: }
346:
1.111 paf 347: static bool is_safe_env_key(const char* key) {
348: for(const char* validator=key; *validator; validator++) {
349: char c=*validator;
350: if(!(c>='A' && c<='Z' || c>='0' && c<='9' || c=='_' || c=='-'))
351: return false;
352: }
1.205 pretende 353: #ifdef PA_SAFE_MODE
1.88 paf 354: if(strncasecmp(key, "HTTP_", 5)==0)
1.83 paf 355: return true;
1.87 paf 356: if(strncasecmp(key, "CGI_", 4)==0)
1.83 paf 357: return true;
358: for(int i=0; suexec_safe_env_lst[i]; i++) {
1.87 paf 359: if(strcasecmp(key, suexec_safe_env_lst[i])==0)
1.83 paf 360: return true;
361: }
362: return false;
1.205 pretende 363: #else
364: return true;
365: #endif
1.83 paf 366: }
1.90 paf 367: #ifndef DOXYGEN
368: struct Append_env_pair_info {
1.141 paf 369: Request_charsets* charsets;
1.111 paf 370: HashStringString* env;
1.100 paf 371: Value* vstdin;
1.90 paf 372: };
373: #endif
1.111 paf 374: static void append_env_pair(
1.180 misha 375: HashStringValue::key_type akey,
376: HashStringValue::value_type avalue,
377: Append_env_pair_info *info) {
1.111 paf 378: if(akey==STDIN_EXEC_PARAM_NAME) {
379: info->vstdin=avalue;
380: } else if(akey==CHARSET_EXEC_PARAM_NAME) {
1.141 paf 381: // ignore, already processed
1.90 paf 382: } else {
1.111 paf 383: if(!is_safe_env_key(akey.cstr()))
1.156 misha 384: throw Exception(PARSER_RUNTIME,
1.111 paf 385: new String(akey, String::L_TAINTED),
1.90 paf 386: "not safe environment variable");
1.196 misha 387: info->env->put(akey, avalue->as_string().cstr_to_string_body_untaint(String::L_AS_IS, 0, info->charsets));
1.90 paf 388: }
1.22 paf 389: }
1.94 paf 390: #ifndef DOXYGEN
391: struct Pass_cgi_header_attribute_info {
1.111 paf 392: Charset* charset;
393: HashStringValue* fields;
394: Value* content_type;
1.94 paf 395: };
396: #endif
1.111 paf 397: static void pass_cgi_header_attribute(
1.180 misha 398: ArrayString::element_type astring,
399: Pass_cgi_header_attribute_info* info) {
1.111 paf 400: size_t colon_pos=astring->pos(':');
1.130 paf 401: if(colon_pos!=STRING_NOT_FOUND) {
1.111 paf 402: const String& key=astring->mid(0, colon_pos).change_case(
403: *info->charset, String::CC_UPPER);
1.130 paf 404: Value* value=new VString(astring->mid(colon_pos+1, astring->length()).trim());
1.111 paf 405: info->fields->put(key, value);
1.181 misha 406: if(key==HTTP_CONTENT_TYPE_UPPER)
1.111 paf 407: info->content_type=value;
1.94 paf 408: }
1.29 paf 409: }
1.155 misha 410:
411: static void append_to_argv(Request& r, ArrayString& argv, const String* str){
1.187 misha 412: if(!str->is_empty())
1.196 misha 413: argv+=new String(str->cstr_to_string_body_untaint(String::L_AS_IS, 0, &r.charsets), String::L_AS_IS);
1.155 misha 414: }
415:
1.90 paf 416: /// @todo fix `` in perl - they produced flipping consoles and no output to perl
1.194 misha 417: static void _exec_cgi(Request& r, MethodParams& params, bool cgi) {
1.215 misha 418: bool is_text=true;
1.194 misha 419: size_t param_index=0;
1.215 misha 420: const String& mode=params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE);
421: if(VFile::is_valid_mode(mode)) {
422: is_text=VFile::is_text_mode(mode);
1.194 misha 423: param_index++;
1.162 misha 424: }
425:
426: if(param_index>=params.count())
1.215 misha 427: throw Exception(PARSER_RUNTIME, 0, FILE_NAME_MUST_BE_SPECIFIED);
1.162 misha 428:
1.215 misha 429: const String& script_name=r.absolute(params.as_string(param_index++, FILE_NAME_MUST_NOT_BE_CODE));
1.23 paf 430:
1.111 paf 431: HashStringString env;
1.62 paf 432: #define ECSTR(name, value_cstr) \
1.111 paf 433: if(value_cstr) \
434: env.put( \
1.112 paf 435: String::Body(#name), \
1.192 misha 436: String::Body(*value_cstr?value_cstr:0)); \
1.82 paf 437: // passing SAPI::environment
1.111 paf 438: if(const char *const *pairs=SAPI::environment(r.sapi_info)) {
439: while(const char* pair=*pairs++)
440: if(const char* eq_at=strchr(pair, '='))
441: if(eq_at[1]) // has value
442: env.put(
443: pa_strdup(pair, eq_at-pair),
444: pa_strdup(eq_at+1, 0));
1.82 paf 445: }
446:
1.23 paf 447: // const
1.63 paf 448: ECSTR(GATEWAY_INTERFACE, "CGI/1.1");
1.216 misha 449: ECSTR("PARSER_VERSION", PARSER_VERSION);
1.23 paf 450: // from Request.info
1.111 paf 451: ECSTR(DOCUMENT_ROOT, r.request_info.document_root);
452: ECSTR(PATH_TRANSLATED, r.request_info.path_translated);
453: ECSTR(REQUEST_METHOD, r.request_info.method);
454: ECSTR(QUERY_STRING, r.request_info.query_string);
455: ECSTR(REQUEST_URI, r.request_info.uri);
456: ECSTR(CONTENT_TYPE, r.request_info.content_type);
1.200 misha 457: ECSTR(CONTENT_LENGTH, format(r.request_info.content_length, "%u"));
1.82 paf 458: // SCRIPT_*
1.119 paf 459: env.put(String::Body("SCRIPT_NAME"), script_name);
460: //env.put(String::Body("SCRIPT_FILENAME"), ??&script_name);
1.23 paf 461:
1.90 paf 462: // environment & stdin from param
1.111 paf 463: String *in=new String();
1.109 paf 464: Charset *charset=0; // default script works raw_in 'source' charset = no transcoding needed
1.162 misha 465: if(param_index < params.count()) {
466: Value& venv=params.as_no_junction(param_index++, "env must not be code");
1.111 paf 467: if(HashStringValue* user_env=venv.get_hash()) {
1.141 paf 468: // $.charset [previewing to handle URI pieces]
469: if(Value* vcharset=user_env->get(CHARSET_EXEC_PARAM_NAME))
470: charset=&charsets.get(vcharset->as_string()
471: .change_case(r.charsets.source(), String::CC_UPPER));
472:
473: // $.others
474: Append_env_pair_info info={&r.charsets, &env, 0};
475: {
1.144 paf 476: // influence tainting
477: // main target -- $.QUERY_STRING -- URLencoding of tainted pieces to String::L_URI lang
1.141 paf 478: Temp_client_charset temp(r.charsets, charset? *charset: r.charsets.source());
1.143 paf 479: user_env->for_each<Append_env_pair_info*>(append_env_pair, &info);
1.141 paf 480: }
1.109 paf 481: // $.stdin
1.103 paf 482: if(info.vstdin) {
1.111 paf 483: if(const String* sstdin=info.vstdin->get_string()) {
1.213 moko 484: // untaint stdin
485: in = new String(sstdin->cstr_to_string_body_untaint(String::L_AS_IS), String::L_AS_IS);
1.103 paf 486: } else
1.199 misha 487: if(VFile* vfile=static_cast<VFile *>(info.vstdin->as("file")))
1.111 paf 488: in->append_know_length((const char* )vfile->value_ptr(), vfile->value_size(), String::L_TAINTED);
1.100 paf 489: else
1.156 misha 490: throw Exception(PARSER_RUNTIME,
1.111 paf 491: 0,
1.100 paf 492: STDIN_EXEC_PARAM_NAME " parameter must be string or file");
1.103 paf 493: }
1.90 paf 494: }
1.21 paf 495: }
496:
1.90 paf 497: // argv from params
1.111 paf 498: ArrayString argv;
1.162 misha 499: if(param_index < params.count()) {
1.180 misha 500: // influence tainting
501: Temp_client_charset temp(r.charsets, charset? *charset: r.charsets.source());
1.154 misha 502:
1.162 misha 503: for(size_t i=param_index; i<params.count(); i++) {
1.161 misha 504: Value& param=params.as_no_junction(i, PARAM_MUST_NOT_BE_CODE);
1.154 misha 505: if(param.is_defined()){
506: if(param.is_string()){
1.155 misha 507: append_to_argv(r, argv, param.get_string());
1.154 misha 508: } else {
1.155 misha 509: Table* table=param.get_table();
510: if(table){
511: for(size_t i=0; i<table->count(); i++) {
512: append_to_argv(r, argv, table->get(i)->get(0));
1.154 misha 513: }
514: } else {
1.156 misha 515: throw Exception(PARSER_RUNTIME,
1.154 misha 516: 0,
1.162 misha 517: "param must be string or table");
1.154 misha 518: }
519: }
1.145 misha 520: }
1.144 paf 521: }
1.21 paf 522: }
1.90 paf 523:
1.109 paf 524: // transcode if necessary
525: if(charset) {
1.111 paf 526: Charset::transcode(env, r.charsets.source(), *charset);
527: Charset::transcode(argv, r.charsets.source(), *charset);
528: in=&Charset::transcode(*in, r.charsets.source(), *charset);
529: }
530: // @todo
531: // ifdef WIN32 do OEM->ANSI transcode on some(.cmd?) programs to
532: // match silent conversion in OS
533:
534: // exec!
1.163 misha 535: PA_exec_result execution=pa_exec(false/*forced_allow*/, script_name, &env, argv, *in);
1.111 paf 536:
1.162 misha 537: File_read_result *file_out=&execution.out;
1.111 paf 538: String *real_err=&execution.err;
1.162 misha 539:
1.165 misha 540: // transcode err if necessary (@todo: need fix line breaks in err as well )
541: if(charset)
542: real_err=&Charset::transcode(*real_err, *charset, r.charsets.source());
543:
1.215 misha 544: if(file_out->length && is_text){
1.162 misha 545: fix_line_breaks(file_out->str, file_out->length);
546: // treat output as string
1.188 misha 547: String *real_out = new String(file_out->str);
1.162 misha 548:
1.165 misha 549: // transcode out if necessary
550: if(charset)
1.162 misha 551: real_out=&Charset::transcode(*real_out, *charset, r.charsets.source());
1.165 misha 552:
1.162 misha 553: // FIXME: unsafe cast
1.163 misha 554: file_out->str=const_cast<char *>(real_out->cstr()); // hacking a little
1.162 misha 555: file_out->length = real_out->length();
1.109 paf 556: }
557:
1.111 paf 558: VFile& self=GET_SELF(r, VFile);
1.109 paf 559:
1.162 misha 560: if(cgi) { // ^file::cgi
1.163 misha 561: const char* eol_marker=0;
562: size_t eol_marker_size;
563:
1.111 paf 564: // construct with 'out' body and header
1.165 misha 565: size_t dos_pos=(file_out->length)?strpos(file_out->str, "\r\n\r\n"):STRING_NOT_FOUND;
566: size_t unix_pos=(file_out->length)?strpos(file_out->str, "\n\n"):STRING_NOT_FOUND;
1.111 paf 567:
568: bool unix_header_break;
569: switch((dos_pos!=STRING_NOT_FOUND?10:00) + (unix_pos!=STRING_NOT_FOUND?01:00)) {
1.166 misha 570: case 10: // dos
571: unix_header_break=false;
572: break;
573: case 01: // unix
574: unix_header_break=true;
575: break;
576: case 11: // dos & unix
577: unix_header_break=unix_pos<dos_pos;
578: break;
579: default: // 00
580: unix_header_break=false; // calm down, compiler
1.179 misha 581: throw Exception("file.execute",
1.166 misha 582: 0,
583: "output does not contain CGI header; "
584: "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"",
585: execution.status,
1.194 misha 586: file_out->length, (file_out->length) ? (file_out->str) : "",
587: real_err->length(), real_err->cstr());
1.166 misha 588: break; //never reached
1.111 paf 589: }
590:
1.165 misha 591: size_t header_break_pos;
1.111 paf 592: if(unix_header_break) {
593: header_break_pos=unix_pos;
1.165 misha 594: eol_marker="\n";
595: eol_marker_size=1;
1.111 paf 596: } else {
597: header_break_pos=dos_pos;
1.165 misha 598: eol_marker="\r\n";
599: eol_marker_size=2;
1.111 paf 600: }
1.21 paf 601:
1.162 misha 602: file_out->str[header_break_pos] = 0;
1.188 misha 603: String *header=new String(file_out->str);
1.162 misha 604: unsigned long headersize = header_break_pos+eol_marker_size*2;
605: file_out->str += headersize;
606: file_out->length -= headersize;
607:
1.164 misha 608: // $body
609: self.set(false/*not tainted*/, file_out->str, file_out->length);
610:
1.162 misha 611: // $fields << header
1.194 misha 612: if(header) {
1.162 misha 613: ArrayString rows;
614: size_t pos_after=0;
615: header->split(rows, pos_after, eol_marker);
616: Pass_cgi_header_attribute_info info={0, 0, 0};
617: info.charset=&r.charsets.source();
618: info.fields=&self.fields();
619: rows.for_each(pass_cgi_header_attribute, &info);
620: if(info.content_type)
621: self.fields().put(content_type_name, info.content_type);
622: }
1.164 misha 623: } else { // ^file::exec
1.166 misha 624: // $body
625: self.set(false/*not tainted*/, file_out->str, file_out->length);
1.164 misha 626: }
1.163 misha 627:
1.215 misha 628: self.set_mode(is_text);
1.194 misha 629:
1.42 parser 630: // $status
1.111 paf 631: self.fields().put(file_status_name, new VInt(execution.status));
1.21 paf 632:
633: // $stderr
1.187 misha 634: if(!real_err->is_empty())
1.21 paf 635: self.fields().put(
1.119 paf 636: String::Body("stderr"),
1.111 paf 637: new VString(*real_err));
1.21 paf 638: }
1.111 paf 639: static void _exec(Request& r, MethodParams& params) {
640: _exec_cgi(r, params, false);
1.41 parser 641: }
1.111 paf 642: static void _cgi(Request& r, MethodParams& params) {
643: _exec_cgi(r, params, true);
1.41 parser 644: }
645:
1.111 paf 646: static void _list(Request& r, MethodParams& params) {
647: Value& relative_path=params.as_no_junction(0, "path must not be code");
1.47 parser 648:
1.191 misha 649: VRegex* vregex=0;
1.184 misha 650: VRegexCleaner vrcleaner;
651: if(params.count()>1){
652: Value& regexp=params.as_no_junction(1, "regexp must not be code");
1.191 misha 653: if(regexp.is_defined()){
1.199 misha 654: if(Value* value=regexp.as(VREGEX_TYPE)){
1.191 misha 655: vregex=static_cast<VRegex*>(value);
656: } else {
657: vregex=new VRegex(r.charsets.source(), ®exp.as_string(), 0/*options*/);
658: vregex->study();
659: vrcleaner.vregex=vregex;
660: }
1.184 misha 661: }
1.114 paf 662: }
1.47 parser 663:
1.196 misha 664: const char* absolute_path_cstr=r.absolute(relative_path.as_string()).taint_cstr(String::L_FILE_SPEC);
1.47 parser 665:
1.111 paf 666: Table::columns_type columns(new ArrayString);
667: *columns+=new String("name");
668: Table& table=*new Table(columns);
1.47 parser 669:
1.184 misha 670: const int ovector_size=(1/*match*/)*3;
671: int ovector[ovector_size];
672:
1.47 parser 673: LOAD_DIR(absolute_path_cstr,
1.111 paf 674: const char* file_name_cstr=ffblk.ff_name;
675: size_t file_name_size=strlen(file_name_cstr);
1.47 parser 676:
1.184 misha 677: if(!vregex || vregex->exec(ffblk.ff_name, file_name_size, ovector, ovector_size)>=0) {
1.111 paf 678: Table::element_type row(new ArrayString);
1.190 misha 679: *row+=new String(pa_strdup(file_name_cstr, file_name_size), String::L_TAINTED);
1.111 paf 680: table+=row;
1.47 parser 681: }
682: );
683:
1.60 parser 684: // write out result
1.111 paf 685: r.write_no_lang(*new VTable(&table));
1.47 parser 686: }
1.21 paf 687:
1.69 paf 688: #ifndef DOXYGEN
689: struct Lock_execute_body_info {
1.111 paf 690: Request* r;
691: Value* body_code;
1.69 paf 692: };
693: #endif
1.111 paf 694: static void lock_execute_body(int , void *ainfo) {
695: Lock_execute_body_info& info=*static_cast<Lock_execute_body_info *>(ainfo);
1.69 paf 696: // execute body
1.78 paf 697: info.r->write_assign_lang(info.r->process(*info.body_code));
1.69 paf 698: };
1.111 paf 699: static void _lock(Request& r, MethodParams& params) {
1.152 misha 700: const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));
1.116 paf 701: Lock_execute_body_info info={
702: &r,
1.117 paf 703: ¶ms.as_junction(1, "body must be code")
1.116 paf 704: };
1.69 paf 705:
1.158 misha 706: file_write_action_under_lock(
707: file_spec,
708: "lock",
709: lock_execute_body,
710: &info);
1.69 paf 711: }
712:
1.111 paf 713: static void _find(Request& r, MethodParams& params) {
1.215 misha 714: const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
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
730: int after_base_slash=lastposafter(*file_spec, 0, "/", 1);
1.111 paf 731: const String* dirname=&file_spec->mid(0, after_base_slash);
732: const String& basename=file_spec->mid(after_base_slash, file_spec->length());
1.90 paf 733:
734: int after_monkey_slash;
735: while((after_monkey_slash=lastposafter(*dirname, 0, "/", 1, true))>0) {
1.111 paf 736: String test_name;
737: test_name<<*(dirname=&dirname->mid(0, after_monkey_slash));
738: test_name<<basename;
1.142 paf 739: if(file_exist(r.absolute(test_name))) {
1.111 paf 740: r.write_assign_lang(test_name);
1.90 paf 741: return;
742: }
743: }
744:
745: // no way, not found
1.215 misha 746: if(not_found_code)
747: r.write_pass_lang(r.process(*not_found_code));
1.90 paf 748: }
749:
1.111 paf 750: static void _dirname(Request& r, MethodParams& params) {
1.152 misha 751: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180 misha 752: // /a/some.tar.gz > /a
1.89 paf 753: // /a/b/ > /a
754: int afterslash=lastposafter(file_spec, 0, "/", 1, true);
755: if(afterslash>0)
756: r.write_assign_lang(file_spec.mid(0, afterslash==1?1:afterslash-1));
757: else
1.189 misha 758: r.write_assign_lang(String("."));
1.89 paf 759: }
760:
1.111 paf 761: static void _basename(Request& r, MethodParams& params) {
1.152 misha 762: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180 misha 763: // /a/some.tar.gz > some.tar.gz
1.89 paf 764: int afterslash=lastposafter(file_spec, 0, "/", 1);
1.111 paf 765: r.write_assign_lang(file_spec.mid(afterslash, file_spec.length()));
1.89 paf 766: }
767:
1.111 paf 768: static void _justname(Request& r, MethodParams& params) {
1.152 misha 769: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180 misha 770: // /a/some.tar.gz > some.tar
1.89 paf 771: int afterslash=lastposafter(file_spec, 0, "/", 1);
772: int afterdot=lastposafter(file_spec, afterslash, ".", 1);
1.111 paf 773: r.write_assign_lang(file_spec.mid(afterslash, afterdot!=afterslash?afterdot-1:file_spec.length()));
1.89 paf 774: }
1.111 paf 775: static void _justext(Request& r, MethodParams& params) {
1.152 misha 776: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180 misha 777: // /a/some.tar.gz > gz
1.89 paf 778: int afterdot=lastposafter(file_spec, 0, ".", 1);
779: if(afterdot>0)
1.111 paf 780: r.write_assign_lang(file_spec.mid(afterdot, file_spec.length()));
1.89 paf 781: }
782:
1.111 paf 783: static void _fullpath(Request& r, MethodParams& params) {
1.152 misha 784: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.111 paf 785: const String* result;
1.102 paf 786: if(file_spec.first_char()=='/')
787: result=&file_spec;
788: else {
789: // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
790: const String& full_disk_path=r.absolute(file_spec);
1.111 paf 791: size_t document_root_length=strlen(r.request_info.document_root);
1.106 paf 792:
793: if(document_root_length>0) {
1.111 paf 794: char last_char=r.request_info.document_root[document_root_length-1];
1.106 paf 795: if(last_char == '/' || last_char == '\\')
796: --document_root_length;
797: }
1.111 paf 798: result=&full_disk_path.mid(document_root_length, full_disk_path.length());
1.102 paf 799: }
800: r.write_assign_lang(*result);
801: }
802:
1.121 paf 803: static void _sql_string(Request& r, MethodParams&) {
804: VFile& self=GET_SELF(r, VFile);
805:
806: const char *quoted=r.connection()->quote(self.value_ptr(), self.value_size());
807: r.write_assign_lang(*new String(quoted));
808: }
1.89 paf 809:
1.122 paf 810: #ifndef DOXYGEN
811: class File_sql_event_handlers: public SQL_Driver_query_event_handlers {
812: const String& statement_string; const char* statement_cstr;
813: int got_columns;
814: int got_cells;
815: public:
816: String::C value;
1.131 paf 817: const String* user_file_name;
818: const String* user_content_type;
1.122 paf 819: public:
820: File_sql_event_handlers(
821: const String& astatement_string, const char* astatement_cstr):
822: statement_string(astatement_string), statement_cstr(astatement_cstr),
823: got_columns(0),
824: got_cells(0),
825: user_file_name(0),
826: user_content_type(0) {}
827:
828: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
829: if(got_columns++==3) {
1.156 misha 830: error=SQL_Error(PARSER_RUNTIME, "result must contain not more then 3 columns");
1.122 paf 831: return true;
832: }
833: return false;
834: }
835: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
836: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
837: bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
838: try {
839: switch(got_cells++) {
840: case 0:
841: value=String::C(str, length);
842: break;
843: case 1:
1.131 paf 844: if(!user_file_name) // user not specified?
1.190 misha 845: user_file_name=new String(str, String::L_TAINTED);
1.122 paf 846: break;
847: case 2:
1.131 paf 848: if(!user_content_type) // user not specified?
1.190 misha 849: user_content_type=new String(str, String::L_TAINTED);
1.122 paf 850: break;
851: default:
1.210 misha 852: error=SQL_Error(PARSER_RUNTIME, "result must not contain more then one row, three columns");
1.122 paf 853: return true;
854: }
855: return false;
856: } catch(...) {
857: error=SQL_Error("exception occured in File_sql_event_handlers::add_row_cell");
858: return true;
859: }
860: }
861: };
862: #endif
863: static void _sql(Request& r, MethodParams& params) {
1.131 paf 864: Value& statement=params.as_junction(0, "statement must be code");
1.122 paf 865:
866: Temp_lang temp_lang(r, String::L_SQL);
867: const String& statement_string=r.process_to_string(statement);
1.197 misha 868: const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection());
1.195 misha 869:
1.122 paf 870: File_sql_event_handlers handlers(statement_string, statement_cstr);
1.131 paf 871:
1.173 misha 872: ulong limit=SQL_NO_LIMIT;
1.172 misha 873: ulong offset=0;
874:
1.131 paf 875: if(params.count()>1)
1.214 misha 876: if(HashStringValue* options=params.as_hash(1)){
1.131 paf 877: int valid_options=0;
878: if(Value* vfilename=options->get(NAME_NAME)) {
879: valid_options++;
880: handlers.user_file_name=&vfilename->as_string();
881: }
882: if(Value* vcontent_type=options->get(CONTENT_TYPE_NAME)) {
883: valid_options++;
884: handlers.user_content_type=&vcontent_type->as_string();
885: }
1.173 misha 886: if(Value* vlimit=options->get(sql_limit_name)) {
887: valid_options++;
888: limit=(ulong)r.process_to_value(*vlimit).as_double();
889: }
1.172 misha 890: if(Value* voffset=options->get(sql_offset_name)) {
891: valid_options++;
892: offset=(ulong)r.process_to_value(*voffset).as_double();
893: }
1.131 paf 894: if(valid_options!=options->count())
1.207 misha 895: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.131 paf 896: }
897:
898:
1.122 paf 899: r.connection()->query(
1.123 paf 900: statement_cstr,
901: 0, 0,
1.173 misha 902: offset, limit,
1.122 paf 903: handlers,
904: statement_string);
905:
906: if(!handlers.value)
1.156 misha 907: throw Exception(PARSER_RUNTIME,
1.122 paf 908: 0,
909: "produced no result");
910:
1.215 misha 911: VFile& self=GET_SELF(r, VFile);
1.122 paf 912:
1.215 misha 913: self.set(true/*tainted*/, handlers.value.str, handlers.value.length, handlers.user_file_name
914: , handlers.user_content_type ? new VString(*handlers.user_content_type) : 0
915: , &r);
1.194 misha 916: self.set_mode(false/*binary*/);
1.122 paf 917: }
1.140 paf 918:
1.139 paf 919: static void _base64(Request& r, MethodParams& params) {
1.209 misha 920: bool dynamic=!(&r.get_self() == file_class);
921: if(dynamic) {
1.180 misha 922: VFile& self=GET_SELF(r, VFile);
923: if(params.count()) {
1.209 misha 924: // decode:
925: // ^file::base64[encoded] // backward
1.217 ! misha 926: // ^file::base64[mode;user-file-name;encoded[;$.content-type[...] $.strict(true|false)]]
1.209 misha 927: bool is_text=false;
1.217 ! misha 928: bool strict=false;
1.209 misha 929: VString* vcontent_type=0;
1.215 misha 930: const String* user_file_name=0;
1.209 misha 931: size_t param_index=0;
932:
933: if(params.count() > 1) {
934: if(params.count() < 3)
935: throw Exception(PARSER_RUNTIME,
936: 0,
1.215 misha 937: "constructor can not have less then 3 parameters (has %d parameters)",
1.209 misha 938: params.count()); // actually it accepts 1 parameter (backward)
939:
1.215 misha 940: is_text=VFile::is_text_mode(params.as_string(0, MODE_MUST_NOT_BE_CODE));
941: user_file_name=¶ms.as_string(1, FILE_NAME_MUST_BE_STRING);
1.209 misha 942:
943: if(params.count() == 4)
944: if(HashStringValue* options=params.as_hash(3)) {
945: int valid_options=0;
946: if(Value* value=options->get(CONTENT_TYPE_NAME)) {
947: vcontent_type=new VString(value->as_string());
948: valid_options++;
949: }
1.217 ! misha 950: if(Value* vstrict=options->get(BASE64_STRICT_OPTION_NAME)) {
! 951: strict=r.process_to_value(*vstrict).as_bool();
! 952: valid_options++;
! 953: }
1.209 misha 954: if(valid_options!=options->count())
955: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
956: }
957:
958: param_index=2;
959: }
960:
961: const char* encoded=params.as_string(param_index, PARAMETER_MUST_BE_STRING).cstr();
962:
1.180 misha 963: char* decoded=0;
964: size_t length=0;
1.217 ! misha 965: pa_base64_decode(encoded, strlen(encoded), decoded, length, strict);
1.209 misha 966:
967: if(length && is_text)
968: fix_line_breaks(decoded, length);
969:
1.215 misha 970: self.set(true/*tainted*/, decoded, length, user_file_name, vcontent_type, &r);
1.209 misha 971:
972: if(params.count() > 1)
973: self.set_mode(is_text);
1.180 misha 974: } else {
975: // encode: ^f.base64[]
976: const char* encoded=pa_base64_encode(self.value_ptr(), self.value_size());
1.190 misha 977: r.write_assign_lang(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed**/));
1.180 misha 978: }
1.151 misha 979: } else {
1.180 misha 980: // encode: ^file:base64[filespec]
1.152 misha 981: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.151 misha 982: const char* encoded=pa_base64_encode(r.absolute(file_spec));
1.190 misha 983: r.write_assign_lang(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed*/));
1.151 misha 984: }
1.139 paf 985: }
1.140 paf 986:
1.146 misha 987: static void _crc32(Request& r, MethodParams& params) {
988: unsigned long crc32 = 0;
989: if(&r.get_self() == file_class) {
990: // ^file:crc32[file-name]
991: if(params.count()) {
1.152 misha 992: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.146 misha 993: crc32=pa_crc32(r.absolute(file_spec));
994: } else {
1.215 misha 995: throw Exception(PARSER_RUNTIME, 0, FILE_NAME_MUST_BE_SPECIFIED);
1.146 misha 996: }
997: } else {
998: // ^file.crc32[]
999: VFile& self=GET_SELF(r, VFile);
1000: crc32=pa_crc32(self.value_ptr(), self.value_size());
1001: }
1002: r.write_no_lang(*new VInt(crc32));
1003: }
1004:
1005:
1.147 misha 1006: static void file_md5_file_action(
1.180 misha 1007: struct stat& finfo,
1008: int f,
1009: const String& , const char* /*fname*/, bool,
1010: void *context)
1.147 misha 1011: {
1012: PA_MD5_CTX& md5context=*static_cast<PA_MD5_CTX *>(context);
1013: if(finfo.st_size) {
1.148 misha 1014: int nCount=0;
1.147 misha 1015: do {
1016: unsigned char buffer[FILE_BUFFER_SIZE];
1.150 misha 1017: nCount = file_block_read(f, buffer, sizeof(buffer));
1.147 misha 1018: if ( nCount ){
1019: pa_MD5Update(&md5context, (const unsigned char*)buffer, nCount);
1020: }
1.148 misha 1021: } while(nCount > 0);
1.147 misha 1022: }
1023: }
1024:
1025: const char* pa_md5(const String& file_spec)
1026: {
1027: PA_MD5_CTX context;
1028: unsigned char digest[16];
1029: pa_MD5Init(&context);
1030: file_read_action_under_lock(file_spec, "md5", file_md5_file_action, &context);
1031: pa_MD5Final(digest, &context);
1032:
1033: return hex_string(digest, sizeof(digest), false);
1034: }
1035:
1036: const char* pa_md5(const char *in, size_t in_size)
1037: {
1038: PA_MD5_CTX context;
1039: unsigned char digest[16];
1040: pa_MD5Init(&context);
1041: pa_MD5Update(&context, (const unsigned char*)in, in_size);
1042: pa_MD5Final(digest, &context);
1043:
1044: return hex_string(digest, sizeof(digest), false);
1045: }
1046:
1047: static void _md5(Request& r, MethodParams& params) {
1048: const char* md5;
1049: if(&r.get_self() == file_class) {
1050: // ^file:md5[file-name]
1051: if(params.count()) {
1.152 misha 1052: const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.147 misha 1053: md5=pa_md5(r.absolute(file_spec));
1054: } else {
1.215 misha 1055: throw Exception(PARSER_RUNTIME, 0, FILE_NAME_MUST_BE_SPECIFIED);
1.147 misha 1056: }
1057: } else {
1058: // ^file.md5[]
1059: VFile& self=GET_SELF(r, VFile);
1060: md5=pa_md5(self.value_ptr(), self.value_size());
1061:
1062: }
1063: r.write_no_lang(*new String(md5));
1064: }
1065:
1.32 paf 1066: // constructor
1067:
1.111 paf 1068: MFile::MFile(): Methoded("file") {
1.215 misha 1069: // ^file::create[text|binary;file-name;string-or-file[;options hash]]
1070: // ^file::create[string-or-file[;options hash]]
1071: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 4);
1.138 paf 1072:
1.146 misha 1073: // ^file.save[mode;file-name]
1.201 misha 1074: // ^file.save[mode;file-name;$.charset[...]]
1075: add_native_method("save", Method::CT_DYNAMIC, _save, 2, 3);
1.7 paf 1076:
1.146 misha 1077: // ^file:delete[file-name]
1.32 paf 1078: add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.45 parser 1079:
1.146 misha 1080: // ^file:move[from-file-name;to-file-name]
1.45 parser 1081: add_native_method("move", Method::CT_STATIC, _move, 2, 2);
1.8 paf 1082:
1.146 misha 1083: // ^file::load[mode;disk-name]
1084: // ^file::load[mode;disk-name;user-name]
1.201 misha 1085: // ^file::load[mode;disk-name;user-name;options hash]
1086: // ^file::load[mode;disk-name;options hash]
1.180 misha 1087: add_native_method("load", Method::CT_DYNAMIC, _load, 2, 4);
1.25 paf 1088:
1.146 misha 1089: // ^file::stat[disk-name]
1.32 paf 1090: add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21 paf 1091:
1.162 misha 1092: // ^file::cgi[mode;file-name]
1093: // ^file::cgi[mode;file-name;env hash]
1094: // ^file::cgi[mode;file-name;env hash;1cmd;2line;3ar;4g;5s]
1095: add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 3+50);
1096:
1097: // ^file::exec[mode;file-name]
1098: // ^file::exec[mode;file-name;env hash]
1099: // ^file::exec[mode;file-name;env hash;1cmd;2line;3ar;4g;5s]
1100: add_native_method("exec", Method::CT_DYNAMIC, _exec, 1, 3+50);
1.47 parser 1101:
1102: // ^file:list[path]
1103: // ^file:list[path][regexp]
1104: add_native_method("list", Method::CT_STATIC, _list, 1, 2);
1.69 paf 1105:
1106: // ^file:lock[path]{code}
1107: add_native_method("lock", Method::CT_STATIC, _lock, 2, 2);
1.90 paf 1108:
1.146 misha 1109: // ^file:find[file-name]
1110: // ^file:find[file-name]{when-not-found}
1.90 paf 1111: add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.47 parser 1112:
1.201 misha 1113: // ^file:dirname[/a/some.tar.gz]=/a
1.89 paf 1114: // ^file:dirname[/a/b/]=/a
1115: add_native_method("dirname", Method::CT_STATIC, _dirname, 1, 1);
1.201 misha 1116: // ^file:basename[/a/some.tar.gz]=some.tar.gz
1117: add_native_method("basename", Method::CT_STATIC, _basename, 1, 1);
1118: // ^file:justname[/a/some.tar.gz]=some.tar
1.89 paf 1119: add_native_method("justname", Method::CT_STATIC, _justname, 1, 1);
1.201 misha 1120: // ^file:justext[/a/some.tar.gz]=gz
1.89 paf 1121: add_native_method("justext", Method::CT_STATIC, _justext, 1, 1);
1.201 misha 1122: // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
1.102 paf 1123: add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1);
1.121 paf 1124:
1.201 misha 1125: // ^file.sql-string[]
1.121 paf 1126: add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.122 paf 1127:
1.209 misha 1128: // ^file::sql{}
1.201 misha 1129: // ^file::sql{}[options hash]
1.122 paf 1130: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.139 paf 1131:
1.209 misha 1132: // encode:
1133: // ^file.base64[]
1134: // ^file:base64[file-name]
1135: // decode:
1136: // ^file::base64[encoded] // backward
1137: // ^file::base64[mode;user-file-name;encoded]
1138: // ^file::base64[mode;user-file-name;encoded;$.content-type[...]]
1139: add_native_method("base64", Method::CT_ANY, _base64, 0, 4);
1.146 misha 1140:
1141: // ^file.crc32[]
1142: // ^file:crc32[file-name]
1143: add_native_method("crc32", Method::CT_ANY, _crc32, 0, 1);
1.147 misha 1144:
1145: // ^file.md5[]
1146: // ^file:md5[file-name]
1147: add_native_method("md5", Method::CT_ANY, _md5, 0, 1);
1148:
1.148 misha 1149: // ^file:copy[from-file-name;to-file-name]
1150: add_native_method("copy", Method::CT_STATIC, _copy, 2, 2);
1.1 paf 1151: }
E-mail: