Annotation of parser3/src/main/pa_request.C, revision 1.453
1.54 paf 1: /** @file
1.55 paf 2: Parser: request class main part. @see compile.C and execute.C.
3:
1.439 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.418 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.218 paf 6: */
1.55 paf 7:
1.69 paf 8: #include "pa_sapi.h"
1.53 paf 9: #include "pa_common.h"
1.372 moko 10: #include "pa_os.h"
1.1 paf 11: #include "pa_request.h"
1.3 paf 12: #include "pa_wwrapper.h"
13: #include "pa_vclass.h"
1.31 paf 14: #include "pa_globals.h"
1.30 paf 15: #include "pa_vint.h"
1.297 misha 16: #include "pa_vmethod_frame.h"
1.32 paf 17: #include "pa_types.h"
1.248 paf 18: #include "pa_venv.h"
19: #include "pa_vmath.h"
20: #include "pa_vstatus.h"
21: #include "pa_vrequest.h"
1.78 paf 22: #include "pa_vtable.h"
1.90 paf 23: #include "pa_vfile.h"
1.147 parser 24: #include "pa_dictionary.h"
1.208 paf 25: #include "pa_charset.h"
1.186 paf 26: #include "pa_charsets.h"
1.248 paf 27: #include "pa_cache_managers.h"
1.402 moko 28: #include "pa_http.h"
1.248 paf 29: #include "pa_vmail.h"
30: #include "pa_vform.h"
31: #include "pa_vcookie.h"
32: #include "pa_vresponse.h"
33: #include "pa_vmemory.h"
1.253 paf 34: #include "pa_vconsole.h"
1.267 paf 35: #include "pa_vdate.h"
1.424 moko 36: #include "pa_varray.h"
1.159 parser 37:
1.453 ! moko 38: volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.452 2026/09/28 02:52:42 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H;
1.329 moko 39:
1.248 paf 40: // consts
41:
1.269 paf 42: #define UNHANDLED_EXCEPTION_METHOD_NAME "unhandled_exception"
1.207 paf 43:
1.82 paf 44: /// content type of response when no $MAIN:defaults.content-type defined
1.248 paf 45: const char* DEFAULT_CONTENT_TYPE="text/html";
46:
1.449 moko 47: // One recursion level uses ~0.85KB of stack under linux/gcc, ~1KB under win32 and ~2KB under win64.
48: // MSVC win64 exception handling requires an extra ~10KB !!! for each throw, and execute() catches and
49: // rethrows on every level to provide a stack trace.
1.450 moko 50: // So 1000 levels need ~1MB under linux (default 8MB stack is enough) and ~2+10=12MB under win64.
51: // Worse, win64 keeps the unwound frames until the catch block returns, so a handler running away
52: // in its turn starts on top of them and needs one more such budget. The stack is set to 40MB in
53: // the MSVC project settings to allow nested three runaways.
1.449 moko 54: const int EXECUTE_RECURSION_LIMIT=1000;
55: const int EXECUTE_RECURSION_RESERVE=100;
56:
1.446 moko 57: const int LOOP_LIMIT=100000;
58: const int ARRAY_LIMIT=1000000;
59: const int HTTPD_TIMEOUT=4;
1.369 moko 60: const size_t FILE_SIZE_LIMIT=512*1024*1024;
1.361 moko 61:
1.248 paf 62: // defines for globals
63:
64: #define MAIN_METHOD_NAME "main"
65: #define AUTO_METHOD_NAME "auto"
1.364 moko 66: #define USE_METHOD_NAME "use"
1.309 misha 67: #define AUTOUSE_METHOD_NAME "autouse"
1.364 moko 68:
69: #define ORIGIN_KEY "origin"
70:
1.248 paf 71: // globals
72:
73: const String main_method_name(MAIN_METHOD_NAME);
74: const String auto_method_name(AUTO_METHOD_NAME);
1.364 moko 75: static const String use_method_name(USE_METHOD_NAME);
76: static const String autouse_method_name(AUTOUSE_METHOD_NAME);
1.311 misha 77:
1.364 moko 78: static const String origin_key(ORIGIN_KEY);
79:
1.369 moko 80: int pa_loop_limit=LOOP_LIMIT;
1.425 moko 81: int pa_array_limit=ARRAY_LIMIT;
1.427 moko 82: int pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT;
1.390 moko 83: int pa_httpd_timeout=HTTPD_TIMEOUT;
1.369 moko 84: size_t pa_file_size_limit=FILE_SIZE_LIMIT;
1.361 moko 85:
1.248 paf 86: // defines for statics
87:
88: #define MIME_TYPES_NAME "MIME-TYPES"
89: #define CLASS_PATH_NAME "CLASS_PATH"
90:
1.344 moko 91: #define DOWNLOAD_NAME_UPPER "DOWNLOAD"
92: #define BODY_NAME_UPPER "BODY"
93:
1.248 paf 94: // statics
95:
96: static const String main_class_name(MAIN_CLASS_NAME);
97: static const String mime_types_name(MIME_TYPES_NAME);
1.395 moko 98: static const String class_path_name(CLASS_PATH_NAME);
1.361 moko 99:
1.395 moko 100: static const String charsets_name("CHARSETS");
101: static const String strict_vars_name("STRICT-VARS");
102: static const String prototype_name("OBJECT-PROTOTYPE");
1.415 moko 103: static const String getter_protected_name("CLASS-GETTER-PROTECTED");
104: static const String locals_name("LOCALS");
1.395 moko 105: static const String limits_name("LIMITS");
106: static const String loop_limit_name("max_loop");
1.425 moko 107: static const String array_limit_name("max_array_size");
1.427 moko 108: static const String recursion_limit_name("max_recursion");
1.395 moko 109: static const String file_size_limit_name("max_file_size");
110: static const String lock_wait_timeout_name("lock_wait_timeout");
111: static const String httpd_name("HTTPD");
112: static const String httpd_timeout_name("timeout");
1.402 moko 113: static const String httpd_mode_name("mode");
1.395 moko 114:
115: static const String conf_method_name("conf");
116: static const String post_process_method_name("postprocess");
117: static const String response_body_file_name("file");
1.248 paf 118:
1.344 moko 119: static const String download_name_upper(DOWNLOAD_NAME_UPPER);
120: static const String body_name_upper(BODY_NAME_UPPER);
121:
122: // more static
123:
124: static const String content_type_name_upper(HTTP_CONTENT_TYPE_UPPER);
125: static const String content_disposition_name_upper(CONTENT_DISPOSITION_UPPER);
1.392 moko 126: static const String content_disposition_inline(CONTENT_DISPOSITION_INLINE);
1.344 moko 127: static const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT);
128:
1.420 moko 129: const String content_disposition_filename_name(CONTENT_DISPOSITION_FILENAME_NAME);
130: const String content_disposition_filename_name_asterisk("filename*");
131:
1.248 paf 132: // defines
1.82 paf 133:
1.344 moko 134: #define CHARSET_NAME_UPPER "CHARSET"
135: #define LAST_MODIFIED_NAME_UPPER "LAST-MODIFIED"
136:
1.233 paf 137: // op.C
1.248 paf 138: VStateless_class& VClassMAIN_create();
1.123 paf 139:
1.157 parser 140: //
1.445 moko 141: void Request::recursion_limit_reached() {
142: if(execute_recursion_limit>pa_execute_recursion_limit)
143: throw Recursion_limit_exception();
144:
145: execute_recursion_limit+=EXECUTE_RECURSION_RESERVE;
146: throw Exception(PARSER_RUNTIME, 0, "call canceled - endless recursion detected");
147: }
148:
1.408 moko 149: Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, String::Language adefault_lang):
1.248 paf 150: // private
1.427 moko 151: anti_endless_execute_recursion(0),
1.445 moko 152: execute_recursion_limit(pa_execute_recursion_limit),
1.248 paf 153:
154: // public
1.335 moko 155: allow_class_replace(false),
1.248 paf 156: method_frame(0),
157: rcontext(0),
158: wcontext(0),
159: flang(adefault_lang),
1.191 paf 160: fconnection(0),
1.371 moko 161: fin_cycle(0),
1.307 misha 162: fskip(SKIP_NOTHING),
1.248 paf 163:
164: // public
1.257 paf 165: request_info(arequest_info),
1.248 paf 166: sapi_info(asapi_info),
1.359 moko 167: charsets(pa_UTF8_charset, pa_UTF8_charset, pa_UTF8_charset), // default charsets
1.248 paf 168:
169: main_class(VClassMAIN_create()),
1.260 paf 170: form(*new VForm(charsets, arequest_info)),
1.248 paf 171: mail(*new VMail),
172: response(*new VResponse(arequest_info, charsets)),
1.298 misha 173: cookie(*new VCookie(charsets, arequest_info)),
1.285 misha 174: console(*new VConsole),
1.248 paf 175:
176: // private
177: configure_admin_done(false),
178:
179: // private defaults
180: fdefault_lang(adefault_lang),
181: // private mime types
182: mime_types(0)
1.157 parser 183: {
1.261 paf 184: pa_register_thread_request(*this);
185:
1.248 paf 186: // file_no=0 => unknown
1.434 moko 187: file_list+=String::Body("UNKNOWN");
188: file_list+=String::Body("-body of process-"); // pseudo_file_no__process
1.186 paf 189:
1.178 paf 190: // maybe expire old caches
1.264 paf 191: cache_managers->maybe_expire();
1.178 paf 192:
1.157 parser 193: /// directly used
1.233 paf 194: // MAIN class, operators
1.353 moko 195: put_class(&main_class);
1.157 parser 196: // classes:
197: // table, file, random, mail, image, ...
1.248 paf 198: methoded_array().register_directly_used(*this);
1.157 parser 199:
200: /// methodless
1.354 moko 201:
1.157 parser 202: // env class
1.353 moko 203: put_class(new VEnv(asapi_info));
1.175 paf 204: // status class
1.353 moko 205: put_class(new VStatus());
1.157 parser 206: // request class
1.353 moko 207: put_class(new VRequest(arequest_info, charsets, form, asapi_info));
1.157 parser 208: // cookie class
1.353 moko 209: put_class(&cookie);
1.253 paf 210: // console class
1.353 moko 211: put_class(&console);
1.354 moko 212:
1.157 parser 213: /// methoded
1.354 moko 214:
1.157 parser 215: // response class
1.353 moko 216: put_class(&response);
1.157 parser 217: // form class
1.353 moko 218: put_class(&form);
1.213 paf 219: // mail class
1.353 moko 220: put_class(&mail);
1.157 parser 221: // math class
1.353 moko 222: put_class(new VMath);
1.248 paf 223: // memory class
1.353 moko 224: put_class(new VMemory);
1.117 paf 225: }
1.192 paf 226:
227: Request::~Request() {
228: #ifdef XML
229: // if for some strange reason xml generic errors failed to be reported, free them up
1.248 paf 230: if(const char* xml_generic_errors=xmlGenericErrors()) {
231: SAPI::log(sapi_info, "warning: unreported xmlGenericErrors: %s", xml_generic_errors);
1.341 moko 232: pa_free((void *)xml_generic_errors);
1.192 paf 233: }
234: #endif
235: }
1.236 paf 236:
1.248 paf 237: Value& Request::get_self() { return method_frame/*always have!*/->self(); }
1.192 paf 238:
1.355 moko 239: VStateless_class* Request::get_class(const String& name){
240: VStateless_class* result=classes().get(name);
1.313 misha 241: if(!result)
1.375 moko 242: if(const Method *method=main_class.get_element_method(autouse_method_name)){
1.364 moko 243: Value *vname=new VString(name);
244: CONSTRUCTOR_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
245: frame.store_params(&vname, 1);
246: // we don't need the result
247: call(frame);
248: });
249: result=classes().get(name);
250: }
1.309 misha 251: return result;
252: }
253:
1.411 moko 254: VStateless_class& Request::get_class_ref(const String& name){
255: VStateless_class* result=get_class(name);
256: if(!result)
257: throw Exception(PARSER_RUNTIME, &name, "class is undefined");
258: return *result;
259: }
260:
1.424 moko 261: bool Request::add_class(const char* atype, VStateless_class *aclass){
1.436 moko 262: if(!classes().put_dont_replace(String::Body(atype), aclass))
263: return true;
1.437 moko 264: if(!allow_class_replace && strcmp(atype, VARRAY_TYPE))
265: return false;
1.436 moko 266: classes().remove(String::Body(atype));
1.424 moko 267: classes().put(atype, aclass);
268: return true;
269: }
270:
1.356 moko 271: static void load_charset(HashStringValue::key_type akey, HashStringValue::value_type avalue, Request_charsets* charsets) {
1.359 moko 272: pa_charsets.load_charset(*charsets, akey, avalue->as_string());
1.208 paf 273: }
1.356 moko 274:
1.395 moko 275:
276: #define CONF_OPTION(config, name, code, exception_name) \
277: if(config) \
278: if(Value* option=config->get_element(name)) { \
279: if(option->is_evaluated_expr()) { \
280: code; \
281: } else \
1.415 moko 282: throw Exception(PARSER_RUNTIME, 0, "$MAIN:" exception_name, name.cstr()); \
1.395 moko 283: }
284:
1.248 paf 285: void Request::configure_admin(VStateless_class& conf_class) {
1.208 paf 286: if(configure_admin_done)
1.356 moko 287: throw Exception(PARSER_RUNTIME, 0, "parser already configured");
1.208 paf 288: configure_admin_done=true;
289:
1.227 paf 290: // charsets must only be specified in method_frame config
1.208 paf 291: // so that users would not interfere
292:
293: /* $MAIN:CHARSETS[
294: $.charsetname1[/full/path/to/charset/file.cfg]
295: ...
296: ]
297: */
1.308 misha 298: if(Value* vcharsets=conf_class.get_element(charsets_name)) {
1.342 moko 299: if(!vcharsets->is_string()) {
1.248 paf 300: if(HashStringValue* charsets=vcharsets->get_hash())
1.281 paf 301: charsets->for_each<Request_charsets*>(load_charset, &this->charsets);
1.240 paf 302: else
1.415 moko 303: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be hash", charsets_name.cstr());
1.342 moko 304: }
1.208 paf 305: }
306:
1.330 moko 307: #ifdef STRICT_VARS
1.332 moko 308: VVoid::strict_vars=false;
1.330 moko 309: if(Value* strict_vars=conf_class.get_element(strict_vars_name)) {
310: if(strict_vars->is_bool())
311: VVoid::strict_vars=strict_vars->as_bool();
1.357 moko 312: else
1.415 moko 313: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", strict_vars_name.cstr());
1.357 moko 314: }
315: #endif
316:
317: #ifdef OBJECT_PROTOTYPE
318: VClass::prototype=true;
319: if(Value* prototype=conf_class.get_element(prototype_name)) {
320: if(prototype->is_bool())
321: VClass::prototype=prototype->as_bool();
322: else
1.415 moko 323: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", prototype_name.cstr());
1.330 moko 324: }
325: #endif
326:
1.415 moko 327: #ifdef CLASS_GETTER_UNPROTECTED
328: VClass::getter_protected=true;
329: if(Value* getter_protected=conf_class.get_element(getter_protected_name)) {
330: if(getter_protected->is_bool())
331: VClass::getter_protected=getter_protected->as_bool();
332: else
333: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", getter_protected_name.cstr());
334: }
335: #endif
336:
337: VStateless_class::gall_vars_local=false;
338: if(Value* locals=conf_class.get_element(locals_name)) {
339: if(locals->is_bool()){
340: VStateless_class::gall_vars_local=locals->as_bool();
341: main_class.set_all_vars_local();
342: } else
343: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", locals_name.cstr());
344: }
345:
1.362 moko 346: Value* limits=conf_class.get_element(limits_name);
347:
1.361 moko 348: pa_loop_limit=LOOP_LIMIT;
1.395 moko 349: CONF_OPTION(limits, loop_limit_name, {
350: pa_loop_limit=option->as_int();
351: if(pa_loop_limit==0) pa_loop_limit=INT_MAX;
352: }, "LIMITS.%s must be int");
1.361 moko 353:
1.425 moko 354: pa_array_limit=ARRAY_LIMIT;
355: CONF_OPTION(limits, array_limit_name, {
356: pa_array_limit=option->as_int();
357: if(pa_array_limit==0) pa_array_limit=INT_MAX;
358: }, "LIMITS.%s must be int");
359:
1.427 moko 360: pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT;
361: CONF_OPTION(limits, recursion_limit_name, {
362: pa_execute_recursion_limit=option->as_int();
1.445 moko 363: if(pa_execute_recursion_limit<=0) pa_execute_recursion_limit=INT_MAX;
364: pa_execute_recursion_limit=min(pa_execute_recursion_limit, INT_MAX-EXECUTE_RECURSION_RESERVE);
1.395 moko 365: }, "LIMITS.%s must be int");
1.445 moko 366: execute_recursion_limit=pa_execute_recursion_limit;
1.390 moko 367:
1.369 moko 368: pa_file_size_limit=FILE_SIZE_LIMIT;
1.395 moko 369: CONF_OPTION(limits, file_size_limit_name, {
370: double limit=option->as_double();
371: if(limit >= (double)SSIZE_MAX)
1.448 moko 372: throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %s", file_size_limit_name.cstr(), pa_itoa(SSIZE_MAX));
1.395 moko 373: pa_file_size_limit=(size_t)limit;
374: if(pa_file_size_limit==0)
375: pa_file_size_limit=SSIZE_MAX;
376: }, "LIMITS.%s must be number");
1.361 moko 377:
1.372 moko 378: pa_lock_attempts=PA_LOCK_ATTEMPTS;
1.395 moko 379: CONF_OPTION(limits, lock_wait_timeout_name, {
380: double limit=option->as_double();
381: if(limit >= 3600*24)
1.426 moko 382: throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %d", lock_wait_timeout_name.cstr(), 3600*24);
1.395 moko 383: pa_lock_attempts=(unsigned int)(limit*2)+1;
384: }, "LIMITS.%s must be number");
385:
386: Value* httpd=conf_class.get_element(httpd_name);
387:
388: pa_httpd_timeout=HTTPD_TIMEOUT;
389: CONF_OPTION(httpd, httpd_timeout_name, {
390: pa_httpd_timeout=option->as_int();
391: if(pa_httpd_timeout==0) pa_httpd_timeout=INT_MAX;
392: }, "HTTPD.%s must be int");
1.372 moko 393:
1.403 moko 394: if(httpd)
395: if(Value* option=httpd->get_element( httpd_mode_name)) {
396: if(option->get_junction())
1.415 moko 397: throw Exception(PARSER_RUNTIME, 0, "$MAIN:HTTPD:mode must be string");
1.403 moko 398: HTTPD_Server::set_mode(option->as_string());
399: }
1.402 moko 400:
1.227 paf 401: // configure method_frame options
1.208 paf 402: // until someone with less privileges have overriden them
1.248 paf 403: methoded_array().configure_admin(*this);
1.208 paf 404: }
1.150 parser 405:
1.442 moko 406: const char* Request::get_exception_cstr(const Exception& e, VException& details) {
1.349 moko 407:
1.269 paf 408: #define PA_URI_FORMAT "%s: "
1.349 moko 409: #define PA_COMMENT_TYPE_FORMAT "%s [%s]"
410:
1.269 paf 411: #define PA_ORIGIN_FILE_POS_FORMAT "%s(%d:%d): "
412: #define PA_SOURCE_FORMAT "'%s' "
1.349 moko 413:
414: #define PA_ORIGIN_FILE_POS_VALUE file_list[details.origin.file_no].cstr(), 1+details.origin.line, 1+details.origin.col,
415: #define PA_SOURCE_VALUE details.problem_source->cstr(),
416:
417: #define EXCEPTION_CSTR(f1,v1,f2,v2) \
418: snprintf(result, MAX_STRING, \
419: PA_URI_FORMAT \
420: f1 f2 \
421: PA_COMMENT_TYPE_FORMAT, \
422: request_info.uri, \
423: v1 v2 \
424: e.comment(), e.type() \
425: );
426:
1.269 paf 427: char* result=new(PointerFreeGC) char[MAX_STRING];
428:
429: if(details.problem_source) { // do we know the guy?
1.349 moko 430: if(details.origin.file_no) // do whe know where he came from?
431: EXCEPTION_CSTR(PA_ORIGIN_FILE_POS_FORMAT, PA_ORIGIN_FILE_POS_VALUE, PA_SOURCE_FORMAT, PA_SOURCE_VALUE)
432: else
433: EXCEPTION_CSTR(PA_SOURCE_FORMAT, PA_SOURCE_VALUE,,)
434: } else {
435: if(details.origin.file_no) // do whe know where he came from?
436: EXCEPTION_CSTR(PA_ORIGIN_FILE_POS_FORMAT, PA_ORIGIN_FILE_POS_VALUE,,)
437: else
438: EXCEPTION_CSTR(,,,)
439: }
1.269 paf 440:
441: return result;
442: }
1.272 paf 443:
1.444 moko 444: Table &Request::Exception_trace::table(Request &r, const String* problem_source, size_t bottom, size_t top){
1.407 moko 445: // $stack[^table::create{name file lineno colno}]
446: Table::columns_type stack_trace_columns(new ArrayString);
447: *stack_trace_columns+=new String("name");
448: *stack_trace_columns+=new String("file");
449: *stack_trace_columns+=new String("lineno");
450: *stack_trace_columns+=new String("colno");
451: Table& stack_trace=*new Table(stack_trace_columns);
452:
1.444 moko 453: if(bottom<top)
454: for(size_t i=bottom+(get(bottom).name()==problem_source); i<top; i++) {
1.407 moko 455: Trace trace=get(i);
456: Table::element_type row(new ArrayString);
457:
458: *row+=trace.name(); // name column
459: Operation::Origin origin=trace.origin();
460: if(origin.file_no) {
461: *row+=new String(r.file_list[origin.file_no], String::L_TAINTED); // 'file' column
1.422 moko 462: *row+=new String(pa_uitoa(1+origin.line), String::L_CLEAN); // 'lineno' column
463: *row+=new String(pa_uitoa(1+origin.col), String::L_CLEAN); // 'colno' column
1.407 moko 464: }
465: stack_trace+=row;
466: }
467:
468: return stack_trace;
469: }
470:
1.272 paf 471: void Request::configure() {
472: // configure admin options if not configured yet
473: if(!configure_admin_done)
474: configure_admin(main_class);
475:
476: // $MAIN:MIME-TYPES
1.308 misha 477: if(Value* element=main_class.get_element(mime_types_name))
1.272 paf 478: if(Table *table=element->get_table())
1.395 moko 479: mime_types=table;
1.272 paf 480: }
1.408 moko 481:
1.64 paf 482: /**
483: load MAIN class, execute @main.
484: MAIN class consists of all the auto.p files we'd manage to find
485: plus
486: the file user requested us to process
487: all located classes become children of one another,
488: composing class we name 'MAIN'
1.180 paf 489:
490: @test log stack trace
491:
1.64 paf 492: */
1.412 moko 493: void Request::core(const char* config_filespec, bool header_only, const String &amain_method_name, const String* amain_class_name) {
1.169 parser 494: try {
1.211 paf 495: // loading config
1.384 moko 496: if(config_filespec)
1.387 moko 497: use_file_directly(*new String(config_filespec));
1.8 paf 498:
1.338 moko 499: // filling mail received
500: mail.fill_received(*this);
501:
1.272 paf 502: try {
503: // compile requested file
1.389 moko 504: if(request_info.path_translated)
505: use_file_directly(*new String(request_info.path_translated, String::L_TAINTED), true, true /* load auto.p files */);
1.272 paf 506: configure();
507: } catch(...) {
508: configure(); // configure anyway, useful in @unhandled_exception [say, if they would want to mail by SMTP something]
509: rethrow;
510: }
1.25 paf 511:
1.412 moko 512: VStateless_class& main = amain_class_name ? get_class_ref(*amain_class_name) : main_class;
1.25 paf 513: // execute @main[]
1.412 moko 514: const String* body_string=amain_method_name.is_empty() ? &String::Empty : execute_method(main, amain_method_name);
1.41 paf 515: if(!body_string)
1.412 moko 516: throw Exception(PARSER_RUNTIME, &amain_method_name, "method not found in class %s", main.type());
1.79 paf 517:
1.250 paf 518: // extract response body
1.344 moko 519: Value* body_value=response.fields().get(download_name_upper); // $response:download?
1.430 moko 520: bool as_attachment=body_value!=0;
1.250 paf 521: if(!body_value)
1.344 moko 522: body_value=response.fields().get(body_name_upper); // $response:body
1.250 paf 523: if(!body_value)
524: body_value=new VString(*body_string); // just result of ^main[]
525:
1.119 paf 526: // @postprocess
1.383 moko 527: if(const Method *method=main_class.get_method(post_process_method_name)) {
528: METHOD_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
529: frame.store_params(&body_value, 1);
530: call(frame);
531: body_value=&frame.result();
532: });
533: }
1.90 paf 534:
1.430 moko 535: // can throw exceptions while handling $response:download[]
536: output_result(body_value->as_vfile(flang, &charsets), header_only, as_attachment);
1.171 parser 537: } catch(const Exception& e) { // request handling problem
1.258 paf 538:
1.445 moko 539: // OK, as native execution stack has unwound
540: anti_endless_execute_recursion=0;
541:
1.431 moko 542: // we're returning not result, but error explanation
1.442 moko 543: VException& details=*new VException(*this, e);
1.431 moko 544: const char* exception_cstr=get_exception_cstr(e, details);
545:
546: // reset language to default
547: flang=fdefault_lang;
548: // reset response
549: response.fields().clear();
550: SAPI::clear_headers(sapi_info);
1.407 moko 551:
1.431 moko 552: // this is what we'd return in $response:body
553: const String* body_string=0;
1.407 moko 554:
1.431 moko 555: try {
1.407 moko 556: // maybe we'd be lucky enough as to report an error in a gracefull way...
557: if(const Method *method=main_class.get_method(*new String(UNHANDLED_EXCEPTION_METHOD_NAME))) {
558: // preparing parameters to @unhandled_exception[exception;stack]
559:
1.443 moko 560: Table& stack_trace=exception_trace.table(*this, details.problem_source);
1.444 moko 561: details.expire_trace();
1.407 moko 562: exception_trace.clear(); // forget all about previous life, in case there would be error inside of this method, error handled would not be mislead by old stack contents (see extract_origin)
563:
1.442 moko 564: Value *params[]={&details, new VTable(&stack_trace)};
1.407 moko 565: METHOD_FRAME_ACTION(*method, 0 /*no caller*/, main_class, {
566: frame.store_params(params, 2);
567: call(frame);
568: body_string=&frame.result().as_string();
569: });
570: }
1.383 moko 571:
1.407 moko 572: // conditionally log it
1.442 moko 573: Value* vhandled=details.handled();
1.407 moko 574: if(!vhandled || !vhandled->as_bool()) {
575: SAPI::log(sapi_info, "%s", exception_cstr);
576: }
1.431 moko 577: } catch(const Exception& e) { // exception in @unhandled_exception
1.442 moko 578: VException& details=*new VException(*this, e);
1.431 moko 579: // logging both initial and new exceptions
580: SAPI::log(sapi_info, "%s", exception_cstr);
581: SAPI::log(sapi_info, "Exception in @unhandled_exception at %s", get_exception_cstr(e, details));
582: SAPI::send_error(sapi_info, "Exception in @unhandled_exception, details are available in Parser error log.");
583: return;
584: }
1.383 moko 585:
1.431 moko 586: if(body_string) { // could report an error beautifully?
587: VString body_vstring(*body_string);
588: output_result(body_vstring.as_vfile(flang, &charsets), header_only, false);
589: } else {
590: // doing that ugly
591: SAPI::send_error(sapi_info, exception_cstr, strcmp(e.type(), "file.missing") ? "500" : "404");
1.268 paf 592: }
1.1 paf 593: }
594: }
595:
1.266 paf 596: uint Request::register_file(String::Body file_spec) {
1.248 paf 597: file_list+=file_spec;
598: return file_list.count()-1;
599: }
600:
1.387 moko 601: void Request::use_file_directly(const String& file_spec, bool fail_on_file_absence, bool with_auto_p) {
1.73 paf 602: // cyclic dependence check
1.318 misha 603: if(used_files.get(file_spec))
1.248 paf 604: return;
1.318 misha 605: used_files.put(file_spec, true);
606:
1.385 moko 607: if(!fail_on_file_absence && !entry_exists(file_spec)) // ignore file absence if asked for
608: return;
1.318 misha 609:
1.386 moko 610: if(with_auto_p) {
611: // loading auto.p files from document_root/..
612: // to the one beside requested file.
613: // all assigned bases from upper dir
614: const char* target=file_spec.cstr();
615:
616: // all relative paths are calculated from main document
617: request_info.path_translated=target;
618:
619: const char* after=target;
620: size_t drlen=strlen(request_info.document_root);
621: if(memcmp(after, request_info.document_root, drlen)==0) {
622: after+=drlen;
623: if(after[-1]=='/')
624: --after;
625: }
626:
627: while(const char* before=strchr(after, '/')) {
628: String& sfile_spec=*new String;
629: if(after!=target) {
630: sfile_spec.append_strdup(target, before-target, String::L_CLEAN);
631: sfile_spec << "/" AUTO_FILE_NAME;
632:
1.387 moko 633: use_file_directly(sfile_spec, false /*ignore absence, sole user*/);
1.386 moko 634: }
635: for(after=before+1;*after=='/';after++);
636: }
637: }
638:
1.385 moko 639: if(const char* source=file_read_text(charsets, file_spec, true))
1.387 moko 640: use_buf(main_class, source, 0, register_file(file_spec));
1.318 misha 641: }
642:
643:
1.387 moko 644: void Request::use_file(const String& file_name, const String* use_filespec/*absolute*/, bool with_auto_p) {
1.326 misha 645: if(file_name.is_empty())
1.366 moko 646: throw Exception(PARSER_RUNTIME, 0, "usage failed - no filename was specified");
1.326 misha 647:
1.318 misha 648: const String* filespec=0;
649:
1.453 ! moko 650: if(file_name.first_char()=='/' || path_scheme(file_name) || is_os_absolute_path(file_name)) // path is not relative, no need to scan MAIN:CLASS_PATH
1.388 moko 651: filespec=&full_disk_path(file_name);
1.318 misha 652: else if(use_filespec){ // search in current dir first
1.331 misha 653: size_t last_slash_pos=use_filespec->strrpbrk("/");
654: if(last_slash_pos!=STRING_NOT_FOUND)
655: filespec=file_exist(use_filespec->mid(0, last_slash_pos), file_name); // found in current dir?
1.318 misha 656: }
657:
658: if(!filespec){
659: // prevent multiple scan CLASS_PATH for searching one file
660: if(searched_along_class_path.get(file_name))
661: return;
662: searched_along_class_path.put(file_name, true);
1.308 misha 663: if(Value* element=main_class.get_element(class_path_name)) {
1.233 paf 664: if(element->is_string()) {
1.388 moko 665: filespec=file_exist(full_disk_path(element->as_string()), file_name); // found at class_path?
1.233 paf 666: } else if(Table *table=element->get_table()) {
1.318 misha 667: for(size_t i=table->count(); i--; ) {
1.248 paf 668: const String& path=*(*table->get(i))[0];
1.388 moko 669: if(filespec=file_exist(full_disk_path(path), file_name))
1.233 paf 670: break; // found along class_path
671: }
672: } else
1.366 moko 673: throw Exception(PARSER_RUNTIME, 0, "$" CLASS_PATH_NAME " must be string or table");
1.318 misha 674: if(!filespec)
1.415 moko 675: throw Exception(PARSER_RUNTIME, &file_name, "not found along $MAIN:" CLASS_PATH_NAME);
1.318 misha 676: } else
1.415 moko 677: throw Exception(PARSER_RUNTIME, &file_name, "usage failed - no $MAIN:" CLASS_PATH_NAME " were specified");
1.148 parser 678: }
1.230 paf 679:
1.387 moko 680: use_file_directly(*filespec, true, with_auto_p);
1.16 paf 681: }
682:
1.364 moko 683: void Request::use_file(const String& file_name, const String* use_filespec/*absolute*/, Operation::Origin origin) {
1.349 moko 684: static String use("USE");
685: try {
1.364 moko 686: static VHash* voptions=new VHash();
1.376 moko 687: if(const Method *method=main_class.get_method(use_method_name)){
1.364 moko 688: Value *params[]={new VString(file_name), voptions};
689: voptions->hash().put(origin_key, new VString(*use_filespec));
690:
691: CONSTRUCTOR_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
692: frame.store_params(params, 2);
693: // we don't need the result
694: call(frame);
695: });
696: }
1.349 moko 697: } catch (...) {
698: exception_trace.push(Trace(&use, origin));
699: rethrow;
700: }
701: }
1.208 paf 702:
1.358 moko 703: void Request::use_buf(VStateless_class& aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset) {
704: // temporary zero @conf to avoid it second execution
1.248 paf 705: Temp_method temp_method_conf(aclass, conf_method_name, 0);
1.358 moko 706: // temporary zero @auto to avoid it second execution
1.248 paf 707: Temp_method temp_method_auto(aclass, auto_method_name, 0);
1.233 paf 708:
1.295 misha 709: // compile loaded classes
710: ArrayClass& cclasses=compile(&aclass, source, main_alias, file_no, line_no_offset);
1.212 paf 711:
1.248 paf 712: VString* vfilespec=
1.435 moko 713: new VString(file_list[file_no]);
1.295 misha 714:
715: for(size_t i=0; i<cclasses.count(); i++){
716: VStateless_class& cclass=*cclasses.get(i);
1.296 misha 717:
718: // locate and execute possible @conf[] static
1.382 moko 719: if(execute_method_if_exists(cclass, conf_method_name, vfilespec))
1.295 misha 720: configure_admin(cclass/*, executed.method->name*/);
721:
722: // locate and execute possible @auto[] static
1.419 moko 723: execute_auto_method_if_exists(cclass, auto_method_name, vfilespec);
1.336 moko 724:
725: cclass.enable_default_setter();
1.295 misha 726: }
1.19 paf 727: }
728:
1.248 paf 729: const String& Request::relative(const char* apath, const String& relative_name) {
1.452 moko 730: // apath is a URI or a path already normalized by the target, always slash separated
1.341 moko 731: char *hpath=pa_strdup(apath);
1.248 paf 732: String& result=*new String;
733: if(rsplit(hpath, '/')) // if something/splitted
1.217 paf 734: result << hpath << "/";
1.248 paf 735: result << relative_name;
736: return result;
1.19 paf 737: }
738:
1.453 ! moko 739: const String& Request::full_disk_path(const String& relative_name, uint allowed) {
1.217 paf 740: if(relative_name.first_char()=='/') {
1.341 moko 741: String& result=*new String(pa_strdup(request_info.document_root));
1.104 paf 742: result << relative_name;
1.19 paf 743: return result;
1.389 moko 744: }
1.453 ! moko 745: if(uint scheme=path_scheme(relative_name)) {
! 746: check_path_scheme(scheme, relative_name, allowed);
! 747: if(scheme!=PA_SCHEME_FILE)
! 748: return relative_name; // an allowed uri is passed through
! 749: if(const String* path=file_uri_to_path(relative_name))
! 750: return *path;
! 751: throw Exception(PARSER_RUNTIME, &relative_name, "must be file:///path or file://localhost/path");
! 752: }
1.451 moko 753: if(is_os_absolute_path(relative_name))
754: return relative_name;
1.389 moko 755:
756: return relative(request_info.path_translated ? request_info.path_translated : request_info.document_root, relative_name);
1.1 paf 757: }
1.45 paf 758:
1.453 ! moko 759: const String* Request::real_disk_path(const String& name) {
! 760: switch(path_scheme(name)) {
! 761: case PA_SCHEME_NONE: return &full_disk_path(name);
! 762: case PA_SCHEME_FILE: return file_uri_to_path(name);
! 763: default: return 0; // not a disk path
! 764: }
! 765: }
! 766:
1.267 paf 767: #ifndef DOXYGEN
768: class Add_header_attribute_info
769: {
770: public:
771: Add_header_attribute_info(Request& ar) : r(ar), add_last_modified(true) {}
772: Request& r;
773: bool add_last_modified;
774: };
775: #endif
1.344 moko 776: static void add_header_attribute(HashStringValue::key_type name, HashStringValue::value_type value, Add_header_attribute_info* info) {
777: if(name==BODY_NAME_UPPER || name==DOWNLOAD_NAME_UPPER || name==CHARSET_NAME_UPPER)
1.101 paf 778: return;
1.290 misha 779:
1.345 moko 780: if(name==LAST_MODIFIED_NAME_UPPER)
781: info->add_last_modified=false;
782:
1.316 misha 783: const char* aname=String(name, String::L_URI).untaint_and_transcode_cstr(String::L_URI, &info->r.charsets);
1.101 paf 784:
1.267 paf 785: SAPI::add_header_attribute(info->r.sapi_info,
1.345 moko 786: aname,
1.316 misha 787: attributed_meaning_to_string(*value, String::L_URI, false).untaint_and_transcode_cstr(String::L_URI, &info->r.charsets)
1.303 misha 788: );
1.267 paf 789: }
790:
1.366 moko 791: static void output_sole_piece(Request& r, bool header_only, VFile& body_file, Value* body_file_content_type) {
1.267 paf 792: // transcode text body when "text/*" or simple result
793: String::C output(body_file.value_ptr(), body_file.value_size());
794: if(!body_file_content_type/*vstring.as_vfile*/ || body_file_content_type->as_string().pos("text/")==0)
1.366 moko 795: output=Charset::transcode(output, r.charsets.source(), r.charsets.client());
1.267 paf 796:
1.311 misha 797: // prepare header: Content-Length
1.428 moko 798: SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, pa_uitoa(output.length));
1.267 paf 799:
800: // send header
1.430 moko 801: SAPI::send_headers(r.sapi_info);
1.267 paf 802:
803: // send body
804: if(!header_only)
805: SAPI::send_body(r.sapi_info, output.str, output.length);
806: }
807:
808: #ifndef DOXYGEN
809: struct Range
810: {
1.396 moko 811: uint64_t start;
812: uint64_t end;
1.267 paf 813: };
814: #endif
1.396 moko 815:
816: #define UNSET ((uint64_t)-1)
817:
1.313 misha 818: static void parse_range(const String* s, Array<Range> &ar) {
1.267 paf 819: const char *p = s->cstr();
820: if(s->starts_with("bytes="))
821: p += 6;
822: Range r;
823: while(*p){
1.396 moko 824: r.start = UNSET;
825: r.end = UNSET;
1.401 moko 826:
827: while(*p==' ' || *p=='\t') p++;
828:
1.267 paf 829: if(*p >= '0' && *p <= '9'){
1.416 moko 830: const char *n=p;
1.401 moko 831: while(*p>='0' && *p<='9') p++;
1.440 moko 832: r.start = pa_atoul(pa_strdup(n, p-n)); // ((uint64_t)-1) is treated as UNSET
1.267 paf 833: }
1.401 moko 834:
835: while(*p==' ' || *p=='\t') p++;
836:
1.267 paf 837: if(*p++ != '-') break;
1.401 moko 838:
839: while(*p==' ' || *p=='\t') p++;
840:
1.267 paf 841: if(*p >= '0' && *p <= '9'){
1.416 moko 842: const char *n=p;
1.401 moko 843: while(*p>='0' && *p<='9') p++;
1.440 moko 844: r.end = pa_atoul(pa_strdup(n, p-n)); // ((uint64_t)-1) is treated as UNSET
1.267 paf 845: }
1.401 moko 846:
847: while(*p==' ' || *p=='\t') p++;
848:
849: if(*p)
850: if(*p++ != ',') break;
851:
1.267 paf 852: ar += r;
853: }
1.101 paf 854: }
1.267 paf 855:
1.400 moko 856: struct Send_range_action_info {
857: Request *r;
858: uint64_t offset;
859: uint64_t part_length;
860: };
861:
862: static void send_range(struct stat& /*finfo*/, int f, const String& /*file_spec*/, void *context){
863: Send_range_action_info &info = *(Send_range_action_info*)context;
864:
1.430 moko 865: SAPI::send_headers(info.r->sapi_info);
1.400 moko 866: pa_lseek(f, info.offset, SEEK_SET);
867:
1.432 moko 868: char buf[FILE_BUFFER_SIZE];
1.400 moko 869: do{
1.432 moko 870: size_t to_read = info.part_length < FILE_BUFFER_SIZE ? (size_t)info.part_length : FILE_BUFFER_SIZE;
1.400 moko 871: size_t to_write = file_block_read(f, buf, to_read);
872:
873: if(to_write == 0)
874: break;
875:
876: size_t size = SAPI::send_body(info.r->sapi_info, buf, to_write);
877: if(size != to_write)
878: break;
879:
880: info.part_length -= to_write;
881: } while (info.part_length);
882: }
883:
1.396 moko 884: static void output_pieces(Request& r, bool header_only, const String& filename, uint64_t content_length, Value& date, bool add_last_modified) {
1.315 misha 885: SAPI::add_header_attribute(r.sapi_info, "accept-ranges", "bytes");
1.270 paf 886:
1.339 moko 887: const char *range = SAPI::Env::get(r.sapi_info, "HTTP_RANGE");
1.396 moko 888: uint64_t offset=0;
889: uint64_t part_length=content_length;
1.401 moko 890:
891: if(range && content_length){
1.267 paf 892: Array<Range> ar;
893: parse_range(new String(range), ar);
1.396 moko 894: int count = ar.count();
1.267 paf 895: if(count == 1){
896: Range &rg = ar.get_ref(0);
1.401 moko 897:
898: if(rg.start == UNSET && rg.end == UNSET)
899: return SAPI::send_error(r.sapi_info, "", "416");
900:
1.396 moko 901: if(rg.start == UNSET && rg.end != UNSET){
1.440 moko 902: if(rg.end == 0)
903: return SAPI::send_error(r.sapi_info, "", "416");
1.401 moko 904: if(rg.end > content_length)
905: rg.end = content_length;
1.404 moko 906: rg.start = content_length - rg.end;
907: rg.end = content_length-1;
1.401 moko 908: } else if(rg.start != UNSET && rg.end == UNSET){
909: if(rg.start >= content_length)
910: return SAPI::send_error(r.sapi_info, "", "416");
1.267 paf 911: rg.end = content_length-1;
1.401 moko 912: } else {
913: if(rg.start >= content_length || rg.start > rg.end)
914: return SAPI::send_error(r.sapi_info, "", "416");
915: if(rg.end >= content_length)
916: rg.end = content_length-1;
1.267 paf 917: }
1.401 moko 918:
919: offset = rg.start;
920: part_length = rg.end-rg.start+1;
921:
1.400 moko 922: char buf[MAX_STRING];
1.448 moko 923: snprintf(buf, MAX_STRING, "bytes %s-%s/%s", pa_uitoa(rg.start), pa_uitoa(rg.end), pa_uitoa(content_length));
1.401 moko 924: SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206");
1.315 misha 925: SAPI::add_header_attribute(r.sapi_info, "content-range", buf);
1.401 moko 926: } else {
1.410 moko 927: return SAPI::send_error(r.sapi_info, count ? "Multiple ranges are not supported" : "Invalid range", count ? "501" : "400");
1.267 paf 928: }
929: }
930:
1.428 moko 931: SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, pa_uitoa(part_length));
1.267 paf 932:
1.314 misha 933: if(add_last_modified)
1.315 misha 934: SAPI::add_header_attribute(r.sapi_info, "last-modified", attributed_meaning_to_string(date, String::L_AS_IS, true).cstr());
1.314 misha 935:
1.414 moko 936: if(header_only){
1.430 moko 937: SAPI::send_headers(r.sapi_info);
1.414 moko 938: } else {
1.400 moko 939: Send_range_action_info info = { &r, offset, part_length};
940: file_read_action_under_lock(r.full_disk_path(filename), "send", send_range, &info);
1.267 paf 941: }
942: }
943:
1.248 paf 944: void Request::output_result(VFile* body_file, bool header_only, bool as_attachment) {
1.51 paf 945: // header: cookies
1.248 paf 946: cookie.output_result(sapi_info);
1.51 paf 947:
1.394 moko 948: // $.file and $.name (when body is real vfile)
949: Value* vfile = body_file->fields().get(response_body_file_name);
950: Value* vname = body_file->fields().get(name_name);
951:
952: const String* sfile = vfile ? &vfile->as_string() : NULL;
953: const String* sname = vname ? &vname->as_string() : NULL;
954:
955: if(sname && *sname == NONAME_DAT)
956: sname = NULL;
1.92 paf 957:
1.409 moko 958: // Content-Disposition, use $.name[<empty>] to avoid
1.394 moko 959: const String* disposition_name = sname ? sname->is_empty() ? NULL : sname : sfile;
960: if(disposition_name) {
1.420 moko 961: String& filename = *new String(pa_filename(disposition_name->cstr()), String::L_URI);
962: String& filename_asterisk = *new String(charsets.client().NAME(), String::L_CLEAN) << (*new String("''")) << filename;
963:
1.394 moko 964: VHash& hash=*new VHash();
965: hash.hash().put(value_name, new VString(as_attachment ? content_disposition_attachment : content_disposition_inline));
1.420 moko 966: hash.hash().put(content_disposition_filename_name, new VString(filename));
967: hash.hash().put(content_disposition_filename_name_asterisk, new VString(filename_asterisk));
1.394 moko 968: response.fields().put(content_disposition_name_upper, &hash);
969: }
1.267 paf 970:
1.394 moko 971: // _file_ content-type might be specified
972: Value* body_file_content_type = body_file->fields().get(content_type_name);
973: if(!body_file_content_type){
974: const String* scontent_name = sname && !sname->is_empty() ? sname : sfile;
975: if(scontent_name)
976: body_file_content_type = new VString(mime_type_of(scontent_name->cstr()));
1.267 paf 977: }
978:
1.312 misha 979: // set Content-Type
1.314 misha 980: if(body_file_content_type) {
981: // body file content type
1.344 moko 982: response.fields().put(content_type_name_upper, body_file_content_type);
1.314 misha 983: } else {
984: // default content type
1.344 moko 985: response.fields().put_dont_replace(content_type_name_upper, new VString(*new String(DEFAULT_CONTENT_TYPE)));
1.314 misha 986: }
1.312 misha 987:
1.314 misha 988: // prepare header: $response:fields without :body, :download and :charset
1.267 paf 989: Add_header_attribute_info info(*this);
1.281 paf 990: response.fields().for_each<Add_header_attribute_info*>(add_header_attribute, &info);
1.50 paf 991:
1.394 moko 992: if(sfile) {
1.314 misha 993: // $response:[download|body][$.file[filespec]] -- optput specified file
1.367 moko 994: uint64_t content_length=0;
1.267 paf 995: time_t atime=0, mtime=0, ctime=0;
1.394 moko 996: file_stat(full_disk_path(*sfile), content_length, atime, mtime, ctime);
1.267 paf 997:
998: VDate* vdate=0;
999: if(Value* v=body_file->fields().get("mdate")) {
1.423 moko 1000: vdate=dynamic_cast<VDate*>(v);
1001: if(!vdate)
1.286 misha 1002: throw Exception(PARSER_RUNTIME, 0, "mdate must be a date");
1.267 paf 1003: }
1004: if(!vdate)
1.346 moko 1005: vdate=new VDate((pa_time_t)mtime);
1.62 paf 1006:
1.399 moko 1007: output_pieces(*this, header_only, *sfile, content_length, *vdate, info.add_last_modified);
1.267 paf 1008: } else {
1.314 misha 1009: if(body_file_content_type)
1010: if(HashStringValue *hash=body_file_content_type->get_hash())
1011: body_file_content_type=hash->get(value_name);
1012:
1.366 moko 1013: output_sole_piece(*this, header_only, *body_file, body_file_content_type);
1.267 paf 1014: }
1.50 paf 1015: }
1.104 paf 1016:
1.328 misha 1017: const String& Request::mime_type_of(const String* file_name) {
1018: return mime_type_of(file_name?file_name->taint_cstr(String::L_FILE_SPEC):0);
1019: }
1020:
1.248 paf 1021: const String& Request::mime_type_of(const char* user_file_name_cstr) {
1.104 paf 1022: if(mime_types)
1.248 paf 1023: if(const char* cext=strrchr(user_file_name_cstr, '.')) {
1024: String sext(++cext);
1.246 paf 1025: Table::Action_options options;
1.342 moko 1026: if(mime_types->locate(0, sext.change_case(charsets.source(), String::CC_LOWER), options)) {
1.313 misha 1027: if(const String* result=mime_types->item(1))
1028: return *result;
1.104 paf 1029: else
1.395 moko 1030: throw Exception(PARSER_RUNTIME, 0, MIME_TYPES_NAME " table column elements must not be empty");
1.342 moko 1031: }
1.104 paf 1032: }
1.248 paf 1033:
1034: return *new String("application/octet-stream");
1035: }
1036:
1.365 moko 1037: const String* Request::get_used_filespec(uint file_no){
1.318 misha 1038: if(file_no < file_list.count())
1039: return new String(file_list[file_no], String::L_TAINTED);
1040: return 0;
1041: }
1042:
1.248 paf 1043: #ifdef XML
1.280 paf 1044: xmlChar* Request::transcode(const String& s) {
1.248 paf 1045: return charsets.source().transcode(s);
1046: }
1047:
1.280 paf 1048: xmlChar* Request::transcode(const String::Body s) {
1.248 paf 1049: return charsets.source().transcode(s);
1050: }
1051:
1.280 paf 1052: const String& Request::transcode(const xmlChar* s) {
1.248 paf 1053: return charsets.source().transcode(s);
1.233 paf 1054: }
1.248 paf 1055: #endif
1056:
1.442 moko 1057: VException::VException(Request& r, const Exception& e):
1058: fexception(e), fhandled(&VBool::get(false)),
1.444 moko 1059: ftrace_bottom(r.exception_trace.bottom_index()), ftrace_top(r.exception_trace.top_index()),
1.442 moko 1060: origin(Operation::Origin::create(0, 0, 0)), problem_source(e.problem_source()) {
1061: Request::Exception_trace& exception_trace=r.exception_trace;
1.248 paf 1062:
1.349 moko 1063: if(!exception_trace.is_empty()) {
1.442 moko 1064: Request::Trace bottom=exception_trace.bottom_value();
1.350 moko 1065: origin=bottom.origin();
1.443 moko 1066: if(!problem_source) // we don't know who trigged the bug
1.350 moko 1067: problem_source=bottom.name(); // we usually know source of next-from-throw-point exception did that
1.248 paf 1068: }
1.442 moko 1069: if(origin.file_no)
1070: ffile=r.file_list[origin.file_no];
1071: }
1072:
1073: Value* VException::handled() {
1074: return fhash ? fhash->get(Symbols::HANDLED_SYMBOL) : fhandled;
1075: }
1.248 paf 1076:
1.444 moko 1077: Table& VException::stack(Request& r) {
1078: if(ftrace_top==size_t(-1))
1079: throw Exception(PARSER_RUNTIME, 0, "exception stack used outside of context");
1080: return r.exception_trace.table(r, problem_source, ftrace_bottom, ftrace_top);
1081: }
1082:
1.442 moko 1083: Value* VException::get_element(const String& name) {
1084: if(!fhash) {
1085: if(SYMBOLS_EQ(name,HANDLED_SYMBOL))
1086: return fhandled;
1087: if(SYMBOLS_EQ(name,TYPE_SYMBOL))
1088: return fexception.type(true) ? new VString(*new String(fexception.type())) : 0;
1089: if(SYMBOLS_EQ(name,SOURCE_SYMBOL))
1090: return problem_source ? new VString(*problem_source) : 0;
1091: if(SYMBOLS_EQ(name,COMMENT_SYMBOL))
1092: return fexception.comment(true) ? new VString(fexception.comment()) : 0;
1093: if(SYMBOLS_EQ(name,FILE_SYMBOL))
1094: return origin.file_no ? new VString(ffile) : 0;
1095: if(SYMBOLS_EQ(name,LINENO_SYMBOL))
1096: return origin.file_no ? new VInt(1+origin.line) : 0;
1097: if(SYMBOLS_EQ(name,COLNO_SYMBOL))
1098: return origin.file_no ? new VInt(1+origin.col) : 0;
1099: if(SYMBOLS_EQ(name,FIELDS_SYMBOL))
1100: return this;
1101: }
1102: // Computed field names need not be interned: use ordinary hash lookup.
1103: return VHashReference::get_element(name);
1104: }
1105:
1106: const VJunction* VException::put_element(const String& name, Value* value) {
1107: if(!fhash && SYMBOLS_EQ(name,HANDLED_SYMBOL)) {
1108: fhandled=value;
1109: return 0;
1110: }
1111: return VHashReference::put_element(name, value);
1112: }
1113:
1114: HashStringValue& VException::hash() {
1115: if(fhash)
1116: return *fhash;
1117: HashStringValue& result=*new HashStringValue;
1.248 paf 1118:
1.442 moko 1119: if(const char* type=fexception.type(true))
1120: result.put(Symbols::TYPE_SYMBOL, new VString(*new String(type)));
1.349 moko 1121: if(problem_source)
1.442 moko 1122: result.put(Symbols::SOURCE_SYMBOL, new VString(*problem_source));
1123: if(origin.file_no) {
1124: result.put(Symbols::FILE_SYMBOL, new VString(ffile));
1125: result.put(Symbols::LINENO_SYMBOL, new VInt(1+origin.line));
1126: result.put(Symbols::COLNO_SYMBOL, new VInt(1+origin.col));
1127: }
1128: if(const char* comment=fexception.comment(true))
1129: result.put(Symbols::COMMENT_SYMBOL, new VString(comment));
1130: result.put(Symbols::HANDLED_SYMBOL, fhandled);
1.248 paf 1131:
1.442 moko 1132: fhash=&result;
1133: return result;
1.133 parser 1134: }
1.337 moko 1135:
1136: Temp_value_element::Temp_value_element(Request& arequest, Value& awhere, const String& aname, Value* awhat) :
1137: frequest(arequest),
1138: fwhere(awhere),
1139: fname(aname),
1140: saved(awhere.get_element(aname))
1141: {
1142: Junction* junction;
1143: if(saved && (junction=saved->get_junction()) && junction->is_getter)
1144: saved=0;
1145: frequest.put_element(fwhere, aname, awhat);
1146: }
1147:
1148: Temp_value_element::~Temp_value_element() {
1149: frequest.put_element(fwhere, fname, saved ? saved : VVoid::get());
1150: }
E-mail: