Annotation of parser3/src/main/pa_request.C, revision 1.462
1.54 paf 1: /** @file
1.55 paf 2: Parser: request class main part. @see compile.C and execute.C.
3:
1.439 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.418 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <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.372 moko 10: #include "pa_os.h"
1.1 paf 11: #include "pa_request.h"
1.3 paf 12: #include "pa_wwrapper.h"
13: #include "pa_vclass.h"
1.31 paf 14: #include "pa_globals.h"
1.30 paf 15: #include "pa_vint.h"
1.297 misha 16: #include "pa_vmethod_frame.h"
1.32 paf 17: #include "pa_types.h"
1.248 paf 18: #include "pa_venv.h"
19: #include "pa_vmath.h"
20: #include "pa_vstatus.h"
21: #include "pa_vrequest.h"
1.78 paf 22: #include "pa_vtable.h"
1.90 paf 23: #include "pa_vfile.h"
1.147 parser 24: #include "pa_dictionary.h"
1.208 paf 25: #include "pa_charset.h"
1.186 paf 26: #include "pa_charsets.h"
1.248 paf 27: #include "pa_cache_managers.h"
1.402 moko 28: #include "pa_http.h"
1.248 paf 29: #include "pa_vmail.h"
30: #include "pa_vform.h"
31: #include "pa_vcookie.h"
32: #include "pa_vresponse.h"
33: #include "pa_vmemory.h"
1.253 paf 34: #include "pa_vconsole.h"
1.267 paf 35: #include "pa_vdate.h"
1.424 moko 36: #include "pa_varray.h"
1.458 moko 37: #include "pa_code_cache.h"
1.159 parser 38:
1.462 ! moko 39: volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.461 2026/10/13 16:14:53 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H;
1.329 moko 40:
1.248 paf 41: // consts
42:
1.269 paf 43: #define UNHANDLED_EXCEPTION_METHOD_NAME "unhandled_exception"
1.207 paf 44:
1.82 paf 45: /// content type of response when no $MAIN:defaults.content-type defined
1.248 paf 46: const char* DEFAULT_CONTENT_TYPE="text/html";
47:
1.449 moko 48: // One recursion level uses ~0.85KB of stack under linux/gcc, ~1KB under win32 and ~2KB under win64.
49: // MSVC win64 exception handling requires an extra ~10KB !!! for each throw, and execute() catches and
50: // rethrows on every level to provide a stack trace.
1.450 moko 51: // So 1000 levels need ~1MB under linux (default 8MB stack is enough) and ~2+10=12MB under win64.
52: // Worse, win64 keeps the unwound frames until the catch block returns, so a handler running away
53: // in its turn starts on top of them and needs one more such budget. The stack is set to 40MB in
54: // the MSVC project settings to allow nested three runaways.
1.449 moko 55: const int EXECUTE_RECURSION_LIMIT=1000;
56: const int EXECUTE_RECURSION_RESERVE=100;
57:
1.446 moko 58: const int LOOP_LIMIT=100000;
59: const int ARRAY_LIMIT=1000000;
60: const int HTTPD_TIMEOUT=4;
1.369 moko 61: const size_t FILE_SIZE_LIMIT=512*1024*1024;
1.361 moko 62:
1.248 paf 63: // defines for globals
64:
65: #define MAIN_METHOD_NAME "main"
66: #define AUTO_METHOD_NAME "auto"
1.364 moko 67: #define USE_METHOD_NAME "use"
1.309 misha 68: #define AUTOUSE_METHOD_NAME "autouse"
1.364 moko 69:
70: #define ORIGIN_KEY "origin"
71:
1.248 paf 72: // globals
73:
74: const String main_method_name(MAIN_METHOD_NAME);
75: const String auto_method_name(AUTO_METHOD_NAME);
1.364 moko 76: static const String use_method_name(USE_METHOD_NAME);
77: static const String autouse_method_name(AUTOUSE_METHOD_NAME);
1.311 misha 78:
1.364 moko 79: static const String origin_key(ORIGIN_KEY);
80:
1.369 moko 81: int pa_loop_limit=LOOP_LIMIT;
1.425 moko 82: int pa_array_limit=ARRAY_LIMIT;
1.427 moko 83: int pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT;
1.390 moko 84: int pa_httpd_timeout=HTTPD_TIMEOUT;
1.369 moko 85: size_t pa_file_size_limit=FILE_SIZE_LIMIT;
1.361 moko 86:
1.248 paf 87: // defines for statics
88:
89: #define MIME_TYPES_NAME "MIME-TYPES"
90: #define CLASS_PATH_NAME "CLASS_PATH"
91:
1.344 moko 92: #define DOWNLOAD_NAME_UPPER "DOWNLOAD"
93: #define BODY_NAME_UPPER "BODY"
94:
1.248 paf 95: // statics
96:
97: static const String main_class_name(MAIN_CLASS_NAME);
98: static const String mime_types_name(MIME_TYPES_NAME);
1.395 moko 99: static const String class_path_name(CLASS_PATH_NAME);
1.361 moko 100:
1.395 moko 101: static const String charsets_name("CHARSETS");
102: static const String strict_vars_name("STRICT-VARS");
103: static const String prototype_name("OBJECT-PROTOTYPE");
1.415 moko 104: static const String getter_protected_name("CLASS-GETTER-PROTECTED");
105: static const String locals_name("LOCALS");
1.395 moko 106: static const String limits_name("LIMITS");
107: static const String loop_limit_name("max_loop");
1.425 moko 108: static const String array_limit_name("max_array_size");
1.427 moko 109: static const String recursion_limit_name("max_recursion");
1.395 moko 110: static const String file_size_limit_name("max_file_size");
111: static const String lock_wait_timeout_name("lock_wait_timeout");
112: static const String httpd_name("HTTPD");
113: static const String httpd_timeout_name("timeout");
1.402 moko 114: static const String httpd_mode_name("mode");
1.458 moko 115: #ifdef CODE_CACHE
116: static const String httpd_code_cache_name("code-cache");
117: #endif
1.395 moko 118:
119: static const String conf_method_name("conf");
120: static const String post_process_method_name("postprocess");
121: static const String response_body_file_name("file");
1.248 paf 122:
1.344 moko 123: static const String download_name_upper(DOWNLOAD_NAME_UPPER);
124: static const String body_name_upper(BODY_NAME_UPPER);
125:
126: // more static
127:
128: static const String content_type_name_upper(HTTP_CONTENT_TYPE_UPPER);
129: static const String content_disposition_name_upper(CONTENT_DISPOSITION_UPPER);
1.392 moko 130: static const String content_disposition_inline(CONTENT_DISPOSITION_INLINE);
1.344 moko 131: static const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT);
132:
1.420 moko 133: const String content_disposition_filename_name(CONTENT_DISPOSITION_FILENAME_NAME);
134: const String content_disposition_filename_name_asterisk("filename*");
135:
1.248 paf 136: // defines
1.82 paf 137:
1.344 moko 138: #define CHARSET_NAME_UPPER "CHARSET"
139: #define LAST_MODIFIED_NAME_UPPER "LAST-MODIFIED"
140:
1.233 paf 141: // op.C
1.248 paf 142: VStateless_class& VClassMAIN_create();
1.123 paf 143:
1.157 parser 144: //
1.445 moko 145: void Request::recursion_limit_reached() {
146: if(execute_recursion_limit>pa_execute_recursion_limit)
147: throw Recursion_limit_exception();
148:
149: execute_recursion_limit+=EXECUTE_RECURSION_RESERVE;
150: throw Exception(PARSER_RUNTIME, 0, "call canceled - endless recursion detected");
151: }
152:
1.408 moko 153: Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, String::Language adefault_lang):
1.248 paf 154: // private
1.427 moko 155: anti_endless_execute_recursion(0),
1.445 moko 156: execute_recursion_limit(pa_execute_recursion_limit),
1.248 paf 157:
158: // public
1.335 moko 159: allow_class_replace(false),
1.248 paf 160: method_frame(0),
161: rcontext(0),
162: wcontext(0),
1.191 paf 163: fconnection(0),
1.371 moko 164: fin_cycle(0),
1.307 misha 165: fskip(SKIP_NOTHING),
1.248 paf 166:
167: // public
1.257 paf 168: request_info(arequest_info),
1.248 paf 169: sapi_info(asapi_info),
1.359 moko 170: charsets(pa_UTF8_charset, pa_UTF8_charset, pa_UTF8_charset), // default charsets
1.248 paf 171:
172: main_class(VClassMAIN_create()),
1.260 paf 173: form(*new VForm(charsets, arequest_info)),
1.248 paf 174: mail(*new VMail),
1.462 ! moko 175: response(*new VResponse(arequest_info, charsets, adefault_lang)),
1.298 misha 176: cookie(*new VCookie(charsets, arequest_info)),
1.285 misha 177: console(*new VConsole),
1.248 paf 178:
179: // private
180: configure_admin_done(false),
181:
182: // private mime types
1.460 moko 183: mime_types(0),
184: fheaders_sent(false)
1.157 parser 185: {
1.261 paf 186: pa_register_thread_request(*this);
187:
1.248 paf 188: // file_no=0 => unknown
1.434 moko 189: file_list+=String::Body("UNKNOWN");
190: file_list+=String::Body("-body of process-"); // pseudo_file_no__process
1.458 moko 191: #ifdef CODE_CACHE
192: Code_cache_manager::add_files(file_list);
193: #endif
1.186 paf 194:
1.178 paf 195: // maybe expire old caches
1.264 paf 196: cache_managers->maybe_expire();
1.178 paf 197:
1.157 parser 198: /// directly used
1.233 paf 199: // MAIN class, operators
1.353 moko 200: put_class(&main_class);
1.157 parser 201: // classes:
202: // table, file, random, mail, image, ...
1.248 paf 203: methoded_array().register_directly_used(*this);
1.157 parser 204:
205: /// methodless
1.354 moko 206:
1.157 parser 207: // env class
1.353 moko 208: put_class(new VEnv(asapi_info));
1.175 paf 209: // status class
1.353 moko 210: put_class(new VStatus());
1.157 parser 211: // request class
1.353 moko 212: put_class(new VRequest(arequest_info, charsets, form, asapi_info));
1.157 parser 213: // cookie class
1.353 moko 214: put_class(&cookie);
1.253 paf 215: // console class
1.353 moko 216: put_class(&console);
1.354 moko 217:
1.157 parser 218: /// methoded
1.354 moko 219:
1.157 parser 220: // response class
1.353 moko 221: put_class(&response);
1.157 parser 222: // form class
1.353 moko 223: put_class(&form);
1.213 paf 224: // mail class
1.353 moko 225: put_class(&mail);
1.157 parser 226: // math class
1.353 moko 227: put_class(new VMath);
1.248 paf 228: // memory class
1.353 moko 229: put_class(new VMemory);
1.117 paf 230: }
1.192 paf 231:
232: Request::~Request() {
233: #ifdef XML
234: // if for some strange reason xml generic errors failed to be reported, free them up
1.248 paf 235: if(const char* xml_generic_errors=xmlGenericErrors()) {
236: SAPI::log(sapi_info, "warning: unreported xmlGenericErrors: %s", xml_generic_errors);
1.341 moko 237: pa_free((void *)xml_generic_errors);
1.192 paf 238: }
239: #endif
240: }
1.236 paf 241:
1.248 paf 242: Value& Request::get_self() { return method_frame/*always have!*/->self(); }
1.192 paf 243:
1.355 moko 244: VStateless_class* Request::get_class(const String& name){
245: VStateless_class* result=classes().get(name);
1.313 misha 246: if(!result)
1.375 moko 247: if(const Method *method=main_class.get_element_method(autouse_method_name)){
1.364 moko 248: Value *vname=new VString(name);
249: CONSTRUCTOR_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
250: frame.store_params(&vname, 1);
251: // we don't need the result
252: call(frame);
253: });
254: result=classes().get(name);
255: }
1.309 misha 256: return result;
257: }
258:
1.411 moko 259: VStateless_class& Request::get_class_ref(const String& name){
260: VStateless_class* result=get_class(name);
261: if(!result)
262: throw Exception(PARSER_RUNTIME, &name, "class is undefined");
263: return *result;
264: }
265:
1.424 moko 266: bool Request::add_class(const char* atype, VStateless_class *aclass){
1.436 moko 267: if(!classes().put_dont_replace(String::Body(atype), aclass))
268: return true;
1.437 moko 269: if(!allow_class_replace && strcmp(atype, VARRAY_TYPE))
270: return false;
1.436 moko 271: classes().remove(String::Body(atype));
1.424 moko 272: classes().put(atype, aclass);
273: return true;
274: }
275:
1.356 moko 276: static void load_charset(HashStringValue::key_type akey, HashStringValue::value_type avalue, Request_charsets* charsets) {
1.359 moko 277: pa_charsets.load_charset(*charsets, akey, avalue->as_string());
1.208 paf 278: }
1.356 moko 279:
1.395 moko 280:
281: #define CONF_OPTION(config, name, code, exception_name) \
282: if(config) \
283: if(Value* option=config->get_element(name)) { \
284: if(option->is_evaluated_expr()) { \
285: code; \
286: } else \
1.415 moko 287: throw Exception(PARSER_RUNTIME, 0, "$MAIN:" exception_name, name.cstr()); \
1.395 moko 288: }
289:
1.248 paf 290: void Request::configure_admin(VStateless_class& conf_class) {
1.208 paf 291: if(configure_admin_done)
1.356 moko 292: throw Exception(PARSER_RUNTIME, 0, "parser already configured");
1.208 paf 293: configure_admin_done=true;
294:
1.227 paf 295: // charsets must only be specified in method_frame config
1.208 paf 296: // so that users would not interfere
297:
298: /* $MAIN:CHARSETS[
299: $.charsetname1[/full/path/to/charset/file.cfg]
300: ...
301: ]
302: */
1.308 misha 303: if(Value* vcharsets=conf_class.get_element(charsets_name)) {
1.342 moko 304: if(!vcharsets->is_string()) {
1.248 paf 305: if(HashStringValue* charsets=vcharsets->get_hash())
1.281 paf 306: charsets->for_each<Request_charsets*>(load_charset, &this->charsets);
1.240 paf 307: else
1.415 moko 308: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be hash", charsets_name.cstr());
1.342 moko 309: }
1.208 paf 310: }
311:
1.330 moko 312: #ifdef STRICT_VARS
1.332 moko 313: VVoid::strict_vars=false;
1.330 moko 314: if(Value* strict_vars=conf_class.get_element(strict_vars_name)) {
315: if(strict_vars->is_bool())
316: VVoid::strict_vars=strict_vars->as_bool();
1.357 moko 317: else
1.415 moko 318: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", strict_vars_name.cstr());
1.357 moko 319: }
320: #endif
321:
322: #ifdef OBJECT_PROTOTYPE
323: VClass::prototype=true;
324: if(Value* prototype=conf_class.get_element(prototype_name)) {
325: if(prototype->is_bool())
326: VClass::prototype=prototype->as_bool();
327: else
1.415 moko 328: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", prototype_name.cstr());
1.330 moko 329: }
330: #endif
331:
1.415 moko 332: #ifdef CLASS_GETTER_UNPROTECTED
333: VClass::getter_protected=true;
334: if(Value* getter_protected=conf_class.get_element(getter_protected_name)) {
335: if(getter_protected->is_bool())
336: VClass::getter_protected=getter_protected->as_bool();
337: else
338: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", getter_protected_name.cstr());
339: }
340: #endif
341:
342: VStateless_class::gall_vars_local=false;
343: if(Value* locals=conf_class.get_element(locals_name)) {
344: if(locals->is_bool()){
345: VStateless_class::gall_vars_local=locals->as_bool();
346: main_class.set_all_vars_local();
347: } else
348: throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", locals_name.cstr());
349: }
350:
1.362 moko 351: Value* limits=conf_class.get_element(limits_name);
352:
1.361 moko 353: pa_loop_limit=LOOP_LIMIT;
1.395 moko 354: CONF_OPTION(limits, loop_limit_name, {
355: pa_loop_limit=option->as_int();
356: if(pa_loop_limit==0) pa_loop_limit=INT_MAX;
357: }, "LIMITS.%s must be int");
1.361 moko 358:
1.425 moko 359: pa_array_limit=ARRAY_LIMIT;
360: CONF_OPTION(limits, array_limit_name, {
361: pa_array_limit=option->as_int();
362: if(pa_array_limit==0) pa_array_limit=INT_MAX;
363: }, "LIMITS.%s must be int");
364:
1.427 moko 365: pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT;
366: CONF_OPTION(limits, recursion_limit_name, {
367: pa_execute_recursion_limit=option->as_int();
1.445 moko 368: if(pa_execute_recursion_limit<=0) pa_execute_recursion_limit=INT_MAX;
369: pa_execute_recursion_limit=min(pa_execute_recursion_limit, INT_MAX-EXECUTE_RECURSION_RESERVE);
1.395 moko 370: }, "LIMITS.%s must be int");
1.445 moko 371: execute_recursion_limit=pa_execute_recursion_limit;
1.390 moko 372:
1.369 moko 373: pa_file_size_limit=FILE_SIZE_LIMIT;
1.395 moko 374: CONF_OPTION(limits, file_size_limit_name, {
375: double limit=option->as_double();
376: if(limit >= (double)SSIZE_MAX)
1.448 moko 377: throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %s", file_size_limit_name.cstr(), pa_itoa(SSIZE_MAX));
1.395 moko 378: pa_file_size_limit=(size_t)limit;
379: if(pa_file_size_limit==0)
380: pa_file_size_limit=SSIZE_MAX;
381: }, "LIMITS.%s must be number");
1.361 moko 382:
1.372 moko 383: pa_lock_attempts=PA_LOCK_ATTEMPTS;
1.395 moko 384: CONF_OPTION(limits, lock_wait_timeout_name, {
385: double limit=option->as_double();
386: if(limit >= 3600*24)
1.426 moko 387: throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %d", lock_wait_timeout_name.cstr(), 3600*24);
1.395 moko 388: pa_lock_attempts=(unsigned int)(limit*2)+1;
389: }, "LIMITS.%s must be number");
390:
391: Value* httpd=conf_class.get_element(httpd_name);
392:
393: pa_httpd_timeout=HTTPD_TIMEOUT;
394: CONF_OPTION(httpd, httpd_timeout_name, {
395: pa_httpd_timeout=option->as_int();
396: if(pa_httpd_timeout==0) pa_httpd_timeout=INT_MAX;
397: }, "HTTPD.%s must be int");
1.372 moko 398:
1.403 moko 399: if(httpd)
400: if(Value* option=httpd->get_element( httpd_mode_name)) {
401: if(option->get_junction())
1.415 moko 402: throw Exception(PARSER_RUNTIME, 0, "$MAIN:HTTPD:mode must be string");
1.403 moko 403: HTTPD_Server::set_mode(option->as_string());
404: }
1.402 moko 405:
1.458 moko 406: #ifdef CODE_CACHE
407: CONF_OPTION(httpd, httpd_code_cache_name, {
408: Code_cache_manager::set_enabled(option->as_bool());
409: }, "HTTPD.%s must be bool");
410: #endif
411:
1.227 paf 412: // configure method_frame options
1.208 paf 413: // until someone with less privileges have overriden them
1.248 paf 414: methoded_array().configure_admin(*this);
1.208 paf 415: }
1.150 parser 416:
1.442 moko 417: const char* Request::get_exception_cstr(const Exception& e, VException& details) {
1.349 moko 418:
1.269 paf 419: #define PA_URI_FORMAT "%s: "
1.349 moko 420: #define PA_COMMENT_TYPE_FORMAT "%s [%s]"
421:
1.269 paf 422: #define PA_ORIGIN_FILE_POS_FORMAT "%s(%d:%d): "
423: #define PA_SOURCE_FORMAT "'%s' "
1.349 moko 424:
425: #define PA_ORIGIN_FILE_POS_VALUE file_list[details.origin.file_no].cstr(), 1+details.origin.line, 1+details.origin.col,
426: #define PA_SOURCE_VALUE details.problem_source->cstr(),
427:
428: #define EXCEPTION_CSTR(f1,v1,f2,v2) \
429: snprintf(result, MAX_STRING, \
430: PA_URI_FORMAT \
431: f1 f2 \
432: PA_COMMENT_TYPE_FORMAT, \
433: request_info.uri, \
434: v1 v2 \
435: e.comment(), e.type() \
436: );
437:
1.269 paf 438: char* result=new(PointerFreeGC) char[MAX_STRING];
439:
440: if(details.problem_source) { // do we know the guy?
1.349 moko 441: if(details.origin.file_no) // do whe know where he came from?
442: EXCEPTION_CSTR(PA_ORIGIN_FILE_POS_FORMAT, PA_ORIGIN_FILE_POS_VALUE, PA_SOURCE_FORMAT, PA_SOURCE_VALUE)
443: else
444: EXCEPTION_CSTR(PA_SOURCE_FORMAT, PA_SOURCE_VALUE,,)
445: } else {
446: if(details.origin.file_no) // do whe know where he came from?
447: EXCEPTION_CSTR(PA_ORIGIN_FILE_POS_FORMAT, PA_ORIGIN_FILE_POS_VALUE,,)
448: else
449: EXCEPTION_CSTR(,,,)
450: }
1.269 paf 451:
452: return result;
453: }
1.272 paf 454:
1.444 moko 455: Table &Request::Exception_trace::table(Request &r, const String* problem_source, size_t bottom, size_t top){
1.407 moko 456: // $stack[^table::create{name file lineno colno}]
457: Table::columns_type stack_trace_columns(new ArrayString);
458: *stack_trace_columns+=new String("name");
459: *stack_trace_columns+=new String("file");
460: *stack_trace_columns+=new String("lineno");
461: *stack_trace_columns+=new String("colno");
462: Table& stack_trace=*new Table(stack_trace_columns);
463:
1.444 moko 464: if(bottom<top)
465: for(size_t i=bottom+(get(bottom).name()==problem_source); i<top; i++) {
1.407 moko 466: Trace trace=get(i);
467: Table::element_type row(new ArrayString);
468:
469: *row+=trace.name(); // name column
470: Operation::Origin origin=trace.origin();
471: if(origin.file_no) {
472: *row+=new String(r.file_list[origin.file_no], String::L_TAINTED); // 'file' column
1.422 moko 473: *row+=new String(pa_uitoa(1+origin.line), String::L_CLEAN); // 'lineno' column
474: *row+=new String(pa_uitoa(1+origin.col), String::L_CLEAN); // 'colno' column
1.407 moko 475: }
476: stack_trace+=row;
477: }
478:
479: return stack_trace;
480: }
481:
1.272 paf 482: void Request::configure() {
483: // configure admin options if not configured yet
484: if(!configure_admin_done)
485: configure_admin(main_class);
486:
487: // $MAIN:MIME-TYPES
1.308 misha 488: if(Value* element=main_class.get_element(mime_types_name))
1.272 paf 489: if(Table *table=element->get_table())
1.395 moko 490: mime_types=table;
1.272 paf 491: }
1.408 moko 492:
1.459 moko 493: /// HEAD request: only the headers are sent
494: static bool is_header_only(Request_info& info) {
495: return info.method && strcasecmp(info.method, "HEAD")==0;
496: }
497:
1.64 paf 498: /**
499: load MAIN class, execute @main.
500: MAIN class consists of all the auto.p files we'd manage to find
501: plus
502: the file user requested us to process
503: all located classes become children of one another,
504: composing class we name 'MAIN'
1.180 paf 505:
506: @test log stack trace
507:
1.64 paf 508: */
1.459 moko 509: void Request::core(const char* config_filespec, const String &amain_method_name, const String* amain_class_name) {
510: bool header_only=is_header_only(request_info);
1.169 parser 511: try {
1.211 paf 512: // loading config
1.384 moko 513: if(config_filespec)
1.454 moko 514: use_file_directly(*new String(backslashes_to_slashes(config_filespec)));
1.8 paf 515:
1.338 moko 516: // filling mail received
517: mail.fill_received(*this);
518:
1.272 paf 519: try {
520: // compile requested file
1.389 moko 521: if(request_info.path_translated)
522: use_file_directly(*new String(request_info.path_translated, String::L_TAINTED), true, true /* load auto.p files */);
1.272 paf 523: configure();
524: } catch(...) {
525: configure(); // configure anyway, useful in @unhandled_exception [say, if they would want to mail by SMTP something]
526: rethrow;
527: }
1.25 paf 528:
1.412 moko 529: VStateless_class& main = amain_class_name ? get_class_ref(*amain_class_name) : main_class;
1.25 paf 530: // execute @main[]
1.412 moko 531: const String* body_string=amain_method_name.is_empty() ? &String::Empty : execute_method(main, amain_method_name);
1.41 paf 532: if(!body_string)
1.412 moko 533: throw Exception(PARSER_RUNTIME, &amain_method_name, "method not found in class %s", main.type());
1.79 paf 534:
1.460 moko 535: // ^response:send[] was used: only its parts are sent
536: if(fheaders_sent)
537: return;
538:
1.250 paf 539: // extract response body
1.344 moko 540: Value* body_value=response.fields().get(download_name_upper); // $response:download?
1.430 moko 541: bool as_attachment=body_value!=0;
1.250 paf 542: if(!body_value)
1.344 moko 543: body_value=response.fields().get(body_name_upper); // $response:body
1.250 paf 544: if(!body_value)
545: body_value=new VString(*body_string); // just result of ^main[]
546:
1.119 paf 547: // @postprocess
1.383 moko 548: if(const Method *method=main_class.get_method(post_process_method_name)) {
549: METHOD_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
550: frame.store_params(&body_value, 1);
551: call(frame);
552: body_value=&frame.result();
553: });
554: }
1.90 paf 555:
1.430 moko 556: // can throw exceptions while handling $response:download[]
1.462 ! moko 557: output_result(body_value->as_vfile(response.lang, &charsets), header_only, as_attachment);
1.171 parser 558: } catch(const Exception& e) { // request handling problem
1.258 paf 559:
1.445 moko 560: // OK, as native execution stack has unwound
561: anti_endless_execute_recursion=0;
562:
1.431 moko 563: // we're returning not result, but error explanation
1.442 moko 564: VException& details=*new VException(*this, e);
1.431 moko 565: const char* exception_cstr=get_exception_cstr(e, details);
566:
567: // reset response
568: response.fields().clear();
1.462 ! moko 569: response.lang=response.default_lang;
1.431 moko 570: SAPI::clear_headers(sapi_info);
1.407 moko 571:
1.431 moko 572: // this is what we'd return in $response:body
573: const String* body_string=0;
1.407 moko 574:
1.431 moko 575: try {
1.407 moko 576: // maybe we'd be lucky enough as to report an error in a gracefull way...
577: if(const Method *method=main_class.get_method(*new String(UNHANDLED_EXCEPTION_METHOD_NAME))) {
578: // preparing parameters to @unhandled_exception[exception;stack]
579:
1.443 moko 580: Table& stack_trace=exception_trace.table(*this, details.problem_source);
1.444 moko 581: details.expire_trace();
1.407 moko 582: 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)
583:
1.442 moko 584: Value *params[]={&details, new VTable(&stack_trace)};
1.407 moko 585: METHOD_FRAME_ACTION(*method, 0 /*no caller*/, main_class, {
586: frame.store_params(params, 2);
587: call(frame);
588: body_string=&frame.result().as_string();
589: });
590: }
1.383 moko 591:
1.407 moko 592: // conditionally log it
1.442 moko 593: Value* vhandled=details.handled();
1.407 moko 594: if(!vhandled || !vhandled->as_bool()) {
595: SAPI::log(sapi_info, "%s", exception_cstr);
596: }
1.431 moko 597: } catch(const Exception& e) { // exception in @unhandled_exception
1.442 moko 598: VException& details=*new VException(*this, e);
1.431 moko 599: // logging both initial and new exceptions
600: SAPI::log(sapi_info, "%s", exception_cstr);
601: SAPI::log(sapi_info, "Exception in @unhandled_exception at %s", get_exception_cstr(e, details));
1.460 moko 602: if(!fheaders_sent)
603: SAPI::send_error(sapi_info, "Exception in @unhandled_exception, details are available in Parser error log.");
1.431 moko 604: return;
605: }
1.383 moko 606:
1.431 moko 607: if(body_string) { // could report an error beautifully?
608: VString body_vstring(*body_string);
1.462 ! moko 609: output_result(body_vstring.as_vfile(response.lang, &charsets), header_only, false);
1.460 moko 610: } else if(!fheaders_sent) {
1.431 moko 611: // doing that ugly
612: SAPI::send_error(sapi_info, exception_cstr, strcmp(e.type(), "file.missing") ? "500" : "404");
1.268 paf 613: }
1.1 paf 614: }
615: }
616:
1.266 paf 617: uint Request::register_file(String::Body file_spec) {
1.248 paf 618: file_list+=file_spec;
619: return file_list.count()-1;
620: }
621:
1.387 moko 622: void Request::use_file_directly(const String& file_spec, bool fail_on_file_absence, bool with_auto_p) {
1.73 paf 623: // cyclic dependence check
1.318 misha 624: if(used_files.get(file_spec))
1.248 paf 625: return;
1.318 misha 626: used_files.put(file_spec, true);
627:
1.385 moko 628: if(!fail_on_file_absence && !entry_exists(file_spec)) // ignore file absence if asked for
629: return;
1.318 misha 630:
1.386 moko 631: if(with_auto_p) {
1.454 moko 632: // loading auto.p files from document_root/..
1.386 moko 633: // to the one beside requested file.
634: // all assigned bases from upper dir
635: const char* target=file_spec.cstr();
636:
637: // all relative paths are calculated from main document
638: request_info.path_translated=target;
639:
640: const char* after=target;
1.454 moko 641: size_t drlen=strlen(request_info.document_root_path);
1.457 moko 642: if(path_cmp(after, request_info.document_root_path, drlen)==0)
1.454 moko 643: after+=drlen; // to start from document_root/auto.p
1.386 moko 644:
645: while(const char* before=strchr(after, '/')) {
646: String& sfile_spec=*new String;
647: if(after!=target) {
648: sfile_spec.append_strdup(target, before-target, String::L_CLEAN);
649: sfile_spec << "/" AUTO_FILE_NAME;
650:
1.387 moko 651: use_file_directly(sfile_spec, false /*ignore absence, sole user*/);
1.386 moko 652: }
653: for(after=before+1;*after=='/';after++);
654: }
655: }
656:
1.458 moko 657: Code_cache* code=0;
658: #ifdef CODE_CACHE
659: if(Code_cache* cached=Code_cache_manager::get(file_spec)) {
660: use_buf(main_class, 0, 0, cached->file_no, 0, cached);
661: return;
662: }
663: // to record the compilation; only while the httpd config runs
664: code=Code_cache_manager::create(file_spec);
665: #endif
666:
1.385 moko 667: if(const char* source=file_read_text(charsets, file_spec, true))
1.458 moko 668: use_buf(main_class, source, 0, register_file(file_spec), 0, code);
1.318 misha 669: }
670:
671:
1.454 moko 672: void Request::use_file(const String& afile_name, const String* use_filespec/*absolute*/, bool with_auto_p) {
673: if(afile_name.is_empty())
1.366 moko 674: throw Exception(PARSER_RUNTIME, 0, "usage failed - no filename was specified");
1.326 misha 675:
1.318 misha 676: const String* filespec=0;
1.454 moko 677: const String& file_name=backslashes_to_slashes(afile_name);
1.318 misha 678:
1.454 moko 679: if(file_name.first_char()=='/' || path_scheme(afile_name) || is_os_absolute_path(afile_name)) // path is not relative, no need to scan MAIN:CLASS_PATH
680: filespec=&full_disk_path(afile_name);
1.318 misha 681: else if(use_filespec){ // search in current dir first
1.454 moko 682: size_t last_slash_pos=use_filespec->strrchr('/');
1.331 misha 683: if(last_slash_pos!=STRING_NOT_FOUND)
684: filespec=file_exist(use_filespec->mid(0, last_slash_pos), file_name); // found in current dir?
1.318 misha 685: }
686:
687: if(!filespec){
688: // prevent multiple scan CLASS_PATH for searching one file
689: if(searched_along_class_path.get(file_name))
690: return;
691: searched_along_class_path.put(file_name, true);
1.308 misha 692: if(Value* element=main_class.get_element(class_path_name)) {
1.233 paf 693: if(element->is_string()) {
1.388 moko 694: filespec=file_exist(full_disk_path(element->as_string()), file_name); // found at class_path?
1.233 paf 695: } else if(Table *table=element->get_table()) {
1.318 misha 696: for(size_t i=table->count(); i--; ) {
1.248 paf 697: const String& path=*(*table->get(i))[0];
1.388 moko 698: if(filespec=file_exist(full_disk_path(path), file_name))
1.233 paf 699: break; // found along class_path
700: }
701: } else
1.366 moko 702: throw Exception(PARSER_RUNTIME, 0, "$" CLASS_PATH_NAME " must be string or table");
1.318 misha 703: if(!filespec)
1.415 moko 704: throw Exception(PARSER_RUNTIME, &file_name, "not found along $MAIN:" CLASS_PATH_NAME);
1.318 misha 705: } else
1.415 moko 706: throw Exception(PARSER_RUNTIME, &file_name, "usage failed - no $MAIN:" CLASS_PATH_NAME " were specified");
1.148 parser 707: }
1.230 paf 708:
1.387 moko 709: use_file_directly(*filespec, true, with_auto_p);
1.16 paf 710: }
711:
1.364 moko 712: void Request::use_file(const String& file_name, const String* use_filespec/*absolute*/, Operation::Origin origin) {
1.349 moko 713: static String use("USE");
714: try {
1.364 moko 715: static VHash* voptions=new VHash();
1.376 moko 716: if(const Method *method=main_class.get_method(use_method_name)){
1.364 moko 717: Value *params[]={new VString(file_name), voptions};
718: voptions->hash().put(origin_key, new VString(*use_filespec));
719:
720: CONSTRUCTOR_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
721: frame.store_params(params, 2);
722: // we don't need the result
723: call(frame);
724: });
725: }
1.349 moko 726: } catch (...) {
727: exception_trace.push(Trace(&use, origin));
728: rethrow;
729: }
730: }
1.208 paf 731:
1.458 moko 732: void Request::use_buf(VStateless_class& aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset, Code_cache* code) {
1.358 moko 733: // temporary zero @conf to avoid it second execution
1.248 paf 734: Temp_method temp_method_conf(aclass, conf_method_name, 0);
1.358 moko 735: // temporary zero @auto to avoid it second execution
1.248 paf 736: Temp_method temp_method_auto(aclass, auto_method_name, 0);
1.233 paf 737:
1.295 misha 738: // compile loaded classes
1.458 moko 739: ArrayClass& cclasses=
740: #ifdef CODE_CACHE
741: !source ? code->replay(*this, aclass) :
742: #endif
743: compile(&aclass, source, main_alias, file_no, line_no_offset, code);
1.212 paf 744:
1.248 paf 745: VString* vfilespec=
1.435 moko 746: new VString(file_list[file_no]);
1.295 misha 747:
748: for(size_t i=0; i<cclasses.count(); i++){
749: VStateless_class& cclass=*cclasses.get(i);
1.296 misha 750:
751: // locate and execute possible @conf[] static
1.382 moko 752: if(execute_method_if_exists(cclass, conf_method_name, vfilespec))
1.295 misha 753: configure_admin(cclass/*, executed.method->name*/);
754:
755: // locate and execute possible @auto[] static
1.419 moko 756: execute_auto_method_if_exists(cclass, auto_method_name, vfilespec);
1.336 moko 757:
758: cclass.enable_default_setter();
1.295 misha 759: }
1.19 paf 760: }
761:
1.248 paf 762: const String& Request::relative(const char* apath, const String& relative_name) {
1.452 moko 763: // apath is a URI or a path already normalized by the target, always slash separated
1.341 moko 764: char *hpath=pa_strdup(apath);
1.248 paf 765: String& result=*new String;
766: if(rsplit(hpath, '/')) // if something/splitted
1.217 paf 767: result << hpath << "/";
1.248 paf 768: result << relative_name;
769: return result;
1.19 paf 770: }
771:
1.454 moko 772: void Request_info::set_document_root(const char* adocument_root) {
773: document_root=adocument_root;
774: char* path=backslashes_to_slashes(adocument_root);
775: if(path) {
776: size_t length=strlen(path);
777: while(length && path[length-1]=='/')
778: path[--length]=0;
779: }
780: document_root_path=path;
781: }
782:
1.453 moko 783: const String& Request::full_disk_path(const String& relative_name, uint allowed) {
1.454 moko 784: if(relative_name.first_char()!='/') {
785: if(uint scheme=path_scheme(relative_name)) {
786: check_path_scheme(scheme, relative_name, allowed);
787: if(scheme!=PA_SCHEME_FILE)
788: return relative_name; // an allowed uri is passed through
789: if(const String* path=file_uri_to_path(relative_name))
790: return backslashes_to_slashes(*path);
1.455 moko 791: #ifdef WIN32
792: throw Exception(PARSER_RUNTIME, &relative_name, "must be file:///path, file://localhost/path or file://server/share/path");
793: #else
1.454 moko 794: throw Exception(PARSER_RUNTIME, &relative_name, "must be file:///path or file://localhost/path");
1.455 moko 795: #endif
1.454 moko 796: }
797: // before '\' becomes '/', to avoid mixing the UNC path \\server with web path //server
798: if(is_os_absolute_path(relative_name))
799: return backslashes_to_slashes(relative_name);
1.453 moko 800: }
1.389 moko 801:
1.454 moko 802: const String& path=backslashes_to_slashes(relative_name);
803: if(path.first_char()!='/')
804: return relative(request_info.path_translated ? request_info.path_translated : pa_strcat(request_info.document_root_path, "/"), path);
805:
806: String& result=*new String(request_info.document_root_path);
807: result << path;
808: return result;
1.1 paf 809: }
1.45 paf 810:
1.453 moko 811: const String* Request::real_disk_path(const String& name) {
812: switch(path_scheme(name)) {
813: case PA_SCHEME_NONE: return &full_disk_path(name);
1.454 moko 814: case PA_SCHEME_FILE: {
1.456 moko 815: const String* path=clean_path_scheme(PA_SCHEME_FILE, name) ? file_uri_to_path(name) : 0;
1.454 moko 816: return path ? &backslashes_to_slashes(*path) : 0;
817: }
1.453 moko 818: default: return 0; // not a disk path
819: }
820: }
821:
1.267 paf 822: #ifndef DOXYGEN
823: class Add_header_attribute_info
824: {
825: public:
826: Add_header_attribute_info(Request& ar) : r(ar), add_last_modified(true) {}
827: Request& r;
828: bool add_last_modified;
829: };
830: #endif
1.344 moko 831: static void add_header_attribute(HashStringValue::key_type name, HashStringValue::value_type value, Add_header_attribute_info* info) {
832: if(name==BODY_NAME_UPPER || name==DOWNLOAD_NAME_UPPER || name==CHARSET_NAME_UPPER)
1.101 paf 833: return;
1.290 misha 834:
1.345 moko 835: if(name==LAST_MODIFIED_NAME_UPPER)
836: info->add_last_modified=false;
837:
1.316 misha 838: const char* aname=String(name, String::L_URI).untaint_and_transcode_cstr(String::L_URI, &info->r.charsets);
1.101 paf 839:
1.267 paf 840: SAPI::add_header_attribute(info->r.sapi_info,
1.345 moko 841: aname,
1.316 misha 842: attributed_meaning_to_string(*value, String::L_URI, false).untaint_and_transcode_cstr(String::L_URI, &info->r.charsets)
1.303 misha 843: );
1.267 paf 844: }
845:
1.366 moko 846: static void output_sole_piece(Request& r, bool header_only, VFile& body_file, Value* body_file_content_type) {
1.267 paf 847: // transcode text body when "text/*" or simple result
848: String::C output(body_file.value_ptr(), body_file.value_size());
849: if(!body_file_content_type/*vstring.as_vfile*/ || body_file_content_type->as_string().pos("text/")==0)
1.366 moko 850: output=Charset::transcode(output, r.charsets.source(), r.charsets.client());
1.267 paf 851:
1.311 misha 852: // prepare header: Content-Length
1.428 moko 853: SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, pa_uitoa(output.length));
1.267 paf 854:
855: // send header
1.430 moko 856: SAPI::send_headers(r.sapi_info);
1.267 paf 857:
858: // send body
859: if(!header_only)
860: SAPI::send_body(r.sapi_info, output.str, output.length);
861: }
862:
863: #ifndef DOXYGEN
864: struct Range
865: {
1.396 moko 866: uint64_t start;
867: uint64_t end;
1.267 paf 868: };
869: #endif
1.396 moko 870:
871: #define UNSET ((uint64_t)-1)
872:
1.313 misha 873: static void parse_range(const String* s, Array<Range> &ar) {
1.267 paf 874: const char *p = s->cstr();
875: if(s->starts_with("bytes="))
876: p += 6;
877: Range r;
878: while(*p){
1.396 moko 879: r.start = UNSET;
880: r.end = UNSET;
1.401 moko 881:
882: while(*p==' ' || *p=='\t') p++;
883:
1.267 paf 884: if(*p >= '0' && *p <= '9'){
1.416 moko 885: const char *n=p;
1.401 moko 886: while(*p>='0' && *p<='9') p++;
1.440 moko 887: r.start = pa_atoul(pa_strdup(n, p-n)); // ((uint64_t)-1) is treated as UNSET
1.267 paf 888: }
1.401 moko 889:
890: while(*p==' ' || *p=='\t') p++;
891:
1.267 paf 892: if(*p++ != '-') break;
1.401 moko 893:
894: while(*p==' ' || *p=='\t') p++;
895:
1.267 paf 896: if(*p >= '0' && *p <= '9'){
1.416 moko 897: const char *n=p;
1.401 moko 898: while(*p>='0' && *p<='9') p++;
1.440 moko 899: r.end = pa_atoul(pa_strdup(n, p-n)); // ((uint64_t)-1) is treated as UNSET
1.267 paf 900: }
1.401 moko 901:
902: while(*p==' ' || *p=='\t') p++;
903:
904: if(*p)
905: if(*p++ != ',') break;
906:
1.267 paf 907: ar += r;
908: }
1.101 paf 909: }
1.267 paf 910:
1.400 moko 911: struct Send_range_action_info {
912: Request *r;
913: uint64_t offset;
914: uint64_t part_length;
915: };
916:
917: static void send_range(struct stat& /*finfo*/, int f, const String& /*file_spec*/, void *context){
918: Send_range_action_info &info = *(Send_range_action_info*)context;
919:
1.430 moko 920: SAPI::send_headers(info.r->sapi_info);
1.400 moko 921: pa_lseek(f, info.offset, SEEK_SET);
922:
1.432 moko 923: char buf[FILE_BUFFER_SIZE];
1.400 moko 924: do{
1.432 moko 925: size_t to_read = info.part_length < FILE_BUFFER_SIZE ? (size_t)info.part_length : FILE_BUFFER_SIZE;
1.400 moko 926: size_t to_write = file_block_read(f, buf, to_read);
927:
928: if(to_write == 0)
929: break;
930:
931: size_t size = SAPI::send_body(info.r->sapi_info, buf, to_write);
932: if(size != to_write)
933: break;
934:
935: info.part_length -= to_write;
936: } while (info.part_length);
937: }
938:
1.396 moko 939: static void output_pieces(Request& r, bool header_only, const String& filename, uint64_t content_length, Value& date, bool add_last_modified) {
1.315 misha 940: SAPI::add_header_attribute(r.sapi_info, "accept-ranges", "bytes");
1.270 paf 941:
1.339 moko 942: const char *range = SAPI::Env::get(r.sapi_info, "HTTP_RANGE");
1.396 moko 943: uint64_t offset=0;
944: uint64_t part_length=content_length;
1.401 moko 945:
946: if(range && content_length){
1.267 paf 947: Array<Range> ar;
948: parse_range(new String(range), ar);
1.396 moko 949: int count = ar.count();
1.267 paf 950: if(count == 1){
951: Range &rg = ar.get_ref(0);
1.401 moko 952:
953: if(rg.start == UNSET && rg.end == UNSET)
954: return SAPI::send_error(r.sapi_info, "", "416");
955:
1.396 moko 956: if(rg.start == UNSET && rg.end != UNSET){
1.440 moko 957: if(rg.end == 0)
958: return SAPI::send_error(r.sapi_info, "", "416");
1.401 moko 959: if(rg.end > content_length)
960: rg.end = content_length;
1.404 moko 961: rg.start = content_length - rg.end;
962: rg.end = content_length-1;
1.401 moko 963: } else if(rg.start != UNSET && rg.end == UNSET){
964: if(rg.start >= content_length)
965: return SAPI::send_error(r.sapi_info, "", "416");
1.267 paf 966: rg.end = content_length-1;
1.401 moko 967: } else {
968: if(rg.start >= content_length || rg.start > rg.end)
969: return SAPI::send_error(r.sapi_info, "", "416");
970: if(rg.end >= content_length)
971: rg.end = content_length-1;
1.267 paf 972: }
1.401 moko 973:
974: offset = rg.start;
975: part_length = rg.end-rg.start+1;
976:
1.400 moko 977: char buf[MAX_STRING];
1.448 moko 978: snprintf(buf, MAX_STRING, "bytes %s-%s/%s", pa_uitoa(rg.start), pa_uitoa(rg.end), pa_uitoa(content_length));
1.401 moko 979: SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206");
1.315 misha 980: SAPI::add_header_attribute(r.sapi_info, "content-range", buf);
1.401 moko 981: } else {
1.410 moko 982: return SAPI::send_error(r.sapi_info, count ? "Multiple ranges are not supported" : "Invalid range", count ? "501" : "400");
1.267 paf 983: }
984: }
985:
1.428 moko 986: SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, pa_uitoa(part_length));
1.267 paf 987:
1.314 misha 988: if(add_last_modified)
1.315 misha 989: SAPI::add_header_attribute(r.sapi_info, "last-modified", attributed_meaning_to_string(date, String::L_AS_IS, true).cstr());
1.314 misha 990:
1.414 moko 991: if(header_only){
1.430 moko 992: SAPI::send_headers(r.sapi_info);
1.414 moko 993: } else {
1.400 moko 994: Send_range_action_info info = { &r, offset, part_length};
995: file_read_action_under_lock(r.full_disk_path(filename), "send", send_range, &info);
1.267 paf 996: }
997: }
998:
1.248 paf 999: void Request::output_result(VFile* body_file, bool header_only, bool as_attachment) {
1.460 moko 1000: if(fheaders_sent)
1001: return;
1002:
1.51 paf 1003: // header: cookies
1.248 paf 1004: cookie.output_result(sapi_info);
1.51 paf 1005:
1.394 moko 1006: // $.file and $.name (when body is real vfile)
1007: Value* vfile = body_file->fields().get(response_body_file_name);
1008: Value* vname = body_file->fields().get(name_name);
1009:
1010: const String* sfile = vfile ? &vfile->as_string() : NULL;
1011: const String* sname = vname ? &vname->as_string() : NULL;
1012:
1013: if(sname && *sname == NONAME_DAT)
1014: sname = NULL;
1.92 paf 1015:
1.409 moko 1016: // Content-Disposition, use $.name[<empty>] to avoid
1.394 moko 1017: const String* disposition_name = sname ? sname->is_empty() ? NULL : sname : sfile;
1018: if(disposition_name) {
1.420 moko 1019: String& filename = *new String(pa_filename(disposition_name->cstr()), String::L_URI);
1020: String& filename_asterisk = *new String(charsets.client().NAME(), String::L_CLEAN) << (*new String("''")) << filename;
1021:
1.394 moko 1022: VHash& hash=*new VHash();
1023: hash.hash().put(value_name, new VString(as_attachment ? content_disposition_attachment : content_disposition_inline));
1.420 moko 1024: hash.hash().put(content_disposition_filename_name, new VString(filename));
1025: hash.hash().put(content_disposition_filename_name_asterisk, new VString(filename_asterisk));
1.394 moko 1026: response.fields().put(content_disposition_name_upper, &hash);
1027: }
1.267 paf 1028:
1.394 moko 1029: // _file_ content-type might be specified
1030: Value* body_file_content_type = body_file->fields().get(content_type_name);
1031: if(!body_file_content_type){
1032: const String* scontent_name = sname && !sname->is_empty() ? sname : sfile;
1033: if(scontent_name)
1034: body_file_content_type = new VString(mime_type_of(scontent_name->cstr()));
1.267 paf 1035: }
1036:
1.312 misha 1037: // set Content-Type
1.314 misha 1038: if(body_file_content_type) {
1039: // body file content type
1.344 moko 1040: response.fields().put(content_type_name_upper, body_file_content_type);
1.314 misha 1041: } else {
1042: // default content type
1.344 moko 1043: response.fields().put_dont_replace(content_type_name_upper, new VString(*new String(DEFAULT_CONTENT_TYPE)));
1.314 misha 1044: }
1.312 misha 1045:
1.314 misha 1046: // prepare header: $response:fields without :body, :download and :charset
1.267 paf 1047: Add_header_attribute_info info(*this);
1.281 paf 1048: response.fields().for_each<Add_header_attribute_info*>(add_header_attribute, &info);
1.50 paf 1049:
1.394 moko 1050: if(sfile) {
1.314 misha 1051: // $response:[download|body][$.file[filespec]] -- optput specified file
1.367 moko 1052: uint64_t content_length=0;
1.267 paf 1053: time_t atime=0, mtime=0, ctime=0;
1.394 moko 1054: file_stat(full_disk_path(*sfile), content_length, atime, mtime, ctime);
1.267 paf 1055:
1056: VDate* vdate=0;
1057: if(Value* v=body_file->fields().get("mdate")) {
1.423 moko 1058: vdate=dynamic_cast<VDate*>(v);
1059: if(!vdate)
1.286 misha 1060: throw Exception(PARSER_RUNTIME, 0, "mdate must be a date");
1.267 paf 1061: }
1062: if(!vdate)
1.346 moko 1063: vdate=new VDate((pa_time_t)mtime);
1.62 paf 1064:
1.399 moko 1065: output_pieces(*this, header_only, *sfile, content_length, *vdate, info.add_last_modified);
1.267 paf 1066: } else {
1.314 misha 1067: if(body_file_content_type)
1068: if(HashStringValue *hash=body_file_content_type->get_hash())
1069: body_file_content_type=hash->get(value_name);
1070:
1.366 moko 1071: output_sole_piece(*this, header_only, *body_file, body_file_content_type);
1.267 paf 1072: }
1.50 paf 1073: }
1.104 paf 1074:
1.460 moko 1075: void Request::send(Value& part) {
1076: // converted before the headers: a stat-ed file throws here
1077: VFile* file=dynamic_cast<VFile*>(&part);
1.462 ! moko 1078: String::C output=file ? String::C(file->value_ptr(), file->value_size()) : String::C(part.get_string()->cstr_to_string_body_untaint(response.lang, 0, &charsets));
1.460 moko 1079: if(!file || file->is_text_mode())
1080: output=Charset::transcode(output, charsets.source(), charsets.client());
1081:
1082: if(!fheaders_sent) {
1083: fheaders_sent=true;
1084: // the headers: cookies and $response:fields, without Content-Length
1085: cookie.output_result(sapi_info);
1086: response.fields().put_dont_replace(content_type_name_upper, new VString(*new String(DEFAULT_CONTENT_TYPE)));
1087: Add_header_attribute_info info(*this);
1088: response.fields().for_each<Add_header_attribute_info*>(add_header_attribute, &info);
1089: SAPI::send_headers(sapi_info);
1090: }
1091:
1092: if(!is_header_only(request_info))
1093: SAPI::send_body(sapi_info, output.str, output.length);
1094: }
1095:
1.328 misha 1096: const String& Request::mime_type_of(const String* file_name) {
1097: return mime_type_of(file_name?file_name->taint_cstr(String::L_FILE_SPEC):0);
1098: }
1099:
1.248 paf 1100: const String& Request::mime_type_of(const char* user_file_name_cstr) {
1.104 paf 1101: if(mime_types)
1.248 paf 1102: if(const char* cext=strrchr(user_file_name_cstr, '.')) {
1103: String sext(++cext);
1.246 paf 1104: Table::Action_options options;
1.342 moko 1105: if(mime_types->locate(0, sext.change_case(charsets.source(), String::CC_LOWER), options)) {
1.313 misha 1106: if(const String* result=mime_types->item(1))
1107: return *result;
1.104 paf 1108: else
1.395 moko 1109: throw Exception(PARSER_RUNTIME, 0, MIME_TYPES_NAME " table column elements must not be empty");
1.342 moko 1110: }
1.104 paf 1111: }
1.248 paf 1112:
1113: return *new String("application/octet-stream");
1114: }
1115:
1.365 moko 1116: const String* Request::get_used_filespec(uint file_no){
1.318 misha 1117: if(file_no < file_list.count())
1118: return new String(file_list[file_no], String::L_TAINTED);
1119: return 0;
1120: }
1121:
1.248 paf 1122: #ifdef XML
1.280 paf 1123: xmlChar* Request::transcode(const String& s) {
1.248 paf 1124: return charsets.source().transcode(s);
1125: }
1126:
1.280 paf 1127: xmlChar* Request::transcode(const String::Body s) {
1.248 paf 1128: return charsets.source().transcode(s);
1129: }
1130:
1.280 paf 1131: const String& Request::transcode(const xmlChar* s) {
1.248 paf 1132: return charsets.source().transcode(s);
1.233 paf 1133: }
1.248 paf 1134: #endif
1135:
1.442 moko 1136: VException::VException(Request& r, const Exception& e):
1137: fexception(e), fhandled(&VBool::get(false)),
1.444 moko 1138: ftrace_bottom(r.exception_trace.bottom_index()), ftrace_top(r.exception_trace.top_index()),
1.442 moko 1139: origin(Operation::Origin::create(0, 0, 0)), problem_source(e.problem_source()) {
1140: Request::Exception_trace& exception_trace=r.exception_trace;
1.248 paf 1141:
1.349 moko 1142: if(!exception_trace.is_empty()) {
1.442 moko 1143: Request::Trace bottom=exception_trace.bottom_value();
1.350 moko 1144: origin=bottom.origin();
1.443 moko 1145: if(!problem_source) // we don't know who trigged the bug
1.350 moko 1146: problem_source=bottom.name(); // we usually know source of next-from-throw-point exception did that
1.248 paf 1147: }
1.442 moko 1148: if(origin.file_no)
1149: ffile=r.file_list[origin.file_no];
1150: }
1151:
1152: Value* VException::handled() {
1153: return fhash ? fhash->get(Symbols::HANDLED_SYMBOL) : fhandled;
1154: }
1.248 paf 1155:
1.444 moko 1156: Table& VException::stack(Request& r) {
1157: if(ftrace_top==size_t(-1))
1158: throw Exception(PARSER_RUNTIME, 0, "exception stack used outside of context");
1159: return r.exception_trace.table(r, problem_source, ftrace_bottom, ftrace_top);
1160: }
1161:
1.442 moko 1162: Value* VException::get_element(const String& name) {
1163: if(!fhash) {
1164: if(SYMBOLS_EQ(name,HANDLED_SYMBOL))
1165: return fhandled;
1166: if(SYMBOLS_EQ(name,TYPE_SYMBOL))
1167: return fexception.type(true) ? new VString(*new String(fexception.type())) : 0;
1168: if(SYMBOLS_EQ(name,SOURCE_SYMBOL))
1169: return problem_source ? new VString(*problem_source) : 0;
1170: if(SYMBOLS_EQ(name,COMMENT_SYMBOL))
1171: return fexception.comment(true) ? new VString(fexception.comment()) : 0;
1172: if(SYMBOLS_EQ(name,FILE_SYMBOL))
1173: return origin.file_no ? new VString(ffile) : 0;
1174: if(SYMBOLS_EQ(name,LINENO_SYMBOL))
1175: return origin.file_no ? new VInt(1+origin.line) : 0;
1176: if(SYMBOLS_EQ(name,COLNO_SYMBOL))
1177: return origin.file_no ? new VInt(1+origin.col) : 0;
1178: if(SYMBOLS_EQ(name,FIELDS_SYMBOL))
1179: return this;
1180: }
1181: // Computed field names need not be interned: use ordinary hash lookup.
1182: return VHashReference::get_element(name);
1183: }
1184:
1185: const VJunction* VException::put_element(const String& name, Value* value) {
1186: if(!fhash && SYMBOLS_EQ(name,HANDLED_SYMBOL)) {
1187: fhandled=value;
1188: return 0;
1189: }
1190: return VHashReference::put_element(name, value);
1191: }
1192:
1193: HashStringValue& VException::hash() {
1194: if(fhash)
1195: return *fhash;
1196: HashStringValue& result=*new HashStringValue;
1.248 paf 1197:
1.442 moko 1198: if(const char* type=fexception.type(true))
1199: result.put(Symbols::TYPE_SYMBOL, new VString(*new String(type)));
1.349 moko 1200: if(problem_source)
1.442 moko 1201: result.put(Symbols::SOURCE_SYMBOL, new VString(*problem_source));
1202: if(origin.file_no) {
1203: result.put(Symbols::FILE_SYMBOL, new VString(ffile));
1204: result.put(Symbols::LINENO_SYMBOL, new VInt(1+origin.line));
1205: result.put(Symbols::COLNO_SYMBOL, new VInt(1+origin.col));
1206: }
1207: if(const char* comment=fexception.comment(true))
1208: result.put(Symbols::COMMENT_SYMBOL, new VString(comment));
1209: result.put(Symbols::HANDLED_SYMBOL, fhandled);
1.248 paf 1210:
1.442 moko 1211: fhash=&result;
1212: return result;
1.133 parser 1213: }
1.337 moko 1214:
1215: Temp_value_element::Temp_value_element(Request& arequest, Value& awhere, const String& aname, Value* awhat) :
1216: frequest(arequest),
1217: fwhere(awhere),
1218: fname(aname),
1219: saved(awhere.get_element(aname))
1220: {
1221: Junction* junction;
1222: if(saved && (junction=saved->get_junction()) && junction->is_getter)
1223: saved=0;
1224: frequest.put_element(fwhere, aname, awhat);
1225: }
1226:
1227: Temp_value_element::~Temp_value_element() {
1228: frequest.put_element(fwhere, fname, saved ? saved : VVoid::get());
1229: }
E-mail: