Annotation of parser3/src/main/pa_request.C, revision 1.369
1.54 paf 1: /** @file
1.55 paf 2: Parser: request class main part. @see compile.C and execute.C.
3:
1.351 moko 4: Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.194 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://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.1 paf 10: #include "pa_request.h"
1.3 paf 11: #include "pa_wwrapper.h"
12: #include "pa_vclass.h"
1.31 paf 13: #include "pa_globals.h"
1.30 paf 14: #include "pa_vint.h"
1.297 misha 15: #include "pa_vmethod_frame.h"
1.32 paf 16: #include "pa_types.h"
1.248 paf 17: #include "pa_venv.h"
18: #include "pa_vmath.h"
19: #include "pa_vstatus.h"
20: #include "pa_vrequest.h"
1.78 paf 21: #include "pa_vtable.h"
1.90 paf 22: #include "pa_vfile.h"
1.147 parser 23: #include "pa_dictionary.h"
1.208 paf 24: #include "pa_charset.h"
1.186 paf 25: #include "pa_charsets.h"
1.248 paf 26: #include "pa_cache_managers.h"
27: #include "pa_vmail.h"
28: #include "pa_vform.h"
29: #include "pa_vcookie.h"
30: #include "pa_vresponse.h"
31: #include "pa_vmemory.h"
1.253 paf 32: #include "pa_vconsole.h"
1.267 paf 33: #include "pa_vdate.h"
1.159 parser 34:
1.369 ! moko 35: volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.368 2016/12/28 17:41:15 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H;
1.329 moko 36:
1.248 paf 37: // consts
38:
1.269 paf 39: #define UNHANDLED_EXCEPTION_METHOD_NAME "unhandled_exception"
1.207 paf 40:
1.82 paf 41: /// content type of exception response, when no @MAIN:exception handler defined
1.248 paf 42: const char* UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain";
1.82 paf 43:
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.369 ! moko 47: const uint LOOP_LIMIT=20000;
1.361 moko 48: const uint EXECUTE_RECOURSION_LIMIT=1000;
1.369 ! moko 49: const size_t FILE_SIZE_LIMIT=512*1024*1024;
1.361 moko 50:
1.248 paf 51: // defines for globals
52:
53: #define MAIN_METHOD_NAME "main"
54: #define AUTO_METHOD_NAME "auto"
1.364 moko 55: #define USE_METHOD_NAME "use"
1.309 misha 56: #define AUTOUSE_METHOD_NAME "autouse"
1.364 moko 57:
1.248 paf 58: #define EXCEPTION_TYPE_PART_NAME "type"
59: #define EXCEPTION_SOURCE_PART_NAME "source"
60: #define EXCEPTION_COMMENT_PART_NAME "comment"
61:
1.364 moko 62: #define ORIGIN_KEY "origin"
63:
1.248 paf 64: // globals
65:
66: const String main_method_name(MAIN_METHOD_NAME);
67: const String auto_method_name(AUTO_METHOD_NAME);
1.364 moko 68: static const String use_method_name(USE_METHOD_NAME);
69: static const String autouse_method_name(AUTOUSE_METHOD_NAME);
1.311 misha 70:
1.248 paf 71: const String exception_type_part_name(EXCEPTION_TYPE_PART_NAME);
72: const String exception_source_part_name(EXCEPTION_SOURCE_PART_NAME);
73: const String exception_comment_part_name(EXCEPTION_COMMENT_PART_NAME);
74: const String exception_handled_part_name(EXCEPTION_HANDLED_PART_NAME);
75:
1.364 moko 76: static const String origin_key(ORIGIN_KEY);
77:
1.369 ! moko 78: int pa_loop_limit=LOOP_LIMIT;
1.361 moko 79: int pa_execute_recoursion_limit=EXECUTE_RECOURSION_LIMIT;
1.369 ! moko 80: size_t pa_file_size_limit=FILE_SIZE_LIMIT;
1.361 moko 81:
1.248 paf 82: // defines for statics
83:
84: #define CHARSETS_NAME "CHARSETS"
85: #define MIME_TYPES_NAME "MIME-TYPES"
1.330 moko 86: #define STRICT_VARS_NAME "STRICT-VARS"
1.357 moko 87: #define PROTOTYPE_NAME "OBJECT-PROTOTYPE"
1.362 moko 88: #define LIMITS_NAME "LIMITS"
1.369 ! moko 89: #define LOOP_LIMIT_NAME "max_loop"
1.362 moko 90: #define RECOURSION_LIMIT_NAME "max_recoursion"
1.369 ! moko 91: #define FILE_SIZE_LIMIT_NAME "max_file_size"
1.248 paf 92: #define CONF_METHOD_NAME "conf"
93: #define POST_PROCESS_METHOD_NAME "postprocess"
94: #define CLASS_PATH_NAME "CLASS_PATH"
1.277 paf 95: #define RESPONSE_BODY_FILE_NAME "file"
1.248 paf 96:
1.344 moko 97: #define DOWNLOAD_NAME_UPPER "DOWNLOAD"
98: #define BODY_NAME_UPPER "BODY"
99:
1.248 paf 100: // statics
101:
102: static const String charsets_name(CHARSETS_NAME);
103: static const String main_class_name(MAIN_CLASS_NAME);
104: static const String mime_types_name(MIME_TYPES_NAME);
1.330 moko 105: static const String strict_vars_name(STRICT_VARS_NAME);
1.357 moko 106: static const String prototype_name(PROTOTYPE_NAME);
1.362 moko 107: static const String limits_name(LIMITS_NAME);
1.369 ! moko 108: static const String loop_limit_name(LOOP_LIMIT_NAME);
1.361 moko 109: static const String recoursion_limit_name(RECOURSION_LIMIT_NAME);
1.369 ! moko 110: static const String file_size_limit_name(FILE_SIZE_LIMIT_NAME);
1.361 moko 111:
1.248 paf 112: static const String conf_method_name(CONF_METHOD_NAME);
113: static const String post_process_method_name(POST_PROCESS_METHOD_NAME);
114: static const String class_path_name(CLASS_PATH_NAME);
1.277 paf 115: static const String response_body_file_name(RESPONSE_BODY_FILE_NAME);
1.248 paf 116:
1.344 moko 117: static const String download_name_upper(DOWNLOAD_NAME_UPPER);
118: static const String body_name_upper(BODY_NAME_UPPER);
119:
120: // more static
121:
122: static const String content_type_name_upper(HTTP_CONTENT_TYPE_UPPER);
123: static const String content_disposition_name_upper(CONTENT_DISPOSITION_UPPER);
124: static const String content_disposition_inline(CONTENT_DISPOSITION_INLINE);
125: static const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT);
126:
1.248 paf 127: // defines
1.82 paf 128:
1.344 moko 129: #define CHARSET_NAME_UPPER "CHARSET"
130: #define LAST_MODIFIED_NAME_UPPER "LAST-MODIFIED"
131:
1.233 paf 132: // op.C
1.248 paf 133: VStateless_class& VClassMAIN_create();
1.123 paf 134:
1.157 parser 135: //
1.248 paf 136: Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info,
1.325 moko 137: String::Language adefault_lang):
1.248 paf 138: // private
139: anti_endless_execute_recoursion(0),
140:
141: // public
1.335 moko 142: allow_class_replace(false),
1.248 paf 143: method_frame(0),
144: rcontext(0),
145: wcontext(0),
146: flang(adefault_lang),
1.191 paf 147: fconnection(0),
1.307 misha 148: finterrupted(false),
149: fskip(SKIP_NOTHING),
150: fin_cycle(0),
1.248 paf 151:
152: // public
1.257 paf 153: request_info(arequest_info),
1.248 paf 154: sapi_info(asapi_info),
1.359 moko 155: charsets(pa_UTF8_charset, pa_UTF8_charset, pa_UTF8_charset), // default charsets
1.248 paf 156:
157: main_class(VClassMAIN_create()),
1.260 paf 158: form(*new VForm(charsets, arequest_info)),
1.248 paf 159: mail(*new VMail),
160: response(*new VResponse(arequest_info, charsets)),
1.298 misha 161: cookie(*new VCookie(charsets, arequest_info)),
1.285 misha 162: console(*new VConsole),
1.248 paf 163:
164: // private
165: configure_admin_done(false),
166:
167: // private defaults
168: fdefault_lang(adefault_lang),
169: // private mime types
170: mime_types(0)
1.157 parser 171: {
1.261 paf 172: pa_register_thread_request(*this);
173:
1.248 paf 174: // file_no=0 => unknown
1.347 moko 175: file_list+="UNKNOWN";
176: file_list+="-body of process-"; // pseudo_file_no__process
1.186 paf 177:
1.178 paf 178: // maybe expire old caches
1.264 paf 179: cache_managers->maybe_expire();
1.178 paf 180:
1.157 parser 181: /// directly used
1.233 paf 182: // MAIN class, operators
1.353 moko 183: put_class(&main_class);
1.157 parser 184: // classes:
185: // table, file, random, mail, image, ...
1.248 paf 186: methoded_array().register_directly_used(*this);
1.157 parser 187:
188: /// methodless
1.354 moko 189:
1.157 parser 190: // env class
1.353 moko 191: put_class(new VEnv(asapi_info));
1.175 paf 192: // status class
1.353 moko 193: put_class(new VStatus());
1.157 parser 194: // request class
1.353 moko 195: put_class(new VRequest(arequest_info, charsets, form, asapi_info));
1.157 parser 196: // cookie class
1.353 moko 197: put_class(&cookie);
1.253 paf 198: // console class
1.353 moko 199: put_class(&console);
1.354 moko 200:
1.157 parser 201: /// methoded
1.354 moko 202:
1.157 parser 203: // response class
1.353 moko 204: put_class(&response);
1.157 parser 205: // form class
1.353 moko 206: put_class(&form);
1.213 paf 207: // mail class
1.353 moko 208: put_class(&mail);
1.157 parser 209: // math class
1.353 moko 210: put_class(new VMath);
1.248 paf 211: // memory class
1.353 moko 212: put_class(new VMemory);
1.117 paf 213: }
1.192 paf 214:
215: Request::~Request() {
216: #ifdef XML
217: // if for some strange reason xml generic errors failed to be reported, free them up
1.248 paf 218: if(const char* xml_generic_errors=xmlGenericErrors()) {
219: SAPI::log(sapi_info, "warning: unreported xmlGenericErrors: %s", xml_generic_errors);
1.341 moko 220: pa_free((void *)xml_generic_errors);
1.192 paf 221: }
222: #endif
223: }
1.236 paf 224:
1.248 paf 225: Value& Request::get_self() { return method_frame/*always have!*/->self(); }
1.192 paf 226:
1.355 moko 227: VStateless_class* Request::get_class(const String& name){
228: VStateless_class* result=classes().get(name);
1.313 misha 229: if(!result)
1.364 moko 230: if(const Method *method=main_class.get_method(autouse_method_name)){
231: Value *vname=new VString(name);
232: CONSTRUCTOR_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
233: frame.store_params(&vname, 1);
234: // we don't need the result
235: call(frame);
236: });
237: result=classes().get(name);
238: }
1.309 misha 239: return result;
240: }
241:
1.356 moko 242: static void load_charset(HashStringValue::key_type akey, HashStringValue::value_type avalue, Request_charsets* charsets) {
1.359 moko 243: pa_charsets.load_charset(*charsets, akey, avalue->as_string());
1.208 paf 244: }
1.356 moko 245:
1.248 paf 246: void Request::configure_admin(VStateless_class& conf_class) {
1.208 paf 247: if(configure_admin_done)
1.356 moko 248: throw Exception(PARSER_RUNTIME, 0, "parser already configured");
1.208 paf 249: configure_admin_done=true;
250:
1.227 paf 251: // charsets must only be specified in method_frame config
1.208 paf 252: // so that users would not interfere
253:
254: /* $MAIN:CHARSETS[
255: $.charsetname1[/full/path/to/charset/file.cfg]
256: ...
257: ]
258: */
1.308 misha 259: if(Value* vcharsets=conf_class.get_element(charsets_name)) {
1.342 moko 260: if(!vcharsets->is_string()) {
1.248 paf 261: if(HashStringValue* charsets=vcharsets->get_hash())
1.281 paf 262: charsets->for_each<Request_charsets*>(load_charset, &this->charsets);
1.240 paf 263: else
1.356 moko 264: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" CHARSETS_NAME " must be hash");
1.342 moko 265: }
1.208 paf 266: }
267:
1.330 moko 268: #ifdef STRICT_VARS
1.332 moko 269: VVoid::strict_vars=false;
1.330 moko 270: if(Value* strict_vars=conf_class.get_element(strict_vars_name)) {
271: if(strict_vars->is_bool())
272: VVoid::strict_vars=strict_vars->as_bool();
1.357 moko 273: else
274: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" STRICT_VARS_NAME " must be bool");
275: }
276: #endif
277:
278: #ifdef OBJECT_PROTOTYPE
279: VClass::prototype=true;
280: if(Value* prototype=conf_class.get_element(prototype_name)) {
281: if(prototype->is_bool())
282: VClass::prototype=prototype->as_bool();
283: else
284: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" PROTOTYPE_NAME " must be bool");
1.330 moko 285: }
286: #endif
287:
1.362 moko 288: Value* limits=conf_class.get_element(limits_name);
289:
1.361 moko 290: pa_loop_limit=LOOP_LIMIT;
1.362 moko 291: if(limits)
292: if(Value* loop_limit=limits->get_element(loop_limit_name)) {
293: if(loop_limit->is_evaluated_expr()) {
294: pa_loop_limit=loop_limit->as_int();
295: if(pa_loop_limit==0) pa_loop_limit=INT_MAX;
296: } else
1.369 ! moko 297: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." LOOP_LIMIT_NAME " must be int");
1.362 moko 298: }
1.361 moko 299:
300: pa_execute_recoursion_limit=EXECUTE_RECOURSION_LIMIT;
1.362 moko 301: if(limits)
302: if(Value* recoursion_limit=limits->get_element(recoursion_limit_name)) {
303: if(recoursion_limit->is_evaluated_expr()) {
304: pa_execute_recoursion_limit=recoursion_limit->as_int();
305: if(pa_execute_recoursion_limit==0) pa_execute_recoursion_limit=INT_MAX;
306: } else
1.369 ! moko 307: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." RECOURSION_LIMIT_NAME " must be int");
! 308: }
! 309:
! 310: pa_file_size_limit=FILE_SIZE_LIMIT;
! 311: if(limits)
! 312: if(Value* file_size_limit=limits->get_element(file_size_limit_name)) {
! 313: if(file_size_limit->is_evaluated_expr()) {
! 314: double limit=file_size_limit->as_double();
! 315: if(limit >= (double)SSIZE_MAX)
! 316: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." FILE_SIZE_LIMIT_NAME " must be less then %.15g", (double)SSIZE_MAX);
! 317: pa_file_size_limit=(size_t)limit;
! 318: if(pa_file_size_limit==0) pa_file_size_limit=SSIZE_MAX;
! 319: } else
! 320: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." FILE_SIZE_LIMIT_NAME " must be number");
1.362 moko 321: }
1.361 moko 322:
1.227 paf 323: // configure method_frame options
1.208 paf 324: // until someone with less privileges have overriden them
1.248 paf 325: methoded_array().configure_admin(*this);
1.208 paf 326: }
1.150 parser 327:
1.349 moko 328: const char* Request::get_exception_cstr(const Exception& e, Request::Exception_details& details) {
329:
1.269 paf 330: #define PA_URI_FORMAT "%s: "
1.349 moko 331: #define PA_COMMENT_TYPE_FORMAT "%s [%s]"
332:
1.269 paf 333: #define PA_ORIGIN_FILE_POS_FORMAT "%s(%d:%d): "
334: #define PA_SOURCE_FORMAT "'%s' "
1.349 moko 335:
336: #define PA_ORIGIN_FILE_POS_VALUE file_list[details.origin.file_no].cstr(), 1+details.origin.line, 1+details.origin.col,
337: #define PA_SOURCE_VALUE details.problem_source->cstr(),
338:
339: #define EXCEPTION_CSTR(f1,v1,f2,v2) \
340: snprintf(result, MAX_STRING, \
341: PA_URI_FORMAT \
342: f1 f2 \
343: PA_COMMENT_TYPE_FORMAT, \
344: request_info.uri, \
345: v1 v2 \
346: e.comment(), e.type() \
347: );
348:
1.269 paf 349: char* result=new(PointerFreeGC) char[MAX_STRING];
350:
351: if(details.problem_source) { // do we know the guy?
1.349 moko 352: if(details.origin.file_no) // do whe know where he came from?
353: EXCEPTION_CSTR(PA_ORIGIN_FILE_POS_FORMAT, PA_ORIGIN_FILE_POS_VALUE, PA_SOURCE_FORMAT, PA_SOURCE_VALUE)
354: else
355: EXCEPTION_CSTR(PA_SOURCE_FORMAT, PA_SOURCE_VALUE,,)
356: } else {
357: if(details.origin.file_no) // do whe know where he came from?
358: EXCEPTION_CSTR(PA_ORIGIN_FILE_POS_FORMAT, PA_ORIGIN_FILE_POS_VALUE,,)
359: else
360: EXCEPTION_CSTR(,,,)
361: }
1.269 paf 362:
363: return result;
364: }
1.272 paf 365:
366: void Request::configure() {
367: // configure admin options if not configured yet
368: if(!configure_admin_done)
369: configure_admin(main_class);
370:
371: // configure not-admin=user options
372: methoded_array().configure_user(*this);
373:
374: // $MAIN:MIME-TYPES
1.308 misha 375: if(Value* element=main_class.get_element(mime_types_name))
1.272 paf 376: if(Table *table=element->get_table())
377: mime_types=table;
378: }
1.64 paf 379: /**
380: load MAIN class, execute @main.
381: MAIN class consists of all the auto.p files we'd manage to find
382: plus
383: the file user requested us to process
384: all located classes become children of one another,
385: composing class we name 'MAIN'
1.180 paf 386:
387: @test log stack trace
388:
1.64 paf 389: */
1.248 paf 390: void Request::core(const char* config_filespec, bool config_fail_on_read_problem, bool header_only) {
1.169 parser 391: try {
1.211 paf 392: // loading config
393: if(config_filespec) {
1.248 paf 394: const String& filespec=*new String(config_filespec);
1.366 moko 395: use_file_directly(main_class, filespec, config_fail_on_read_problem, true /*file must exist if 'fail on read problem' not set*/);
1.29 paf 396: }
1.8 paf 397:
1.338 moko 398: // filling mail received
399: mail.fill_received(*this);
400:
1.72 paf 401: // loading auto.p files from document_root/..
402: // to the one beside requested file.
403: // all assigned bases from upper dir
404: {
1.248 paf 405: const char* after=request_info.path_translated;
406: size_t drlen=strlen(request_info.document_root);
407: if(memcmp(after, request_info.document_root, drlen)==0) {
1.115 paf 408: after+=drlen;
409: if(after[-1]=='/')
410: --after;
411: }
412:
1.248 paf 413: while(const char* before=strchr(after, '/')) {
414: String& sfile_spec=*new String;
415: if(after!=request_info.path_translated) {
1.366 moko 416: sfile_spec.append_strdup(request_info.path_translated, before-request_info.path_translated, String::L_CLEAN);
1.152 parser 417: sfile_spec << "/" AUTO_FILE_NAME;
418:
1.366 moko 419: use_file_directly(main_class, sfile_spec, true /*fail on read problem*/, false /*but ignore absence, sole user*/);
1.115 paf 420: }
1.317 misha 421: for(after=before+1;*after=='/';after++);
1.72 paf 422: }
423: }
1.34 paf 424:
1.272 paf 425: try {
426: // compile requested file
427: String& spath_translated=*new String;
428: spath_translated.append_help_length(request_info.path_translated, 0, String::L_TAINTED);
1.318 misha 429: use_file_directly(main_class, spath_translated);
1.272 paf 430:
431: configure();
432: } catch(...) {
433: configure(); // configure anyway, useful in @unhandled_exception [say, if they would want to mail by SMTP something]
434: rethrow;
435: }
1.25 paf 436:
437: // execute @main[]
1.248 paf 438: const String* body_string=execute_virtual_method(main_class, main_method_name);
1.41 paf 439: if(!body_string)
1.366 moko 440: throw Exception(PARSER_RUNTIME, 0, "'" MAIN_METHOD_NAME "' method not found");
1.79 paf 441:
1.250 paf 442: // extract response body
1.344 moko 443: Value* body_value=response.fields().get(download_name_upper); // $response:download?
1.250 paf 444: bool as_attachment=body_value!=0;
445: if(!body_value)
1.344 moko 446: body_value=response.fields().get(body_name_upper); // $response:body
1.250 paf 447: if(!body_value)
448: body_value=new VString(*body_string); // just result of ^main[]
449:
1.119 paf 450: // @postprocess
1.308 misha 451: if(Value* value=main_class.get_element(post_process_method_name))
1.248 paf 452: if(Junction* junction=value->get_junction())
1.91 paf 453: if(const Method *method=junction->method) {
454: // preparing to pass parameters to
1.119 paf 455: // @postprocess[data]
1.363 moko 456: METHOD_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
457: frame.store_params(&body_value, 1);
458: call(frame);
459: body_value=&frame.result();
460: });
1.91 paf 461: }
1.90 paf 462:
1.303 misha 463: VFile* body_file=body_value->as_vfile(flang, &charsets);
1.41 paf 464:
1.45 paf 465: // OK. write out the result
1.248 paf 466: output_result(body_file, header_only, as_attachment);
1.183 paf 467:
1.171 parser 468: } catch(const Exception& e) { // request handling problem
1.268 paf 469: try {
1.76 paf 470: // we're returning not result, but error explanation
1.269 paf 471:
472: Request::Exception_details details=get_details(e);
473: const char* exception_cstr=get_exception_cstr(e, details);
1.258 paf 474:
475: // reset language to default
476: flang=fdefault_lang;
477:
478: // reset response
479: response.fields().clear();
480:
481: // this is what we'd return in $response:body
482: const String* body_string=0;
483:
484: // maybe we'd be lucky enough as to report an error
485: // in a gracefull way...
1.308 misha 486: if(Value* value=main_class.get_element(*new String(UNHANDLED_EXCEPTION_METHOD_NAME))) {
1.258 paf 487: if(Junction* junction=value->get_junction()) {
488: if(const Method *method=junction->method) {
1.287 misha 489: // preparing to pass parameters to
490: // @unhandled_exception[exception;stack]
491:
492: // $stack[^table::create{name file lineno colno}]
493: Table::columns_type stack_trace_columns(new ArrayString);
494: *stack_trace_columns+=new String("name");
495: *stack_trace_columns+=new String("file");
496: *stack_trace_columns+=new String("lineno");
497: *stack_trace_columns+=new String("colno");
498: Table& stack_trace=*new Table(stack_trace_columns);
499: if(!exception_trace.is_empty()/*signed!*/)
500: for(size_t i=exception_trace.bottom_index(); i<exception_trace.top_index(); i++) {
501: Trace trace=exception_trace.get(i);
502: Table::element_type row(new ArrayString);
503:
504: *row+=trace.name(); // name column
505: Operation::Origin origin=trace.origin();
506: if(origin.file_no) {
507: *row+=new String(file_list[origin.file_no], String::L_TAINTED); // 'file' column
508: *row+=new String(String::Body::Format(1+origin.line), String::L_CLEAN); // 'lineno' column
509: *row+=new String(String::Body::Format(1+origin.col), String::L_CLEAN); // 'colno' column
510: }
511: stack_trace+=row;
512: }
513:
1.323 moko 514: // future $response:body=
515: // execute ^unhandled_exception[exception;stack]
516: 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)
517:
1.300 misha 518: Value *params[]={&details.vhash, new VTable(&stack_trace)};
1.363 moko 519: METHOD_FRAME_ACTION(*method, 0 /*no caller*/, main_class, {
520: frame.store_params(params, 2);
521: call(frame);
522: body_string=&frame.result().as_string();
523: });
1.233 paf 524: }
1.30 paf 525: }
1.258 paf 526: }
527:
528: if(!body_string) { // couldn't report an error beautifully?
529: // doing that ugly
530:
531: // future $response:content-type
1.344 moko 532: response.fields().put(content_type_name_upper, new VString(*new String(UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.258 paf 533: // future $response:body
1.269 paf 534: body_string=new String(exception_cstr);
1.258 paf 535: }
1.41 paf 536:
1.258 paf 537: VString body_vstring(*body_string);
1.303 misha 538: VFile* body_file=body_vstring.as_vfile(flang, &charsets);
1.90 paf 539:
1.273 paf 540: // conditionally log it
541: Value* vhandled=details.vhash.hash().get(exception_handled_part_name);
542: if(!vhandled || !vhandled->as_bool()) {
543: SAPI::log(sapi_info, "%s", exception_cstr);
544: }
545:
1.258 paf 546: // ERROR. write it out
547: output_result(body_file, header_only, false);
1.287 misha 548:
1.273 paf 549: } catch(const Exception& e) { // exception in unhandled exception
1.269 paf 550: Request::Exception_details details=get_details(e);
551: const char* exception_cstr=get_exception_cstr(e, details);
1.273 paf 552: // unconditionally log the beast
553: SAPI::log(sapi_info, "%s", exception_cstr);
1.269 paf 554:
1.366 moko 555: throw Exception(0, 0, "in %s", exception_cstr);
1.268 paf 556: }
1.1 paf 557: }
558: }
559:
1.266 paf 560: uint Request::register_file(String::Body file_spec) {
1.248 paf 561: file_list+=file_spec;
562: return file_list.count()-1;
563: }
564:
1.366 moko 565: void Request::use_file_directly(VStateless_class& aclass, const String& file_spec, bool fail_on_read_problem, bool fail_on_file_absence) {
1.73 paf 566: // cyclic dependence check
1.318 misha 567: if(used_files.get(file_spec))
1.248 paf 568: return;
1.318 misha 569: used_files.put(file_spec, true);
570:
571: if(fail_on_read_problem && !fail_on_file_absence) // ignore file absence if asked for
572: if(!entry_exists(file_spec))
573: return;
574:
575: if(const char* source=file_read_text(charsets, file_spec, fail_on_read_problem))
576: use_buf(aclass, source, 0, register_file(file_spec));
577: }
578:
579:
1.349 moko 580: void Request::use_file(VStateless_class& aclass, const String& file_name, const String* use_filespec/*absolute*/) {
1.326 misha 581: if(file_name.is_empty())
1.366 moko 582: throw Exception(PARSER_RUNTIME, 0, "usage failed - no filename was specified");
1.326 misha 583:
1.318 misha 584: const String* filespec=0;
585:
586: if(file_name.first_char()=='/') //absolute path? [no need to scan MAIN:CLASS_PATH]
587: filespec=&absolute(file_name);
588: else if(use_filespec){ // search in current dir first
1.331 misha 589: size_t last_slash_pos=use_filespec->strrpbrk("/");
590: if(last_slash_pos!=STRING_NOT_FOUND)
591: filespec=file_exist(use_filespec->mid(0, last_slash_pos), file_name); // found in current dir?
1.318 misha 592: }
593:
594: if(!filespec){
595: // prevent multiple scan CLASS_PATH for searching one file
596: if(searched_along_class_path.get(file_name))
597: return;
598: searched_along_class_path.put(file_name, true);
1.308 misha 599: if(Value* element=main_class.get_element(class_path_name)) {
1.233 paf 600: if(element->is_string()) {
1.318 misha 601: filespec=file_exist(absolute(element->as_string()), file_name); // found at class_path?
1.233 paf 602: } else if(Table *table=element->get_table()) {
1.318 misha 603: for(size_t i=table->count(); i--; ) {
1.248 paf 604: const String& path=*(*table->get(i))[0];
1.318 misha 605: if(filespec=file_exist(absolute(path), file_name))
1.233 paf 606: break; // found along class_path
607: }
608: } else
1.366 moko 609: throw Exception(PARSER_RUNTIME, 0, "$" CLASS_PATH_NAME " must be string or table");
1.318 misha 610: if(!filespec)
1.366 moko 611: throw Exception(PARSER_RUNTIME, &file_name, "not found along $" MAIN_CLASS_NAME ":" CLASS_PATH_NAME);
1.318 misha 612: } else
1.366 moko 613: throw Exception(PARSER_RUNTIME, &file_name, "usage failed - no $" MAIN_CLASS_NAME ":" CLASS_PATH_NAME " were specified");
1.148 parser 614: }
1.230 paf 615:
1.318 misha 616: use_file_directly(aclass, *filespec);
1.16 paf 617: }
618:
1.364 moko 619: void Request::use_file(const String& file_name, const String* use_filespec/*absolute*/, Operation::Origin origin) {
1.349 moko 620: static String use("USE");
621: try {
1.364 moko 622: static VHash* voptions=new VHash();
623: if(const Method *method=main_class.get_method(use_method_name)){
624: Value *params[]={new VString(file_name), voptions};
625: voptions->hash().put(origin_key, new VString(*use_filespec));
626:
627: CONSTRUCTOR_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
628: frame.store_params(params, 2);
629: // we don't need the result
630: call(frame);
631: });
632: }
1.349 moko 633: } catch (...) {
634: exception_trace.push(Trace(&use, origin));
635: rethrow;
636: }
637: }
1.208 paf 638:
1.358 moko 639: void Request::use_buf(VStateless_class& aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset) {
640: // temporary zero @conf to avoid it second execution
1.248 paf 641: Temp_method temp_method_conf(aclass, conf_method_name, 0);
1.358 moko 642: // temporary zero @auto to avoid it second execution
1.248 paf 643: Temp_method temp_method_auto(aclass, auto_method_name, 0);
1.233 paf 644:
1.295 misha 645: // compile loaded classes
646: ArrayClass& cclasses=compile(&aclass, source, main_alias, file_no, line_no_offset);
1.212 paf 647:
1.248 paf 648: VString* vfilespec=
649: new VString(*new String(file_list[file_no], String::L_TAINTED));
1.295 misha 650:
651: for(size_t i=0; i<cclasses.count(); i++){
652: VStateless_class& cclass=*cclasses.get(i);
1.296 misha 653:
654: // locate and execute possible @conf[] static
1.358 moko 655: Execute_nonvirtual_method_result executed=execute_nonvirtual_method(cclass, conf_method_name, vfilespec, false/*no string result needed*/);
1.295 misha 656: if(executed.method)
657: configure_admin(cclass/*, executed.method->name*/);
658:
659: // locate and execute possible @auto[] static
1.358 moko 660: execute_nonvirtual_method(cclass, auto_method_name, vfilespec, false/*no result needed*/);
1.336 moko 661:
662: cclass.enable_default_setter();
1.295 misha 663: }
1.19 paf 664: }
665:
1.248 paf 666: const String& Request::relative(const char* apath, const String& relative_name) {
1.341 moko 667: char *hpath=pa_strdup(apath);
1.248 paf 668: String& result=*new String;
669: if(rsplit(hpath, '/')) // if something/splitted
1.217 paf 670: result << hpath << "/";
1.248 paf 671: result << relative_name;
672: return result;
1.19 paf 673: }
674:
1.77 paf 675: const String& Request::absolute(const String& relative_name) {
1.217 paf 676: if(relative_name.first_char()=='/') {
1.341 moko 677: String& result=*new String(pa_strdup(request_info.document_root));
1.104 paf 678: result << relative_name;
1.19 paf 679: return result;
680: } else
1.248 paf 681: if(relative_name.pos("://")!=STRING_NOT_FOUND // something like "http://xxx"
1.247 paf 682: #ifdef WIN32
683: || relative_name.pos(":")==1 // DRIVE:
684: || relative_name.starts_with("\\\\") // UNC1
685: || relative_name.starts_with("//") // UNC2
686: #endif
687: )
688: return relative_name;
689: else
1.248 paf 690: return relative(request_info.path_translated, relative_name);
1.1 paf 691: }
1.45 paf 692:
1.267 paf 693: #ifndef DOXYGEN
694: class Add_header_attribute_info
695: {
696: public:
697: Add_header_attribute_info(Request& ar) : r(ar), add_last_modified(true) {}
698: Request& r;
699: bool add_last_modified;
700: };
701: #endif
1.344 moko 702: static void add_header_attribute(HashStringValue::key_type name, HashStringValue::value_type value, Add_header_attribute_info* info) {
703: if(name==BODY_NAME_UPPER || name==DOWNLOAD_NAME_UPPER || name==CHARSET_NAME_UPPER)
1.101 paf 704: return;
1.290 misha 705:
1.345 moko 706: if(name==LAST_MODIFIED_NAME_UPPER)
707: info->add_last_modified=false;
708:
1.316 misha 709: const char* aname=String(name, String::L_URI).untaint_and_transcode_cstr(String::L_URI, &info->r.charsets);
1.101 paf 710:
1.267 paf 711: SAPI::add_header_attribute(info->r.sapi_info,
1.345 moko 712: aname,
1.316 misha 713: attributed_meaning_to_string(*value, String::L_URI, false).untaint_and_transcode_cstr(String::L_URI, &info->r.charsets)
1.303 misha 714: );
1.267 paf 715: }
716:
1.366 moko 717: static void output_sole_piece(Request& r, bool header_only, VFile& body_file, Value* body_file_content_type) {
1.267 paf 718: // transcode text body when "text/*" or simple result
719: String::C output(body_file.value_ptr(), body_file.value_size());
720: if(!body_file_content_type/*vstring.as_vfile*/ || body_file_content_type->as_string().pos("text/")==0)
1.366 moko 721: output=Charset::transcode(output, r.charsets.source(), r.charsets.client());
1.267 paf 722:
1.311 misha 723: // prepare header: Content-Length
724: SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, format(output.length, "%u"));
1.267 paf 725:
726: // send header
727: SAPI::send_header(r.sapi_info);
728:
729: // send body
730: if(!header_only)
731: SAPI::send_body(r.sapi_info, output.str, output.length);
732: }
733:
734: #ifndef DOXYGEN
735: struct Range
736: {
737: size_t start;
738: size_t end;
739: };
740: #endif
1.313 misha 741: static void parse_range(const String* s, Array<Range> &ar) {
1.267 paf 742: const char *p = s->cstr();
743: if(s->starts_with("bytes="))
744: p += 6;
745: Range r;
746: while(*p){
747: r.start = (size_t)-1;
748: r.end = (size_t)-1;
749: if(*p >= '0' && *p <= '9'){
750: r.start = atol(p);
751: while(*p>='0' && *p<='9') ++p;
752: }
753: if(*p++ != '-') break;
754: if(*p >= '0' && *p <= '9'){
755: r.end = atol(p);
756: while(*p>='0' && *p<='9') ++p;
757: }
758: if(*p == ',') ++p;
759: ar += r;
760: }
1.101 paf 761: }
1.267 paf 762:
1.366 moko 763: static void output_pieces(Request& r, bool header_only, const String& filename, size_t content_length, Value& date, bool add_last_modified) {
1.315 misha 764: SAPI::add_header_attribute(r.sapi_info, "accept-ranges", "bytes");
1.270 paf 765:
1.267 paf 766: const size_t BUFSIZE = 10*0x400;
767: char buf[BUFSIZE];
1.339 moko 768: const char *range = SAPI::Env::get(r.sapi_info, "HTTP_RANGE");
1.267 paf 769: size_t offset=0;
770: size_t part_length=content_length;
771: if(range){
772: Array<Range> ar;
773: parse_range(new String(range), ar);
774: size_t count = ar.count();
775: if(count == 1){
776: Range &rg = ar.get_ref(0);
777: if(rg.start == (size_t)-1 && rg.end == (size_t)-1){
1.311 misha 778: SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "416 Requested Range Not Satisfiable");
1.267 paf 779: return;
780: }
781: if(rg.start == (size_t)-1 && rg.end != (size_t)-1){
782: rg.start = content_length - rg.end;
783: rg.end = content_length;
784: offset += rg.start;
785: part_length = rg.end-rg.start;
786: }else if(rg.start != (size_t)-1 && rg.end == (size_t)-1){
787: rg.end = content_length-1;
788: offset += rg.start;
789: part_length -= rg.start;
790: }
791: if(part_length == 0){
1.311 misha 792: SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "204 No Content");
1.267 paf 793: return;
794: }
1.311 misha 795: SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206 Partial Content");
1.267 paf 796: snprintf(buf, BUFSIZE, "bytes %u-%u/%u", rg.start, rg.end, content_length);
1.315 misha 797: SAPI::add_header_attribute(r.sapi_info, "content-range", buf);
1.267 paf 798: }else if(count != 0){
1.311 misha 799: SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "501 Not Implemented");
1.267 paf 800: return;
801: }
802: }
803:
804:
1.311 misha 805: SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, format(part_length, "%u"));
1.267 paf 806:
1.314 misha 807: if(add_last_modified)
1.315 misha 808: SAPI::add_header_attribute(r.sapi_info, "last-modified", attributed_meaning_to_string(date, String::L_AS_IS, true).cstr());
1.314 misha 809:
1.267 paf 810: SAPI::send_header(r.sapi_info);
811:
812: const String& filespec=r.absolute(filename);
813:
814: size_t sent = 0;
815: if(!header_only){
816: size_t to_read = 0;
817: size_t size = 0;
818: do{
819: to_read = part_length<BUFSIZE?part_length:BUFSIZE;
820: File_read_result read_result=file_read(r.charsets, filespec,
821: false /*as text*/, 0/*params*/, true/*fail on problem*/,
822: buf, offset, to_read);
823: to_read=read_result.length;
824: if(to_read == 0)
825: break;
826: offset += to_read;
827:
828: size = SAPI::send_body(r.sapi_info, read_result.str, to_read);
829: sent += size;
830: if(size != to_read)
831: break;
832: part_length -= to_read;
833: }while(part_length);
834: }
835: }
836:
1.248 paf 837: void Request::output_result(VFile* body_file, bool header_only, bool as_attachment) {
1.51 paf 838: // header: cookies
1.248 paf 839: cookie.output_result(sapi_info);
1.51 paf 840:
1.314 misha 841: // _file_ content-type might be specified
1.267 paf 842: Value* body_file_content_type=body_file->fields().get(content_type_name);
1.92 paf 843:
1.314 misha 844: // Content-Disposition
1.267 paf 845: Value* vfile_name=body_file->fields().get(name_name);
846: if(!vfile_name) {
847: vfile_name=body_file->fields().get(response_body_file_name);
1.313 misha 848: if(vfile_name) {
849: char* name_cstr=vfile_name->as_string().cstrm();
1.274 paf 850: if(char *after_slash=rsplit(name_cstr, '\\'))
851: name_cstr=after_slash;
852: if(char *after_slash=rsplit(name_cstr, '/'))
853: name_cstr=after_slash;
1.327 misha 854: vfile_name=new VString(*new String(name_cstr));
1.274 paf 855: }
1.267 paf 856: }
857: if(vfile_name) {
858: const String& sfile_name=vfile_name->as_string();
859: if(sfile_name!=NONAME_DAT) {
1.283 misha 860: VHash& hash=*new VHash();
861: HashStringValue &h=hash.hash();
1.288 misha 862: h.put(value_name, new VString( as_attachment ? content_disposition_attachment : content_disposition_inline ));
1.343 moko 863: h.put(content_disposition_filename_name, new VString(*new String(sfile_name, String::L_HTTP_HEADER)));
1.283 misha 864:
1.344 moko 865: response.fields().put(content_disposition_name_upper, &hash);
1.267 paf 866:
867: if(!body_file_content_type)
868: body_file_content_type=new VString(mime_type_of(sfile_name.cstr()));
1.111 paf 869: }
1.267 paf 870: }
871:
1.312 misha 872: // set Content-Type
1.314 misha 873: if(body_file_content_type) {
874: // body file content type
1.344 moko 875: response.fields().put(content_type_name_upper, body_file_content_type);
1.314 misha 876: } else {
877: // default content type
1.344 moko 878: response.fields().put_dont_replace(content_type_name_upper, new VString(*new String(DEFAULT_CONTENT_TYPE)));
1.314 misha 879: }
1.312 misha 880:
1.314 misha 881: // prepare header: $response:fields without :body, :download and :charset
1.267 paf 882: Add_header_attribute_info info(*this);
1.281 paf 883: response.fields().for_each<Add_header_attribute_info*>(add_header_attribute, &info);
1.50 paf 884:
1.267 paf 885: if(Value* vresponse_body_file=body_file->fields().get(response_body_file_name)) {
1.314 misha 886: // $response:[download|body][$.file[filespec]] -- optput specified file
1.267 paf 887: const String& sresponse_body_file=vresponse_body_file->as_string();
1.367 moko 888: uint64_t content_length=0;
1.267 paf 889: time_t atime=0, mtime=0, ctime=0;
1.366 moko 890: file_stat(absolute(sresponse_body_file), content_length, atime, mtime, ctime);
1.267 paf 891:
892: VDate* vdate=0;
893: if(Value* v=body_file->fields().get("mdate")) {
1.308 misha 894: if(Value* vdatep=v->as(VDATE_TYPE))
1.267 paf 895: vdate=static_cast<VDate*>(vdatep);
896: else
1.286 misha 897: throw Exception(PARSER_RUNTIME, 0, "mdate must be a date");
1.267 paf 898: }
899: if(!vdate)
1.346 moko 900: vdate=new VDate((pa_time_t)mtime);
1.62 paf 901:
1.368 moko 902: output_pieces(*this, header_only, sresponse_body_file, (size_t)content_length, *vdate, info.add_last_modified);
1.267 paf 903: } else {
1.314 misha 904: if(body_file_content_type)
905: if(HashStringValue *hash=body_file_content_type->get_hash())
906: body_file_content_type=hash->get(value_name);
907:
1.366 moko 908: output_sole_piece(*this, header_only, *body_file, body_file_content_type);
1.267 paf 909: }
1.50 paf 910: }
1.104 paf 911:
1.328 misha 912: const String& Request::mime_type_of(const String* file_name) {
913: return mime_type_of(file_name?file_name->taint_cstr(String::L_FILE_SPEC):0);
914: }
915:
1.248 paf 916: const String& Request::mime_type_of(const char* user_file_name_cstr) {
1.104 paf 917: if(mime_types)
1.248 paf 918: if(const char* cext=strrchr(user_file_name_cstr, '.')) {
919: String sext(++cext);
1.246 paf 920: Table::Action_options options;
1.342 moko 921: if(mime_types->locate(0, sext.change_case(charsets.source(), String::CC_LOWER), options)) {
1.313 misha 922: if(const String* result=mime_types->item(1))
923: return *result;
1.104 paf 924: else
1.366 moko 925: throw Exception(PARSER_RUNTIME, 0, MIME_TYPES_NAME " table column elements must not be empty");
1.342 moko 926: }
1.104 paf 927: }
1.248 paf 928:
929: return *new String("application/octet-stream");
930: }
931:
1.365 moko 932: const String* Request::get_used_filespec(uint file_no){
1.318 misha 933: if(file_no < file_list.count())
934: return new String(file_list[file_no], String::L_TAINTED);
935: return 0;
936: }
937:
1.248 paf 938: #ifdef XML
1.280 paf 939: xmlChar* Request::transcode(const String& s) {
1.248 paf 940: return charsets.source().transcode(s);
941: }
942:
1.280 paf 943: xmlChar* Request::transcode(const String::Body s) {
1.248 paf 944: return charsets.source().transcode(s);
945: }
946:
1.280 paf 947: const String& Request::transcode(const xmlChar* s) {
1.248 paf 948: return charsets.source().transcode(s);
1.233 paf 949: }
1.248 paf 950: #endif
951:
1.349 moko 952: Request::Exception_details Request::get_details(const Exception& e) {
953: const String* problem_source=e.problem_source();
954: VHash& vhash=*new VHash; HashStringValue& hash=vhash.hash();
955: Operation::Origin origin={0, 0, 0};
1.248 paf 956:
1.349 moko 957: if(!exception_trace.is_empty()) {
958: Trace bottom=exception_trace.bottom_value();
1.350 moko 959: origin=bottom.origin();
960: if(!problem_source) { // we don't know who trigged the bug
961: problem_source=bottom.name(); // we usually know source of next-from-throw-point exception did that
962: exception_trace.set_bottom_index(exception_trace.bottom_index()+1);
963: } else if (bottom.name()==problem_source) { // it is that same guy?
1.349 moko 964: exception_trace.set_bottom_index(exception_trace.bottom_index()+1); // throw away that trace
965: } else {
966: // stack top contains not us, leaving intact to help ^throw
1.279 paf 967: }
1.248 paf 968: }
969:
970: // $.type
971: if(const char* type=e.type(true))
972: hash.put(exception_type_part_name, new VString(*new String(type)));
973:
974: // $.source
1.349 moko 975: if(problem_source)
976: hash.put(exception_source_part_name, new VString(*new String(*problem_source, String::L_TAINTED)));
1.248 paf 977:
1.349 moko 978: // $.file $.lineno $.colno
979: if(origin.file_no){
1.347 moko 980: hash.put("file", new VString(*new String(file_list[origin.file_no], String::L_TAINTED)));
981: hash.put("lineno", new VInt(1+origin.line));
982: hash.put("colno", new VInt(1+origin.col));
1.248 paf 983: }
984:
985: // $.comment
986: if(const char* comment=e.comment(true))
1.349 moko 987: hash.put(exception_comment_part_name, new VString(*new String(comment, String::L_TAINTED)));
1.248 paf 988:
989: // $.handled(0)
1.299 misha 990: hash.put(exception_handled_part_name, &VBool::get(false));
1.233 paf 991:
1.349 moko 992: return Request::Exception_details(origin, problem_source, vhash);
1.133 parser 993: }
1.337 moko 994:
995: Temp_value_element::Temp_value_element(Request& arequest, Value& awhere, const String& aname, Value* awhat) :
996: frequest(arequest),
997: fwhere(awhere),
998: fname(aname),
999: saved(awhere.get_element(aname))
1000: {
1001: Junction* junction;
1002: if(saved && (junction=saved->get_junction()) && junction->is_getter)
1003: saved=0;
1004: frequest.put_element(fwhere, aname, awhat);
1005: }
1006:
1007: Temp_value_element::~Temp_value_element() {
1008: frequest.put_element(fwhere, fname, saved ? saved : VVoid::get());
1009: }
E-mail: