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