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