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