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