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