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