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