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