Annotation of parser3/src/classes/file.C, revision 1.107.2.16
1.17 paf 1: /** @file
2: Parser: @b file parser class.
3:
1.107.2.3 paf 4: Copyright (c) 2001-2003 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.107.2.16! paf 8: static const char* IDENT_FILE_C="$Date: 2003/02/26 11:29:55 $";
1.47 parser 9:
10: #include "pa_config_includes.h"
11:
12: #include "pcre.h"
1.1 paf 13:
1.35 paf 14: #include "classes.h"
1.107.2.5 paf 15: #include "pa_vmethod_frame.h"
16:
1.1 paf 17: #include "pa_request.h"
18: #include "pa_vfile.h"
1.11 paf 19: #include "pa_table.h"
1.21 paf 20: #include "pa_vint.h"
1.24 paf 21: #include "pa_exec.h"
1.40 parser 22: #include "pa_vdate.h"
1.47 parser 23: #include "pa_dir.h"
24: #include "pa_vtable.h"
1.67 paf 25: #include "pa_charset.h"
1.1 paf 26:
1.32 paf 27: // defines
28:
1.48 parser 29: #define TEXT_MODE_NAME "text"
1.90 paf 30: #define STDIN_EXEC_PARAM_NAME "stdin"
1.48 parser 31:
1.107.2.8 paf 32: // class
33:
1.107.2.12 paf 34: class MFile: public Methoded {
1.107.2.8 paf 35: public: // VStateless_class
36:
37: ValuePtr create_new_value() { return ValuePtr(new VFile()); }
38:
39: public: // Methoded
40: bool used_directly() { return true; }
41:
42: public:
43: MFile();
44:
45: };
46:
47: // global variable
48:
1.107.2.15 paf 49: DECLARE_CLASS_VAR(file, new MFile, 0);
1.107.2.8 paf 50:
1.83 paf 51: // consts
52:
53: /// from apache-1.3|src|support|suexec.c
1.107.2.3 paf 54: static const char* suexec_safe_env_lst[]={
1.83 paf 55: "AUTH_TYPE",
56: "CONTENT_LENGTH",
57: "CONTENT_TYPE",
58: "DATE_GMT",
59: "DATE_LOCAL",
60: "DOCUMENT_NAME",
61: "DOCUMENT_PATH_INFO",
62: "DOCUMENT_ROOT",
63: "DOCUMENT_URI",
64: "FILEPATH_INFO",
65: "GATEWAY_INTERFACE",
66: "LAST_MODIFIED",
67: "PATH_INFO",
68: "PATH_TRANSLATED",
69: "QUERY_STRING",
70: "QUERY_STRING_UNESCAPED",
71: "REMOTE_ADDR",
72: "REMOTE_HOST",
73: "REMOTE_IDENT",
74: "REMOTE_PORT",
75: "REMOTE_USER",
76: "REDIRECT_QUERY_STRING",
77: "REDIRECT_STATUS",
78: "REDIRECT_URL",
79: "REQUEST_METHOD",
80: "REQUEST_URI",
81: "SCRIPT_FILENAME",
82: "SCRIPT_NAME",
83: "SCRIPT_URI",
84: "SCRIPT_URL",
85: "SERVER_ADMIN",
86: "SERVER_NAME",
87: "SERVER_ADDR",
88: "SERVER_PORT",
89: "SERVER_PROTOCOL",
90: "SERVER_SOFTWARE",
91: "UNIQUE_ID",
92: "USER_NAME",
93: "TZ",
94: NULL
95: };
96:
1.107.2.7 paf 97: // statics
98:
99: static StringPtr adate_name(new String("adate"));
100: static StringPtr mdate_name(new String("mdate"));
101: static StringPtr cdate_name(new String("cdate"));
102:
1.1 paf 103: // methods
104:
1.107.2.13 paf 105: static void _save(Request& r, StringPtr /*method_name*/, MethodParams* params) {
1.107.2.7 paf 106: Pool& pool=r.pool();
1.107.2.13 paf 107: ValuePtr vmode_name=params-> as_no_junction(0, "mode must not be code");
108: ValuePtr vfile_name=params->as_no_junction(1, "file name must not be code");
1.4 paf 109:
1.7 paf 110: // save
1.107.2.7 paf 111: GET_SELF(r, VFile).save(r.absolute(vfile_name->as_string(&pool)),
112: *vmode_name->as_string(&pool)==TEXT_MODE_NAME);
1.7 paf 113: }
114:
1.107.2.13 paf 115: static void _delete(Request& r, StringPtr /*method_name*/, MethodParams* params) {
1.107.2.7 paf 116: Pool& pool=r.pool();
1.107.2.13 paf 117: ValuePtr vfile_name=params->as_no_junction(0, "file name must not be code");
1.7 paf 118:
119: // unlink
1.107.2.7 paf 120: file_delete(r.absolute(vfile_name->as_string(&pool)));
1.1 paf 121: }
122:
1.107.2.13 paf 123: static void _move(Request& r, StringPtr /*method_name*/, MethodParams* params) {
1.107.2.7 paf 124: Pool& pool=r.pool();
1.107.2.13 paf 125: ValuePtr vfrom_file_name=params->as_no_junction(0, "from file name must not be code");
126: ValuePtr vto_file_name=params->as_no_junction(1, "to file name must not be code");
1.45 parser 127:
1.51 parser 128: // move
1.68 paf 129: file_move(
1.107.2.7 paf 130: r.absolute(vfrom_file_name->as_string(&pool)),
131: r.absolute(vto_file_name->as_string(&pool)));
1.45 parser 132: }
133:
1.107.2.7 paf 134: static void _load_pass_param(
135: HashStringValue::key_type key,
136: HashStringValue::value_type value,
137: HashStringValue *dest) {
138: dest->put(key, value);
1.105 paf 139: }
1.107.2.13 paf 140: static void _load(Request& r, StringPtr method_name, MethodParams* params) {
1.9 paf 141: Pool& pool=r.pool();
1.107.2.13 paf 142: ValuePtr vmode_name=params-> as_no_junction(0, "mode must not be code");
143: StringPtr lfile_name=r.absolute(params->as_no_junction(1, "file name must not be code")->as_string(&pool));
144: ValuePtr third_param=params->count()>2?params->as_no_junction(2, "filename or options must not be code")
1.107.2.7 paf 145: :ValuePtr(0);
146: HashStringValue* third_param_hash=third_param?third_param->get_hash(method_name):0;
1.104 paf 147: int alt_filename_param_index=2;
148: if(third_param_hash)
149: alt_filename_param_index++;
1.9 paf 150:
1.107.2.9 paf 151: File_read_result file=file_read(pool, r.charsets.source(), lfile_name,
1.107.2.7 paf 152: *vmode_name->as_string(&pool)==TEXT_MODE_NAME,
153: third_param_hash
1.104 paf 154: );
1.9 paf 155:
1.107.2.13 paf 156: char *user_file_name=params->count()>alt_filename_param_index?
157: params->as_string(alt_filename_param_index, "filename must be string")->cstr(pool)
1.107.2.7 paf 158: :lfile_name->cstr(pool, String::UL_FILE_SPEC);
1.104 paf 159:
1.107.2.7 paf 160: ValuePtr vcontent_type(0);
161: if(file.headers)
162: vcontent_type=file.headers->get(content_type_name);
1.104 paf 163: if(!vcontent_type)
1.107.2.7 paf 164: vcontent_type=ValuePtr(new VString(r.mime_type_of(user_file_name)));
1.10 paf 165:
1.107.2.7 paf 166: VFile& self=GET_SELF(r, VFile);
1.107.2.9 paf 167: self.set(pool, true/*tainted*/, file.data, file.size, user_file_name, vcontent_type);
1.107.2.7 paf 168: if(file.headers)
169: file.headers->for_each(_load_pass_param, &self.fields());
1.9 paf 170: }
171:
1.107.2.13 paf 172: static void _stat(Request& r, StringPtr method_name, MethodParams* params) {
1.25 paf 173: Pool& pool=r.pool();
1.107.2.13 paf 174: ValuePtr vfile_name=params->as_no_junction(0, "file name must not be code");
1.25 paf 175:
1.107.2.7 paf 176: StringPtr lfile_name=vfile_name->as_string(&pool);
1.25 paf 177:
1.40 parser 178: size_t size;
179: time_t atime, mtime, ctime;
180: file_stat(r.absolute(lfile_name),
181: size,
182: atime, mtime, ctime);
1.25 paf 183:
1.107.2.7 paf 184: VFile& self=GET_SELF(r, VFile);
185: self.set(pool, true/*tainted*/, 0/*no bytes*/, size);
186: HashStringValue& ff=self.fields();
187: ff.put(adate_name, ValuePtr(new VDate(atime)));
188: ff.put(mdate_name, ValuePtr(new VDate(mtime)));
189: ff.put(cdate_name, ValuePtr(new VDate(ctime)));
190: ff.put(content_type_name, ValuePtr(new VString(r.mime_type_of(lfile_name->cstr(String::UL_FILE_SPEC)))));
1.25 paf 191: }
192:
1.107.2.14 paf 193: static bool is_safe_env_key(const char* key) {
1.88 paf 194: if(strncasecmp(key, "HTTP_", 5)==0)
1.83 paf 195: return true;
1.87 paf 196: if(strncasecmp(key, "CGI_", 4)==0)
1.83 paf 197: return true;
198: for(int i=0; suexec_safe_env_lst[i]; i++) {
1.87 paf 199: if(strcasecmp(key, suexec_safe_env_lst[i])==0)
1.83 paf 200: return true;
201: }
202: return false;
203: }
1.90 paf 204: #ifndef DOXYGEN
205: struct Append_env_pair_info {
1.107.2.7 paf 206: Pool* pool;
207: HashStringString* env;
208: ValuePtr vstdin;
1.90 paf 209: };
210: #endif
1.107.2.7 paf 211: static void append_env_pair(
212: HashStringValue::key_type akey,
213: HashStringValue::value_type avalue,
214: Append_env_pair_info *info) {
215: if(*akey==STDIN_EXEC_PARAM_NAME) {
216: info->vstdin=avalue;
1.90 paf 217: } else {
1.107.2.7 paf 218: if(!is_safe_env_key(akey->cstr()))
1.90 paf 219: throw Exception("parser.runtime",
1.107.2.7 paf 220: akey,
1.90 paf 221: "not safe environment variable");
1.107.2.7 paf 222: info->env->put(akey, avalue->as_string(info->pool));
1.90 paf 223: }
1.22 paf 224: }
1.94 paf 225: #ifndef DOXYGEN
226: struct Pass_cgi_header_attribute_info {
1.107.2.7 paf 227: Pool* pool;
228: Charset* charset;
229: HashStringValue* fields;
230: ValuePtr content_type;
1.94 paf 231: };
232: #endif
1.107.2.7 paf 233: static void pass_cgi_header_attribute(
234: ArrayString::element_type astring,
235: Pass_cgi_header_attribute_info* info) {
236: int colon_pos=astring->pos(":", 1);
1.94 paf 237: if(colon_pos>0) {
1.107.2.7 paf 238: StringPtr key(astring->mid(0, colon_pos)->change_case(
239: *info->pool, *info->charset, String::CC_UPPER));
240: ValuePtr value(new VString(astring->mid(colon_pos+1, astring->size())));
241: info->fields->put(key, value);
242: if(*key=="CONTENT-TYPE")
243: info->content_type=value;
1.94 paf 244: }
1.29 paf 245: }
1.90 paf 246: /// @todo fix `` in perl - they produced flipping consoles and no output to perl
1.107.2.13 paf 247: static void _exec_cgi(Request& r, StringPtr method_name, MethodParams* params,
1.41 parser 248: bool cgi) {
1.21 paf 249: Pool& pool=r.pool();
250:
1.107.2.13 paf 251: ValuePtr vfile_name=params->as_no_junction(0, "file name must not be code");
1.21 paf 252:
1.107.2.7 paf 253: StringPtr script_name=r.absolute(vfile_name->as_string(&pool));
1.23 paf 254:
1.107.2.7 paf 255: HashStringString env;
1.62 paf 256: #define ECSTR(name, value_cstr) \
1.107.2.7 paf 257: if(value_cstr) \
258: env.put( \
259: StringPtr(new String(#name)), \
260: StringPtr(new String(value_cstr))); \
1.82 paf 261: // passing SAPI::environment
1.107.2.10 paf 262: if(const char* const *pairs=SAPI::environment(r.sapi_info, pool)) {
1.107.2.3 paf 263: while(const char* pair=*pairs++)
1.107.2.7 paf 264: if(const char* eq_at=strchr(pair, '='))
265: env.put(
266: StringPtr(new String(pair, eq_at-pair)),
267: StringPtr(new String(eq_at+1)));
1.82 paf 268: }
269:
1.23 paf 270: // const
1.63 paf 271: ECSTR(GATEWAY_INTERFACE, "CGI/1.1");
1.23 paf 272: // from Request.info
1.107.2.7 paf 273: ECSTR(DOCUMENT_ROOT, r.request_info.document_root);
274: ECSTR(PATH_TRANSLATED, r.request_info.path_translated);
275: ECSTR(REQUEST_METHOD, r.request_info.method);
276: ECSTR(QUERY_STRING, r.request_info.query_string);
277: ECSTR(REQUEST_URI, r.request_info.uri);
278: ECSTR(CONTENT_TYPE, r.request_info.content_type);
1.23 paf 279: char content_length_cstr[MAX_NUMBER];
1.107.2.7 paf 280: snprintf(content_length_cstr, MAX_NUMBER, "%u", r.request_info.content_length);
281: //String content_length(content_length_cstr);
1.62 paf 282: ECSTR(CONTENT_LENGTH, content_length_cstr);
1.82 paf 283: // SCRIPT_*
1.107.2.7 paf 284: env.put(StringPtr(new String("SCRIPT_NAME")), script_name);
1.82 paf 285: //env.put(*new(pool) String(pool, "SCRIPT_FILENAME"), ??&script_name);
1.23 paf 286:
1.103 paf 287: bool stdin_specified=false;
1.90 paf 288: // environment & stdin from param
1.107.2.7 paf 289: String in;
1.107.2.13 paf 290: if(params->count()>1) {
291: ValuePtr venv=params->as_no_junction(1, "env must not be code");
1.107.2.7 paf 292: if(HashStringValue* user_env=venv->get_hash(method_name)) {
293: Append_env_pair_info info;
294: info.pool=&pool;
295: info.env=&env;
1.90 paf 296: user_env->for_each(append_env_pair, &info);
1.103 paf 297: if(info.vstdin) {
298: stdin_specified=true;
1.107.2.7 paf 299: if(StringPtr sstdin=info.vstdin->get_string(&pool)) {
1.103 paf 300: in.append(*sstdin, String::UL_CLEAN, true);
301: } else
1.100 paf 302: if(VFile *vfile=static_cast<VFile *>(info.vstdin->as("file", false)))
1.107.2.3 paf 303: in.APPEND_TAINTED((const char* )vfile->value_ptr(), vfile->value_size(),
1.100 paf 304: "$.stdin[assigned]", 0);
305: else
306: throw Exception("parser.runtime",
1.107.2.6 paf 307: method_name,
1.100 paf 308: STDIN_EXEC_PARAM_NAME " parameter must be string or file");
1.103 paf 309: }
1.90 paf 310: }
1.21 paf 311: }
312:
1.90 paf 313: // argv from params
1.107.2.7 paf 314: ArrayString argv;
1.107.2.13 paf 315: if(params->count()>2) {
316: for(int i=2; i<params->count(); i++)
317: argv+=params->as_string(i, "parameter must be string");
1.21 paf 318: }
319:
1.90 paf 320: // passing POST data
1.103 paf 321: if(!stdin_specified) // if $.stdin[...] not specified
1.107.2.7 paf 322: in.APPEND(r.request_info.post_data, r.request_info.post_size, String::UL_CLEAN,
323: "POST data (passed)", 0);
1.90 paf 324:
325: // exec!
1.107.2.7 paf 326: PA_exec_result execution=
1.107.2.16! paf 327: pa_exec(pool, false/*forced_allow*/, script_name, &env, argv, in);
1.21 paf 328:
1.107.2.7 paf 329: VFile& self=GET_SELF(r, VFile);
1.21 paf 330:
1.107.2.7 paf 331: StringPtr body=execution.out; // ^file:exec
332: ValuePtr content_type(0);
1.107.2.3 paf 333: const char* eol_marker=0; size_t eol_marker_size;
1.107.2.7 paf 334: StringPtr header(0);
1.41 parser 335: if(cgi) { // ^file:cgi
336: // construct with 'out' body and header
1.107.2.7 paf 337: int dos_pos=execution.out->pos("\r\n\r\n", 4);
338: int unix_pos=execution.out->pos("\n\n", 2);
1.98 paf 339:
340: bool unix_header_break;
341: switch((dos_pos >= 0?10:00) + (unix_pos >= 0?01:00)) {
342: case 10: // dos
343: unix_header_break=false;
344: break;
345: case 01: // unix
346: unix_header_break=true;
347: break;
348: case 11: // dos & unix
349: unix_header_break=unix_pos<dos_pos;
350: break;
351: default: // 00
352: unix_header_break=false; // calm down, compiler
1.74 paf 353: throw Exception(0,
1.107.2.6 paf 354: method_name,
1.90 paf 355: "output does not contain CGI header; "
356: "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"",
1.107.2.7 paf 357: execution.status,
1.107.2.14 paf 358: (uint)execution.out->size(), execution.out->cstr().get(),
359: (uint)execution.err->size(), execution.err->cstr().get());
1.98 paf 360: break; //never reached
361: }
362:
363: int header_break_pos;
364: if(unix_header_break) {
365: header_break_pos=unix_pos;
366: eol_marker="\n"; eol_marker_size=1;
367: } else {
368: header_break_pos=dos_pos;
369: eol_marker="\r\n"; eol_marker_size=2;
1.41 parser 370: }
1.21 paf 371:
1.107.2.7 paf 372: header=execution.out->mid(0, header_break_pos);
373: body=execution.out->mid(header_break_pos+eol_marker_size*2, execution.out->size());
1.29 paf 374: }
1.41 parser 375: // body
1.107.2.14 paf 376: self.set(pool, false/*not tainted*/, body->cstr(pool), body->size());
1.94 paf 377:
378: // $fields << header
1.98 paf 379: if(header && eol_marker) {
1.107.2.7 paf 380: ArrayString rows;
1.94 paf 381: header->split(rows, 0, eol_marker, eol_marker_size);
1.107.2.7 paf 382: Pass_cgi_header_attribute_info info;
383: info.pool=&pool;
384: info.charset=&r.charsets.source();
385: info.fields=&self.fields();
1.94 paf 386: rows.for_each(pass_cgi_header_attribute, &info);
387: if(info.content_type)
1.107.2.7 paf 388: self.fields().put(content_type_name, info.content_type);
1.94 paf 389: }
1.21 paf 390:
1.42 parser 391: // $status
1.107.2.7 paf 392: self.fields().put(file_status_name, ValuePtr(new VInt(execution.status)));
1.21 paf 393:
394: // $stderr
1.107.2.7 paf 395: if(execution.err->size())
1.21 paf 396: self.fields().put(
1.107.2.7 paf 397: StringPtr(new String("stderr")),
398: ValuePtr(new VString(execution.err)));
1.21 paf 399: }
1.107.2.13 paf 400: static void _exec(Request& r, StringPtr method_name, MethodParams* params) {
1.41 parser 401: _exec_cgi(r, method_name, params, false);
402: }
1.107.2.13 paf 403: static void _cgi(Request& r, StringPtr method_name, MethodParams* params) {
1.41 parser 404: _exec_cgi(r, method_name, params, true);
405: }
406:
1.107.2.13 paf 407: static void _list(Request& r, StringPtr method_name, MethodParams* params) {
1.47 parser 408: Pool& pool=r.pool();
409:
1.107.2.13 paf 410: ValuePtr relative_path=params->as_no_junction(0, "path must not be code");
1.47 parser 411:
1.107.2.7 paf 412: StringPtr regexp;
1.47 parser 413: pcre *regexp_code;
1.81 paf 414: const int ovecsize=(1/*match*/)*3;
415: int ovector[ovecsize];
1.107.2.13 paf 416: if(params->count()>1) {
417: regexp=params->as_no_junction(1, "regexp must not be code")->as_string(&pool);
1.47 parser 418:
1.107.2.14 paf 419: CharPtr pattern=regexp->cstr();
1.107.2.3 paf 420: const char* errptr;
1.47 parser 421: int erroffset;
422: regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL,
423: &errptr, &erroffset,
1.107.2.7 paf 424: r.charsets.source().pcre_tables);
1.47 parser 425:
426: if(!regexp_code)
1.74 paf 427: throw Exception(0,
1.107.2.7 paf 428: regexp->mid(erroffset, regexp->size()),
1.47 parser 429: "regular expression syntax error - %s", errptr);
430: } else
431: regexp_code=0;
432:
433:
1.107.2.7 paf 434: CharPtr absolute_path_cstr=r.absolute(relative_path->as_string(&pool))->
435: cstr(String::UL_FILE_SPEC);
1.47 parser 436:
1.107.2.7 paf 437: Table::columns_type columns(new ArrayString);
438: *columns+=StringPtr(new String("name"));
439: TablePtr table(new Table(method_name, columns));
1.47 parser 440:
441: LOAD_DIR(absolute_path_cstr,
1.107.2.7 paf 442: const char* file_name_cstr=ffblk.ff_name;
443: size_t file_name_size=strlen(file_name_cstr);
1.47 parser 444: bool suits=true;
445: if(regexp_code) {
446: int exec_result=pcre_exec(regexp_code, 0,
447: ffblk.ff_name, file_name_size, 0,
448: 0, ovector, ovecsize);
449:
450: if(exec_result==PCRE_ERROR_NOMATCH)
451: suits=false;
452: else if(exec_result<0) {
453: (*pcre_free)(regexp_code);
1.74 paf 454: throw Exception(0,
1.47 parser 455: regexp,
456: "regular expression execute (%d)",
457: exec_result);
458: }
459: }
460:
461: if(suits) {
1.107.2.7 paf 462: StringPtr file_name(new String);
463: file_name->APPEND_TAINTED(pool.copy(file_name_cstr, file_name_size), file_name_size,
1.107.2.6 paf 464: method_name->origin().file, method_name->origin().line);
1.47 parser 465:
1.107.2.7 paf 466: Table::element_type row(new ArrayString);
467: *row+=file_name;
468: *table+=row;
1.47 parser 469: }
470: );
471:
472: if(regexp_code)
1.107.2.7 paf 473: pcre_free(regexp_code);
1.47 parser 474:
1.60 parser 475: // write out result
1.107.2.7 paf 476: r.write_no_lang(ValuePtr(new VTable(table)));
1.47 parser 477: }
1.21 paf 478:
1.69 paf 479: #ifndef DOXYGEN
480: struct Lock_execute_body_info {
1.107.2.7 paf 481: Request* r;
482: ValuePtr body_code;
1.69 paf 483: };
484: #endif
1.107.2.7 paf 485: static void lock_execute_body(int , void *ainfo) {
486: Lock_execute_body_info& info=*static_cast<Lock_execute_body_info *>(ainfo);
1.69 paf 487: // execute body
1.107.2.7 paf 488: info.r->write_assign_lang(info.r->process(info.body_code));
1.69 paf 489: };
1.107.2.13 paf 490: static void _lock(Request& r, StringPtr method_name, MethodParams* params) {
1.107.2.7 paf 491: Lock_execute_body_info info;
492: info.r=&r;
1.107.2.13 paf 493: StringPtr file_spec=r.absolute(params->as_string(0, "file name must be string"));
494: info.body_code=params->as_junction(1, "body must be code");
1.69 paf 495:
1.70 paf 496: file_write_action_under_lock(file_spec, "lock", lock_execute_body, &info);
1.69 paf 497: }
498:
1.107.2.3 paf 499: static int lastposafter(const String& s, int after, const char* substr, size_t substr_size, bool beforelast=false) {
1.89 paf 500: size_t size;
501: if(beforelast)
502: size=s.size();
503: int at;
504: while((at=s.pos(substr, substr_size, after))>=0) {
505: size_t newafter=at+substr_size/*skip substr*/;
506: if(beforelast && newafter==size)
507: break;
508: after=newafter;
509: }
510:
511: return after;
512: }
513:
1.107.2.13 paf 514: static void _find(Request& r, StringPtr method_name, MethodParams* params) {
1.90 paf 515: Pool& pool=r.pool();
1.107.2.13 paf 516: StringPtr file_name=params->as_no_junction(0, "file name must not be code")->as_string(&pool);
1.107.2.7 paf 517: StringPtr file_spec;
518: if(file_name->first_char()=='/')
519: file_spec=file_name;
1.90 paf 520: else
1.107.2.7 paf 521: file_spec=r.relative(r.request_info.uri, file_name);
1.90 paf 522:
523: // easy way
1.107.2.7 paf 524: if(file_readable(r.absolute(file_spec))) {
1.96 paf 525: r.write_assign_lang(*file_spec);
1.90 paf 526: return;
527: }
528:
529: // monkey way
530: int after_base_slash=lastposafter(*file_spec, 0, "/", 1);
1.107.2.7 paf 531: StringPtr dirname=file_spec->mid(0, after_base_slash);
532: StringPtr basename=file_spec->mid(after_base_slash, file_spec->size());
1.90 paf 533:
534: int after_monkey_slash;
535: while((after_monkey_slash=lastposafter(*dirname, 0, "/", 1, true))>0) {
1.107.2.7 paf 536: StringPtr test_name(new String);
537: *test_name<<*(dirname=dirname->mid(0, after_monkey_slash));
538: *test_name<<basename;
539: if(file_readable(r.absolute(test_name))) {
540: r.write_assign_lang(*test_name);
1.90 paf 541: return;
542: }
543: }
544:
545: // no way, not found
1.107.2.13 paf 546: if(params->count()==2) {
547: ValuePtr not_found_code=params->as_junction(1, "not-found param must be code");
1.90 paf 548: r.write_pass_lang(r.process(not_found_code));
549: }
550: }
551:
1.107.2.13 paf 552: static void _dirname(Request& r, StringPtr method_name, MethodParams* params) {
553: StringPtr file_spec=params->as_string(0, "file name must be string");
1.89 paf 554: // /a/some.tar.gz > /a
555: // /a/b/ > /a
1.107.2.7 paf 556: int afterslash=lastposafter(*file_spec, 0, "/", 1, true);
1.89 paf 557: if(afterslash>0)
1.107.2.7 paf 558: r.write_assign_lang(*file_spec->mid(0, afterslash==1?1:afterslash-1));
1.89 paf 559: else
1.107.2.7 paf 560: r.write_assign_lang(String(".", 1));
1.89 paf 561: }
562:
1.107.2.13 paf 563: static void _basename(Request& r, StringPtr method_name, MethodParams* params) {
564: StringPtr file_spec=params->as_string(0, "file name must be string");
1.89 paf 565: // /a/some.tar.gz > some.tar.gz
1.107.2.7 paf 566: int afterslash=lastposafter(*file_spec, 0, "/", 1);
567: r.write_assign_lang(*file_spec->mid(afterslash, file_spec->size()));
1.89 paf 568: }
569:
1.107.2.13 paf 570: static void _justname(Request& r, StringPtr method_name, MethodParams* params) {
571: StringPtr file_spec=params->as_string(0, "file name must be string");
1.89 paf 572: // /a/some.tar.gz > some.tar
1.107.2.7 paf 573: int afterslash=lastposafter(*file_spec, 0, "/", 1);
574: int afterdot=lastposafter(*file_spec, afterslash, ".", 1);
575: r.write_assign_lang(*file_spec->mid(afterslash, afterdot!=afterslash?afterdot-1:file_spec->size()));
1.89 paf 576: }
1.107.2.13 paf 577: static void _justext(Request& r, StringPtr method_name, MethodParams* params) {
578: StringPtr file_spec=params->as_string(0, "file name must be string");
1.89 paf 579: // /a/some.tar.gz > gz
1.107.2.7 paf 580: int afterdot=lastposafter(*file_spec, 0, ".", 1);
1.89 paf 581: if(afterdot>0)
1.107.2.7 paf 582: r.write_assign_lang(*file_spec->mid(afterdot, file_spec->size()));
1.89 paf 583: }
584:
1.107.2.13 paf 585: static void _fullpath(Request& r, StringPtr method_name, MethodParams* params) {
586: StringPtr file_spec=params->as_string(0, "file name must be string");
1.107.2.7 paf 587: StringPtr result(new String);
588: if(file_spec->first_char()=='/')
589: result=file_spec;
1.102 paf 590: else {
591: // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
1.107.2.7 paf 592: StringPtr full_disk_path=r.absolute(file_spec);
593: size_t document_root_length=strlen(r.request_info.document_root);
1.106 paf 594:
595: if(document_root_length>0) {
1.107.2.7 paf 596: char last_char=r.request_info.document_root[document_root_length-1];
1.106 paf 597: if(last_char == '/' || last_char == '\\')
598: --document_root_length;
599: }
1.107.2.7 paf 600: result=full_disk_path->mid(document_root_length, full_disk_path->size());
1.102 paf 601: }
602: r.write_assign_lang(*result);
603: }
604:
1.89 paf 605:
1.32 paf 606: // constructor
607:
1.107.2.7 paf 608: MFile::MFile(): Methoded("file") {
1.48 parser 609: // ^save[mode;file-name]
610: add_native_method("save", Method::CT_DYNAMIC, _save, 2, 2);
1.7 paf 611:
612: // ^delete[file-name]
1.32 paf 613: add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.45 parser 614:
615: // ^move[from-file-name;to-file-name]
616: add_native_method("move", Method::CT_STATIC, _move, 2, 2);
1.8 paf 617:
1.48 parser 618: // ^load[mode;disk-name]
619: // ^load[mode;disk-name;user-name]
620: add_native_method("load", Method::CT_DYNAMIC, _load, 2, 3);
1.25 paf 621:
622: // ^stat[disk-name]
1.32 paf 623: add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21 paf 624:
1.36 paf 625: // ^cgi[file-name]
626: // ^cgi[file-name;env hash]
627: // ^cgi[file-name;env hash;1cmd;2line;3ar;4g;5s]
1.41 parser 628: add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+10);
629:
630: // ^exec[file-name]
631: // ^exec[file-name;env hash]
632: // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
633: add_native_method("exec", Method::CT_DYNAMIC, _exec, 1, 2+10);
1.47 parser 634:
635: // ^file:list[path]
636: // ^file:list[path][regexp]
637: add_native_method("list", Method::CT_STATIC, _list, 1, 2);
1.69 paf 638:
639: // ^file:lock[path]{code}
640: add_native_method("lock", Method::CT_STATIC, _lock, 2, 2);
1.90 paf 641:
642: // ^find[file-name]
643: // ^find[file-name]{when-not-found}
644: add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.47 parser 645:
1.89 paf 646: // ^file:dirname[/a/some.tar.gz]=/a
647: // ^file:dirname[/a/b/]=/a
648: add_native_method("dirname", Method::CT_STATIC, _dirname, 1, 1);
649: // ^file:basename[/a/some.tar.gz]=some.tar.gz
650: add_native_method("basename", Method::CT_STATIC, _basename, 1, 1);
651: // ^file:justname[/a/some.tar.gz]=some.tar
652: add_native_method("justname", Method::CT_STATIC, _justname, 1, 1);
653: // ^file:justext[/a/some.tar.gz]=gz
654: add_native_method("justext", Method::CT_STATIC, _justext, 1, 1);
1.102 paf 655: // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
656: add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1);
1.1 paf 657: }
E-mail: