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