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