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