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