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