Annotation of parser3/src/main/pa_request.C, revision 1.245.2.35
1.54 paf 1: /** @file
1.55 paf 2: Parser: request class main part. @see compile.C and execute.C.
3:
1.245.2.5 paf 4: Copyright (c) 2001-2003 ArtLebedev Group (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.245.2.35! paf 8: static const char* IDENT_REQUEST_C="$Date: 2003/03/08 16:37:19 $";
1.1 paf 9:
1.69 paf 10: #include "pa_sapi.h"
1.53 paf 11: #include "pa_common.h"
1.1 paf 12: #include "pa_request.h"
1.3 paf 13: #include "pa_wwrapper.h"
14: #include "pa_vclass.h"
1.31 paf 15: #include "pa_globals.h"
1.30 paf 16: #include "pa_vint.h"
1.112 paf 17: #include "pa_vmethod_frame.h"
1.32 paf 18: #include "pa_types.h"
1.245.2.27 paf 19: #include "pa_venv.h"
1.245.2.29 paf 20: #include "pa_vmath.h"
1.245.2.27 paf 21: #include "pa_vstatus.h"
22: #include "pa_vrequest.h"
1.78 paf 23: #include "pa_vtable.h"
1.90 paf 24: #include "pa_vfile.h"
1.147 parser 25: #include "pa_dictionary.h"
1.208 paf 26: #include "pa_charset.h"
1.186 paf 27: #include "pa_charsets.h"
1.245.2.4 paf 28: #include "pa_cache_managers.h"
1.245.2.34 paf 29: #include "pa_vmail.h"
1.159 parser 30:
1.245.2.3 paf 31: // consts
32:
1.245.2.5 paf 33: const char* UNHANDLED_EXCEPTION_METHOD_NAME="unhandled_exception";
1.207 paf 34:
1.82 paf 35: /// content type of exception response, when no @MAIN:exception handler defined
1.245.2.5 paf 36: const char* UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain";
1.82 paf 37:
38: /// content type of response when no $MAIN:defaults.content-type defined
1.245.2.5 paf 39: const char* DEFAULT_CONTENT_TYPE="text/html";
40: const char* ORIGINS_CONTENT_TYPE="text/plain";
1.82 paf 41:
1.245.2.6 paf 42: // defines for globals
43:
1.245.2.12 paf 44: #define MAIN_METHOD_NAME "main"
1.245.2.16 paf 45: #define AUTO_METHOD_NAME "auto"
1.245.2.14 paf 46: #define BODY_NAME "body"
1.245.2.12 paf 47:
1.245.2.3 paf 48: // globals
49:
1.245.2.4 paf 50: StringPtr main_method_name(new String(MAIN_METHOD_NAME));
1.245.2.16 paf 51: StringPtr auto_method_name(new String(AUTO_METHOD_NAME));
1.245.2.11 paf 52: StringPtr content_disposition_name(new String(CONTENT_DISPOSITION_NAME));
53: StringPtr content_disposition_value(new String(CONTENT_DISPOSITION_VALUE));
54: StringPtr content_disposition_filename_name(new String(CONTENT_DISPOSITION_FILENAME_NAME));
1.245.2.14 paf 55: StringPtr body_name(new String(BODY_NAME));
1.245.2.3 paf 56:
1.245.2.6 paf 57: // defines for statics
58:
59: #define CHARSETS_NAME "CHARSETS"
60: #define MIME_TYPES_NAME "MIME-TYPES"
61: #define ORIGINS_MODE_NAME "ORIGINS"
62: #define CONF_METHOD_NAME "conf"
63: #define POST_PROCESS_METHOD_NAME "postprocess"
64: #define DOWNLOAD_NAME "download"
65: #define CLASS_PATH_NAME "CLASS_PATH"
66:
67: // statics
68:
69: static StringPtr charsets_name(new String(CHARSETS_NAME));
70: static StringPtr main_class_name(new String(MAIN_CLASS_NAME));
71: static StringPtr mime_types_name(new String(MIME_TYPES_NAME));
72: static StringPtr origins_mode_name(new String(ORIGINS_MODE_NAME));
73: static StringPtr conf_method_name(new String(CONF_METHOD_NAME));
74: static StringPtr post_process_method_name(new String(POST_PROCESS_METHOD_NAME));
75: static StringPtr download_name(new String(DOWNLOAD_NAME));
76: static StringPtr class_path_name(new String(CLASS_PATH_NAME));
77:
1.245.2.7 paf 78: // defines
1.245.2.6 paf 79:
1.233 paf 80: // op.C
1.245.2.4 paf 81: VStateless_classPtr VClassMAIN_create();
1.195 paf 82: // op.C
1.245.2.16 paf 83: VHashPtr exception2vhash(Pool& pool, const Exception& e);
1.123 paf 84:
1.157 parser 85: //
1.245.2.18 paf 86: Request::Request(Pool& apool, SAPI_Info& asapi_info, Request_info& arequest_info,
1.245.2.28 paf 87: String_UL adefault_lang, bool status_allowed):
1.245.2.20 paf 88: // private
1.245.2.18 paf 89: fpool(apool),
1.245.2.20 paf 90: anti_endless_execute_recoursion(0),
91:
92: // public
93: method_frame(0),
94: rcontext(0),
95: wcontext(0),
96: flang(adefault_lang),
97: fconnection(0),
98: finterrupted(false),
99:
100: // public
101: #ifdef RESOURCES_DEBUG
102: sql_connect_time(0),
103: sql_request_time(0),
104: #endif
105:
106: // public
1.245.2.4 paf 107: sapi_info(asapi_info),
108: request_info(arequest_info),
1.245.2.6 paf 109: charsets(pool(), *UTF8_charset, *UTF8_charset, *UTF8_charset), // default charsets
110:
1.245.2.20 paf 111: main_class(VClassMAIN_create()),
1.245.2.6 paf 112: form(new VForm),
113: mail(new VMail),
1.245.2.20 paf 114: response(new VResponse(fpool, arequest_info, charsets)),
115: cookie(new VCookie),
116:
1.245.2.24 paf 117: // private
118: configure_admin_done(false),
119:
1.245.2.20 paf 120: // private defaults
121: fdefault_lang(adefault_lang),
122: // private mime types
123: mime_types(0)
1.245.2.4 paf 124: {
1.178 paf 125: // maybe expire old caches
1.245.2.4 paf 126: cache_managers.maybe_expire();
1.178 paf 127:
1.157 parser 128: /// directly used
1.233 paf 129: // MAIN class, operators
1.245.2.4 paf 130: classes().put(main_class->name(), main_class);
1.157 parser 131: // classes:
132: // table, file, random, mail, image, ...
1.245.2.24 paf 133: methoded_array().register_directly_used(*this);
1.157 parser 134:
135: /// methodless
136: // env class
1.245.2.4 paf 137: classes().put(StringPtr(new String(ENV_CLASS_NAME)),
1.245.2.27 paf 138: ValuePtr(new VEnv(pool(), asapi_info)));
1.175 paf 139: // status class
1.179 paf 140: if(status_allowed)
1.245.2.4 paf 141: classes().put(StringPtr(new String(STATUS_CLASS_NAME)),
1.245.2.12 paf 142: ValuePtr(new VStatus(pool())));
1.157 parser 143: // request class
1.245.2.4 paf 144: classes().put(StringPtr(new String(REQUEST_CLASS_NAME)),
145: ValuePtr(new VRequest(arequest_info, charsets)));
1.157 parser 146: // cookie class
1.245.2.6 paf 147: classes().put(StringPtr(new String(COOKIE_CLASS_NAME)), cookie);
1.157 parser 148:
149: /// methoded
150: // response class
1.245.2.6 paf 151: classes().put(response->get_class()->name(), response);
1.157 parser 152:
153: /// bases used
154: // form class
1.245.2.6 paf 155: classes().put(form->get_class()->base_class()->name(), form);
1.213 paf 156: // mail class
1.245.2.6 paf 157: classes().put(mail->get_class()->base_class()->name(), mail);
1.157 parser 158: // math class
1.245.2.4 paf 159: {
1.245.2.29 paf 160: ValuePtr math(new VMath);
1.245.2.4 paf 161: classes().put(math->get_class()->base_class()->name(), math);
162: }
1.117 paf 163: }
1.192 paf 164:
165: Request::~Request() {
166: #ifdef XML
167: // if for some strange reason xml generic errors failed to be reported, free them up
1.245.2.5 paf 168: if(const char* xml_generic_errors=xmlGenericErrors()) {
1.245.2.30 paf 169: SAPI::log(sapi_info, "warning: unreported xmlGenericErrors: %s", xml_generic_errors);
1.192 paf 170: free((void *)xml_generic_errors);
171: }
172: #endif
173: }
1.236 paf 174:
1.245.2.8 paf 175: ValuePtr Request::get_self() { return method_frame?method_frame->get_self():ValuePtr(0); }
1.192 paf 176:
1.245.2.6 paf 177: static void load_charset(HashStringValue::key_type akey, HashStringValue::value_type avalue, int) {
178: ::charsets.load_charset(akey, avalue->as_string(0));
1.208 paf 179: }
1.245.2.6 paf 180: void Request::configure_admin(VStateless_class& conf_class, StringPtr source) {
1.208 paf 181: if(configure_admin_done)
182: throw Exception("parser.runtime",
183: source,
184: "parser already configured");
185: configure_admin_done=true;
186:
1.227 paf 187: // charsets must only be specified in method_frame config
1.208 paf 188: // so that users would not interfere
189:
190: /* $MAIN:CHARSETS[
191: $.charsetname1[/full/path/to/charset/file.cfg]
192: ...
193: ]
194: */
1.245.2.6 paf 195: if(ValuePtr vcharsets=conf_class.get_element(charsets_name, conf_class, false)) {
1.240 paf 196: if(!vcharsets->is_string())
1.245.2.6 paf 197: if(HashStringValue* charsets=vcharsets->get_hash(StringPtr(0)))
198: charsets->for_each(load_charset, 0);
1.240 paf 199: else
200: throw Exception("parser.runtime",
1.245.2.6 paf 201: Exception::undefined_source,
1.240 paf 202: "$" MAIN_CLASS_NAME ":" CHARSETS_NAME " must be hash");
1.208 paf 203: }
204:
1.227 paf 205: // configure method_frame options
1.208 paf 206: // until someone with less privileges have overriden them
1.245.2.24 paf 207: methoded_array().configure_admin(*this);
1.208 paf 208: }
1.150 parser 209:
1.64 paf 210: /**
211: load MAIN class, execute @main.
212: MAIN class consists of all the auto.p files we'd manage to find
213: plus
214: the file user requested us to process
215: all located classes become children of one another,
216: composing class we name 'MAIN'
1.180 paf 217:
218: @test log stack trace
219:
1.64 paf 220: */
1.153 parser 221: void Request::core(
1.245.2.5 paf 222: const char* config_filespec, bool config_fail_on_read_problem,
1.62 paf 223: bool header_only) {
1.183 paf 224:
225: #ifdef RESOURCES_DEBUG
226: //measures
227: struct timeval mt[10];
228: //measure:before all
229: gettimeofday(&mt[0],NULL);
230: #endif
1.169 parser 231: try {
1.211 paf 232: // loading config
233: if(config_filespec) {
1.245.2.6 paf 234: StringPtr filespec(new String);
235: filespec->APPEND_CLEAN(config_filespec, 0, "config", 0);
236: use_file(*main_class,
1.233 paf 237: filespec, true/* ignore class_path */,
238: config_fail_on_read_problem, true/* file must exist if 'fail on read problem' not set */);
1.29 paf 239: }
1.8 paf 240:
1.72 paf 241: // loading auto.p files from document_root/..
242: // to the one beside requested file.
243: // all assigned bases from upper dir
244: {
1.245.2.6 paf 245: const char* after=request_info.path_translated;
246: size_t drlen=strlen(request_info.document_root);
247: if(memcmp(after, request_info.document_root, drlen)==0) {
1.115 paf 248: after+=drlen;
249: if(after[-1]=='/')
250: --after;
251: }
252:
1.152 parser 253: int step=0;
1.245.2.5 paf 254: while(const char* before=strchr(after, '/')) {
1.245.2.6 paf 255: StringPtr sfile_spec(new String);
256: if(after!=request_info.path_translated) {
257: sfile_spec->APPEND_CLEAN(
258: request_info.path_translated, before-request_info.path_translated,
1.152 parser 259: "path-translated-scanned", step++);
1.245.2.6 paf 260: *sfile_spec << "/" AUTO_FILE_NAME;
1.152 parser 261:
1.245.2.6 paf 262: use_file(*main_class,
1.233 paf 263: sfile_spec, true/* ignore class_path */,
264: true/* fail on read problem */, false/* but ignore absence, sole user */);
1.115 paf 265: }
266: after=before+1;
1.72 paf 267: }
268: }
1.34 paf 269:
1.125 paf 270: // compile requested file
1.245.2.6 paf 271: StringPtr spath_translated(new String);
272: spath_translated->APPEND_TAINTED(request_info.path_translated, 0, "user-request", 0);
273: use_file(*main_class,
1.233 paf 274: spath_translated, true/* ignore class_path */,
275: true/* fail on read problem*/, true/* fail on abscence */);
1.214 paf 276:
1.227 paf 277: // configure method_frame options if not configured yet
1.214 paf 278: if(!configure_admin_done)
1.245.2.6 paf 279: configure_admin(*main_class, StringPtr(0));
1.7 paf 280:
1.227 paf 281: // configure not-method_frame=user options
1.245.2.24 paf 282: methoded_array().configure_user(*this);
1.124 paf 283:
1.85 paf 284: // $MAIN:MIME-TYPES
1.245.2.6 paf 285: if(ValuePtr element=main_class->get_element(mime_types_name, *main_class, false))
1.85 paf 286: if(Table *table=element->get_table())
287: mime_types=table;
1.102 paf 288:
1.124 paf 289: // filling form fields
1.245.2.10 paf 290: form->fill_fields_and_tables(pool(), charsets, request_info);
1.102 paf 291:
1.124 paf 292: // filling cookies
1.245.2.6 paf 293: cookie->fill_fields(pool(), request_info);
1.213 paf 294:
295: // filling mail received
1.245.2.35! paf 296: mail->fill_received(*this);
1.25 paf 297:
1.183 paf 298: #ifdef RESOURCES_DEBUG
299: //measure:after compile
300: gettimeofday(&mt[1],NULL);
301: #endif
1.25 paf 302: // execute @main[]
1.245.2.9 paf 303: StringPtr body_string=execute_virtual_method(main_class, main_method_name);
1.41 paf 304: if(!body_string)
1.197 paf 305: throw Exception("parser.runtime",
1.245.2.6 paf 306: Exception::undefined_source,
1.145 parser 307: "'"MAIN_METHOD_NAME"' method not found");
1.79 paf 308:
1.183 paf 309: #ifdef RESOURCES_DEBUG
310: //measure:after main
311: gettimeofday(&mt[2],NULL);
312: #endif
313:
1.245.2.6 paf 314: VStringPtr body_vstring_before_post_process(new VString(body_string));
315: VStringPtr body_vstring_after_post_process(body_vstring_before_post_process);
1.119 paf 316: // @postprocess
1.245.2.6 paf 317: if(ValuePtr value=main_class->get_element(post_process_method_name, *main_class, false))
318: if(JunctionPtr junction=value->get_junction())
1.91 paf 319: if(const Method *method=junction->method) {
320: // preparing to pass parameters to
1.119 paf 321: // @postprocess[data]
1.245.2.23 paf 322: VMethodFrame frame(pool(), method->name, *junction, 0/*no parent*/); frame.ref();
1.245.2.8 paf 323: frame.set_self(main_class);
1.91 paf 324:
1.245.2.6 paf 325: frame.store_param(body_vstring_before_post_process);
1.91 paf 326: body_vstring_after_post_process=
1.245.2.6 paf 327: VStringPtr(new VString(execute_method(frame, *method)));
1.91 paf 328: }
1.90 paf 329:
1.204 paf 330: bool lorigins_mode=origins_mode();
1.144 parser 331:
1.245.2.13 paf 332: VFilePtr body_file=body_vstring_after_post_process->as_vfile(pool(),
1.204 paf 333: String::UL_UNSPECIFIED, lorigins_mode);
1.41 paf 334:
1.45 paf 335: // extract response body
1.245.2.6 paf 336: ValuePtr body_value=response->fields().get(download_name);
1.244 paf 337: bool as_attachment=body_value!=0;
338: if(!body_value)
1.245.2.6 paf 339: body_value=response->fields().get(body_name);
1.244 paf 340:
1.216 paf 341: if(body_value) // there is some $response:body
1.245.2.6 paf 342: body_file=body_value->as_vfile(pool());
1.204 paf 343: else if(lorigins_mode)
1.245.2.6 paf 344: response->fields().put(content_type_name,
345: ValuePtr(new VString(StringPtr(new String(ORIGINS_CONTENT_TYPE)))));
1.45 paf 346:
1.183 paf 347: #ifdef RESOURCES_DEBUG
348: //measure:after postprocess
349: gettimeofday(&mt[3],NULL);
350: #endif
351:
1.45 paf 352: // OK. write out the result
1.245.2.7 paf 353: output_result(body_file, header_only, as_attachment);
1.183 paf 354:
355: #ifdef RESOURCES_DEBUG
356: //measure:after output_result
357: gettimeofday(&mt[9],NULL);
358: t[9]=mt[9].tv_sec+mt[9].tv_usec/1000000.0;
359:
360: double t[10];
361: for(int i=0;i<10;i++)
362: t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
363: //measure:log2 compile,main,postprocess,output
364: SAPI::log(pool(), "rmeasure: %s,%.2f,%.2f,%.2f %.2f,%.2f %.2f",
1.245.2.6 paf 365: request_info.uri,
1.183 paf 366: t[1]-t[0],
367: t[2]-t[1],
368: t[3]-t[2],
369: sql_connect_time,sql_request_time,
370: t[9]-t[3]
371: );
372: #endif
1.171 parser 373: } catch(const Exception& e) { // request handling problem
1.76 paf 374: // we're returning not result, but error explanation
1.169 parser 375: try {
1.76 paf 376: // log the beast
1.245.2.6 paf 377: if(StringPtr problem_source=e.problem_source())
378: SAPI::log(sapi_info,
1.199 paf 379: "%s: "
1.76 paf 380: #ifndef NO_STRING_ORIGIN
1.180 paf 381: ORIGIN_FILE_LINE_FORMAT": "
1.76 paf 382: #endif
1.197 paf 383: "'%s' %s [%s]",
1.245.2.6 paf 384: request_info.uri,
1.76 paf 385: #ifndef NO_STRING_ORIGIN
386: problem_source->origin().file?problem_source->origin().file:"global",
387: problem_source->origin().line,
388: #endif
1.245.2.14 paf 389: problem_source->cstr().get(),
1.76 paf 390: e.comment(),
1.206 paf 391: e.type()
1.76 paf 392: );
393: else
1.245.2.6 paf 394: SAPI::log(sapi_info,
1.199 paf 395: "%s: "
1.197 paf 396: "%s [%s]",
1.245.2.6 paf 397: request_info.uri,
1.206 paf 398: e.comment(), e.type()
1.180 paf 399: );
1.171 parser 400:
1.43 paf 401: // reset language to default
402: flang=fdefault_lang;
403:
404: // reset response
1.245.2.6 paf 405: response->fields().clear();
1.43 paf 406:
407: // this is what we'd return in $response:body
1.245.2.6 paf 408: StringPtr body_string(0);
1.43 paf 409:
1.233 paf 410: // maybe we'd be lucky enough as to report an error
411: // in a gracefull way...
1.245.2.6 paf 412: if(ValuePtr value=main_class->get_element(
413: StringPtr(new String(UNHANDLED_EXCEPTION_METHOD_NAME)),
414: *main_class,
1.233 paf 415: false)) {
1.245.2.6 paf 416: if(JunctionPtr junction=value->get_junction()) {
1.233 paf 417: if(const Method *method=junction->method) {
418: // preparing to pass parameters to
419: // @unhandled_exception[exception;stack]
1.245.2.23 paf 420: VMethodFrame frame(pool(), method->name, *junction, 0/*no caller*/); frame.ref();
1.245.2.8 paf 421: frame.set_self(main_class);
1.233 paf 422:
423: // $exception
1.245.2.16 paf 424: frame.store_param(exception2vhash(pool(), e));
1.233 paf 425: // $stack[^table::set{name origin}]
1.245.2.24 paf 426: Table::columns_type stack_trace_columns(new ArrayString);
1.245.2.6 paf 427: *stack_trace_columns+=StringPtr(new String("name"));
428: *stack_trace_columns+=StringPtr(new String("file"));
429: *stack_trace_columns+=StringPtr(new String("lineno"));
430: TablePtr stack_trace(new Table(Exception::undefined_source, stack_trace_columns));
431: Array_iterator<StringPtr> tracei(exception_trace);
1.233 paf 432: while(tracei.has_next()) {
1.245.2.26 paf 433: Table::element_type row(new ArrayString);
1.171 parser 434:
1.245.2.6 paf 435: StringPtr name=tracei.next();
436: *row+=name; // name column
1.171 parser 437: #ifndef NO_STRING_ORIGIN
1.245.2.6 paf 438: const String_fragment::Origin& origin=name->origin();
1.233 paf 439: if(origin.file) {
1.245.2.6 paf 440: *row+=StringPtr(new String(origin.file, 0, true)); // file column
1.245.2.25 paf 441: *row+=StringPtr(new String(
442: pool().format_integer(1+origin.line),
443: 0, true)); // line column
1.233 paf 444: }
1.171 parser 445: #endif
1.245.2.6 paf 446: *stack_trace+=row;
1.233 paf 447: }
1.245.2.6 paf 448: frame.store_param(ValuePtr(new VTable(stack_trace)));
1.171 parser 449:
1.233 paf 450: // future $response:body=
451: // execute ^unhandled_exception[exception;stack]
1.245.2.6 paf 452: body_string=execute_method(frame, *method);
1.233 paf 453: }
454: }
1.30 paf 455: }
456:
1.41 paf 457: if(!body_string) { // couldn't report an error beautifully?
458: // doing that ugly
459:
460: // make up result: $origin $source $comment $type $code
1.245.2.25 paf 461: char *buf=new(pool()) char[MAX_STRING];
1.30 paf 462: size_t printed=0;
1.245.2.6 paf 463: if(StringPtr problem_source=e.problem_source()) {
1.16 paf 464: #ifndef NO_STRING_ORIGIN
1.245.2.6 paf 465: const String_fragment::Origin& origin=problem_source->origin();
1.30 paf 466: if(origin.file)
1.180 paf 467: printed+=snprintf(buf+printed, MAX_STRING-printed,
468: ORIGIN_FILE_LINE_FORMAT": ",
469: origin.file, 1+origin.line
470: );
1.28 paf 471: #endif
1.41 paf 472: printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ",
1.245.2.14 paf 473: problem_source->cstr().get());
1.30 paf 474: }
1.245.2.5 paf 475: if(const char* comment=e.comment(true))
1.206 paf 476: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", comment);
1.245.2.5 paf 477: if(const char* type=e.type(true))
1.197 paf 478: printed+=snprintf(buf+printed, MAX_STRING-printed, " type: %s", type);
1.43 paf 479:
480: // future $response:content-type
1.245.2.6 paf 481: response->fields().put(content_type_name,
482: ValuePtr(new VString(StringPtr(new String(UNHANDLED_EXCEPTION_CONTENT_TYPE)))));
1.43 paf 483: // future $response:body
1.245.2.6 paf 484: body_string=StringPtr(new String(buf));
1.30 paf 485: }
1.41 paf 486:
1.245.2.6 paf 487: VString body_vstring(body_string);
1.245.2.13 paf 488: VFilePtr body_file=body_vstring.as_vfile(pool());
1.90 paf 489:
1.45 paf 490: // ERROR. write it out
1.245.2.7 paf 491: output_result(body_file, header_only, false);
1.170 parser 492: } catch(const Exception& ) {
1.245.2.6 paf 493: rethrow;
1.3 paf 494: }
1.1 paf 495: }
496: }
497:
1.245.2.3 paf 498: VStateless_classPtr Request::use_file(VStateless_class& aclass,
1.245.2.4 paf 499: StringPtr file_name, bool ignore_class_path,
1.233 paf 500: bool fail_on_read_problem, bool fail_on_file_absence) {
1.73 paf 501: // cyclic dependence check
1.148 parser 502: if(used_files.get(file_name))
1.245.2.14 paf 503: return VStateless_classPtr(0);
1.245.2.2 paf 504: used_files.put(file_name, true);
1.73 paf 505:
1.245.2.6 paf 506: StringPtr file_spec;
1.153 parser 507: if(ignore_class_path) // ignore_class_path?
1.245.2.6 paf 508: file_spec=file_name;
509: else if(file_name->first_char()=='/') //absolute path, no need to scan MAIN:CLASS_PATH?
510: file_spec=absolute(file_name);
1.153 parser 511: else {
1.245.2.6 paf 512: file_spec=StringPtr(0);
513: if(ValuePtr element=main_class->get_element(class_path_name, *main_class, false)) {
1.233 paf 514: if(element->is_string()) {
1.245.2.19 paf 515: file_spec=file_readable(*absolute(element->as_string(&pool())), *file_name); // found at class_path?
1.233 paf 516: } else if(Table *table=element->get_table()) {
1.245.2.6 paf 517: int size=table->count();
1.233 paf 518: for(int i=size; i--; ) {
1.245.2.6 paf 519: StringPtr path=(*table->get(i))[0];
1.245.2.19 paf 520: if(file_spec=file_readable(*absolute(path), *file_name))
1.233 paf 521: break; // found along class_path
522: }
523: } else
524: throw Exception("parser.runtime",
1.245.2.6 paf 525: Exception::undefined_source,
1.233 paf 526: "$" CLASS_PATH_NAME " must be string or table");
527: if(!file_spec)
528: throw Exception("parser.runtime",
1.245.2.6 paf 529: file_name,
1.233 paf 530: "not found along " MAIN_CLASS_NAME ":" CLASS_PATH_NAME);
531: }
1.153 parser 532: if(!file_spec)
1.197 paf 533: throw Exception("parser.runtime",
1.245.2.6 paf 534: file_name,
1.203 paf 535: "usage failed - no $" MAIN_CLASS_NAME ":" CLASS_PATH_NAME " were specified");
1.148 parser 536: }
1.230 paf 537:
538: if(fail_on_read_problem && !fail_on_file_absence) // ignore file absence if asked for
1.245.2.6 paf 539: if(!entry_exists(file_spec))
1.245.2.14 paf 540: return VStateless_classPtr(0);
1.73 paf 541:
1.245.2.6 paf 542: if(const char *source=file_read_text(pool(), charsets.source(), file_spec, fail_on_read_problem))
543: return use_buf(aclass, source, file_spec, file_spec->cstr(pool()));
544: else
1.245.2.14 paf 545: return VStateless_classPtr(0);
1.16 paf 546: }
547:
1.208 paf 548:
1.245.2.3 paf 549: VStateless_classPtr Request::use_buf(VStateless_class& aclass,
1.245.2.5 paf 550: const char* source,
1.245.2.6 paf 551: StringPtr filespec, const char* filespec_cstr) {
1.233 paf 552: // temporary zero @conf so to maybe-replace it in compiled code
1.245.2.6 paf 553: Temp_method temp_method_conf(aclass, conf_method_name, MethodPtr(0));
1.233 paf 554: // temporary zero @auto so to maybe-replace it in compiled code
1.245.2.6 paf 555: Temp_method temp_method_auto(aclass, auto_method_name, MethodPtr(0));
1.233 paf 556:
1.5 paf 557: // compile loaded class
1.245.2.6 paf 558: VStateless_class& cclass=COMPILE(&aclass, source, filespec_cstr);
1.212 paf 559:
1.233 paf 560: // locate and execute possible @conf[] static
1.245.2.6 paf 561: VStringPtr vfilespec(new VString(filespec));
1.245.2.26 paf 562: Execute_nonvirtual_method_result executed=execute_nonvirtual_method(cclass,
1.245.2.6 paf 563: conf_method_name, vfilespec,
1.245.2.26 paf 564: false/*no string result needed*/);
565: if(executed.method)
566: configure_admin(cclass, executed.method->name);
1.208 paf 567:
568: // locate and execute possible @auto[] static
1.233 paf 569: execute_nonvirtual_method(cclass,
1.245.2.6 paf 570: auto_method_name, vfilespec,
1.245.2.21 paf 571: false/*no result needed*/);
1.245.2.14 paf 572: return VStateless_classPtr(&cclass);
1.19 paf 573: }
574:
1.245.2.6 paf 575: StringPtr Request::relative(const char* apath, StringPtr relative_name) {
576: char *hpath=pool().copy(apath);
577: StringPtr result(new String);
1.217 paf 578: if(rsplit(hpath, '/')) // if something/splitted
1.245.2.6 paf 579: *result << hpath << "/";
580: *result << relative_name;
1.19 paf 581: return result;
582: }
583:
1.245.2.6 paf 584: StringPtr Request::absolute(StringPtr relative_name) {
585: if(relative_name->first_char()=='/') {
586: StringPtr result=StringPtr(new String(request_info.document_root));
587: *result << relative_name;
1.19 paf 588: return result;
589: } else
1.245.2.6 paf 590: return relative_name->pos("://")<0? relative(request_info.path_translated, relative_name)
1.239 paf 591: :relative_name; // something like "http://xxx"
1.1 paf 592: }
1.45 paf 593:
1.245.2.7 paf 594: static void add_header_attribute(
595: HashStringValue::key_type aattribute,
596: HashStringValue::value_type ameaning,
597: Request *r) {
598: if(*aattribute==BODY_NAME
599: || *aattribute==DOWNLOAD_NAME
600: || *aattribute==CHARSET_NAME)
1.101 paf 601: return;
602:
1.245.2.7 paf 603: SAPI::add_header_attribute(r->sapi_info,
604: aattribute->cstr(),
605: attributed_meaning_to_string(r->pool(), ameaning, String::UL_HTTP_HEADER, false)->
606: cstr(String::UL_UNSPECIFIED));
1.101 paf 607: }
1.245.2.7 paf 608: void Request::output_result(VFilePtr body_file, bool header_only, bool as_attachment) {
1.51 paf 609: // header: cookies
1.245.2.10 paf 610: cookie->output_result(pool(), sapi_info);
1.51 paf 611:
1.245.2.6 paf 612: ValuePtr body_file_content_type;
1.90 paf 613: // set content-type
1.245.2.7 paf 614: if(body_file_content_type=body_file->fields().get(content_type_name)) {
1.90 paf 615: // body file content type
1.245.2.7 paf 616: response->fields().put(content_type_name, body_file_content_type);
1.90 paf 617: } else {
618: // default content type
1.245.2.7 paf 619: response->fields().put_dont_replace(content_type_name,
620: ValuePtr(new VString(StringPtr(new String(DEFAULT_CONTENT_TYPE)))));
1.92 paf 621: }
622:
623: // content-disposition
1.245.2.7 paf 624: if(ValuePtr vfile_name=body_file->fields().get(name_name))
1.245.2.14 paf 625: if(*vfile_name->get_string(&fpool)!=NONAME_DAT) {
1.245.2.7 paf 626: VHashPtr hash(new VHash);
627: HashStringValue &h=hash->hash(Exception::undefined_source);
1.244 paf 628: if(as_attachment)
1.245.2.7 paf 629: h.put(value_name, ValuePtr(new VString(content_disposition_value)));
630: h.put(content_disposition_filename_name, vfile_name);
631: response->fields().put(content_disposition_name, hash);
1.111 paf 632: }
1.49 paf 633:
1.62 paf 634: // prepare header: $response:fields without :body
1.245.2.6 paf 635: response->fields().for_each(add_header_attribute, this);
1.50 paf 636:
1.184 paf 637: const void *client_body;
638: size_t client_content_length;
1.220 paf 639: // transcode text body when "text/*" or simple result
1.223 paf 640: if(body_file_content_type)
1.245.2.7 paf 641: if(HashStringValue *hash=body_file_content_type->get_hash(Exception::undefined_source))
642: body_file_content_type=hash->get(value_name);
643: if(!body_file_content_type/*vstring.as_vfile*/ || body_file_content_type->as_string(&pool())->pos("text/")==0) {
1.220 paf 644: Charset::transcode(pool(),
1.245.2.7 paf 645: charsets.source(), body_file->value_ptr(), body_file->value_size(),
646: charsets.client(), client_body, client_content_length
1.220 paf 647: );
648: } else {
1.245.2.7 paf 649: client_body=body_file->value_ptr();
650: client_content_length=body_file->value_size();
1.220 paf 651: }
1.50 paf 652:
1.62 paf 653: // prepare header: content-length
1.200 paf 654: char content_length_cstr[MAX_NUMBER];
655: snprintf(content_length_cstr, MAX_NUMBER, "%u", client_content_length);
1.245.2.7 paf 656: SAPI::add_header_attribute(sapi_info, "content-length", content_length_cstr);
1.62 paf 657:
658: // send header
1.245.2.7 paf 659: SAPI::send_header(sapi_info);
1.71 paf 660:
1.62 paf 661: // send body
662: if(!header_only)
1.245.2.7 paf 663: SAPI::send_body(sapi_info, client_body, client_content_length);
1.50 paf 664: }
1.104 paf 665:
1.245.2.7 paf 666: StringPtr Request::mime_type_of(const char* user_file_name_cstr) {
1.104 paf 667: if(mime_types)
1.245.2.5 paf 668: if(const char* cext=strrchr(user_file_name_cstr, '.')) {
1.245.2.7 paf 669: String sext(++cext);
670: if(mime_types->locate(0, sext.change_case(pool(), charsets.source(), String::CC_LOWER)))
671: if(StringPtr result=mime_types->item(1))
672: return result;
1.104 paf 673: else
1.197 paf 674: throw Exception("parser.runtime",
1.104 paf 675: mime_types->origin_string(),
1.212 paf 676: MIME_TYPES_NAME " table column elements must not be empty");
1.104 paf 677: }
1.245.2.7 paf 678:
679: return StringPtr(new String("application/octet-stream"));
1.233 paf 680: }
681:
682: bool Request::origins_mode() {
1.245.2.7 paf 683: return main_class->get_element(origins_mode_name, *main_class, false)!=0; // $ORIGINS mode
1.133 parser 684: }
1.245.2.30 paf 685:
1.245.2.33 paf 686: #ifdef XML
687:
1.245.2.30 paf 688: GdomeDOMString_auto_ptr Request::transcode(StringPtr s) {
689: return charsets.source().transcode(s);
690: }
691:
692: StringPtr Request::transcode(GdomeDOMString* s
693: #ifndef NO_STRING_ORIGIN
694: , StringPtr origin
695: #endif
696: ) {
1.245.2.31 paf 697: return charsets.source().transcode(pool(), s
1.245.2.30 paf 698: #ifndef NO_STRING_ORIGIN
699: , origin
700: #endif
701: );
702: }
703:
1.245.2.31 paf 704: StringPtr Request::transcode(xmlChar* s
705: #ifndef NO_STRING_ORIGIN
706: , StringPtr origin
707: #endif
708: ) {
709: return charsets.source().transcode(pool(), s
710: #ifndef NO_STRING_ORIGIN
711: , origin
712: #endif
713: );
714: }
1.245.2.34 paf 715: #endif
E-mail: