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