Annotation of parser3/src/main/pa_request.C, revision 1.147
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.55 paf 5:
1.14 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.1 paf 7: */
1.147 ! parser 8: static const char *RCSId="$Id: pa_request.C,v 1.146 2001/07/26 10:47:02 parser Exp $";
1.1 paf 9:
1.70 paf 10: #include "pa_config_includes.h"
1.19 paf 11:
1.117 paf 12: #include "pcre.h"
13: #include "internal.h"
1.132 parser 14: extern "C" unsigned char pcre_default_tables[]; // pcre/chartables.c
1.96 paf 15:
1.69 paf 16: #include "pa_sapi.h"
1.53 paf 17: #include "pa_common.h"
1.1 paf 18: #include "pa_request.h"
1.3 paf 19: #include "pa_wwrapper.h"
20: #include "pa_vclass.h"
1.31 paf 21: #include "pa_globals.h"
1.30 paf 22: #include "pa_vint.h"
1.112 paf 23: #include "pa_vmethod_frame.h"
1.32 paf 24: #include "pa_types.h"
1.78 paf 25: #include "pa_vtable.h"
1.90 paf 26: #include "pa_vfile.h"
1.147 ! parser 27: #include "pa_dictionary.h"
1.3 paf 28:
1.82 paf 29: /// content type of exception response, when no @MAIN:exception handler defined
30: const char *UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain";
31:
32: /// content type of response when no $MAIN:defaults.content-type defined
33: const char *DEFAULT_CONTENT_TYPE="text/html";
1.144 parser 34: const char *ORIGINS_CONTENT_TYPE="text/plain";
1.82 paf 35:
1.123 paf 36: Methoded *MOP_create(Pool&);
37:
1.72 paf 38: //
1.28 paf 39: Request::Request(Pool& apool,
1.33 paf 40: Info& ainfo,
1.43 paf 41: String::Untaint_lang adefault_lang) : Pooled(apool),
1.3 paf 42: stack(apool),
1.123 paf 43: OP(*MOP_create(apool)),
1.41 paf 44: env(apool),
45: form(apool),
1.141 parser 46: math(apool),
1.41 paf 47: request(apool, *this),
48: response(apool),
1.51 paf 49: cookie(apool),
1.3 paf 50: fclasses(apool),
1.43 paf 51: fdefault_lang(adefault_lang), flang(adefault_lang),
52: info(ainfo),
1.105 paf 53: post_data(0), post_size(0),
1.85 paf 54: used_files(apool),
1.74 paf 55: default_content_type(0),
1.94 paf 56: mime_types(0),
1.124 paf 57: main_class(0),
1.126 paf 58: connection(0),
59: pcre_tables(0),
1.131 parser 60: classes_conf(apool),
61: anti_endless_execute_recoursion(0)
1.3 paf 62: {
1.123 paf 63: /// directly used
64: // operators
65: OP.register_directly_used(*this);
66: // classes:
1.128 paf 67: // table, file, random, mail, image, ...
1.123 paf 68: methoded_array->register_directly_used(*this);
69:
70: /// methodless
1.3 paf 71: // env class
1.123 paf 72: classes().put(*NEW String(pool(), ENV_CLASS_NAME), &env);
1.40 paf 73: // request class
1.123 paf 74: classes().put(*NEW String(pool(), REQUEST_CLASS_NAME), &request);
75: // cookie class
76: classes().put(*NEW String(pool(), COOKIE_CLASS_NAME), &cookie);
77:
78: /// methoded
1.41 paf 79: // response class
1.123 paf 80: classes().put(response.get_class()->name(), &response);
81:
82: /// bases used
83: // form class
84: classes().put(form.get_class()->base()->name(), &form);
1.141 parser 85: // math class
86: classes().put(math.get_class()->base()->name(), &math);
1.45 paf 87: }
88:
1.117 paf 89: static void element2ctypes(unsigned char *tables,
90: Value& ctype, const String& name,
91: unsigned char bit,
92: int group_offset=-1,
93: bool skip_ws=true) {
94: Value *value=ctype.get_element(name);
95: if(!value)
96: return;
97:
98: unsigned char *ctypes_table=tables+ctypes_offset;
99: const unsigned char *cstr=
100: (const unsigned char *)value->as_string().cstr(String::UL_AS_IS);
101: for(; *cstr; cstr++) {
102: unsigned char c=*cstr;
103: if(skip_ws && (c=='\n' || c=='\t' || c==' '))
104: continue;
105: ctypes_table[c]|=bit;
106:
107: if(group_offset>=0)
1.118 paf 108: tables[cbits_offset+group_offset+c/8] |= 1 << (c%8);
1.117 paf 109: }
110: }
111: static void cstr2ctypes(unsigned char *tables, const unsigned char *cstr,
112: unsigned char bit) {
113: unsigned char *ctypes_table=tables+ctypes_offset;
114: ctypes_table[0]=bit;
115: for(; *cstr; cstr++) {
116: unsigned char c=*cstr;
117: ctypes_table[c]|=bit;
118: }
119: }
120: static void prepare_case_tables(unsigned char *tables) {
121: unsigned char *lcc_table=tables+lcc_offset;
122: unsigned char *fcc_table=tables+fcc_offset;
123: for(int i=0; i<0x100; i++)
124: lcc_table[i]=fcc_table[i]=i;
125: }
126: static void element2case(unsigned char *tables, Value& ctype, const String& name) {
127: Value *value=ctype.get_element(name);
128: if(!value)
129: return;
130:
131: unsigned char *lcc_table=tables+lcc_offset;
132: unsigned char *fcc_table=tables+fcc_offset;
133: const unsigned char *cstr=
134: (const unsigned char *)value->as_string().cstr(String::UL_AS_IS);
135: unsigned char from=0;
136: for(; *cstr; cstr++) {
137: unsigned char c=*cstr;
138: if(c=='\n' || c=='\t' || c==' ')
139: continue;
140: if(from) {
141: lcc_table[from]=c;
142: fcc_table[from]=c; fcc_table[c]=from;
143: from=0;
144: } else
145: from=c;
146: }
147: }
1.64 paf 148: /**
149: load MAIN class, execute @main.
150: MAIN class consists of all the auto.p files we'd manage to find
151: plus
152: the file user requested us to process
153: all located classes become children of one another,
154: composing class we name 'MAIN'
155: */
1.63 paf 156: void Request::core(const char *root_auto_path, bool root_auto_fail,
157: const char *site_auto_path, bool site_auto_fail,
1.62 paf 158: bool header_only) {
1.121 paf 159: //_asm { int 3 }
1.41 paf 160: bool need_rethrow=false; Exception rethrow_me;
1.3 paf 161: TRY {
1.29 paf 162: char *auto_filespec=(char *)malloc(MAX_STRING);
163:
1.63 paf 164: // loading root auto.p
165: if(root_auto_path) {
1.77 paf 166: String& filespec=*NEW String(pool());
1.88 paf 167: filespec.APPEND_CLEAN(root_auto_path, 0, "root_auto", 0);
1.77 paf 168: filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.29 paf 169: main_class=use_file(
1.77 paf 170: filespec, root_auto_fail,
1.37 paf 171: main_class_name, main_class);
1.29 paf 172: }
173:
1.124 paf 174: // configure root options
1.72 paf 175: // until someone with less privileges have overriden them
1.129 paf 176: OP.configure_admin(*this);
1.124 paf 177: methoded_array->configure_admin(*this);
1.72 paf 178:
1.64 paf 179: // loading site auto.p
1.63 paf 180: if(site_auto_path) {
1.77 paf 181: String& filespec=*NEW String(pool());
1.88 paf 182: filespec.APPEND_CLEAN(site_auto_path, 0, "site_auto", 0);
1.77 paf 183: filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.37 paf 184: main_class=use_file(
1.77 paf 185: filespec, site_auto_fail,
1.37 paf 186: main_class_name, main_class);
1.29 paf 187: }
1.8 paf 188:
1.72 paf 189: // loading auto.p files from document_root/..
190: // to the one beside requested file.
191: // all assigned bases from upper dir
192: {
1.115 paf 193: Array ladder(pool());
194: const char *after=info.path_translated;
195: size_t drlen=strlen(info.document_root);
196: if(memcmp(after, info.document_root, drlen)==0) {
197: after+=drlen;
198: if(after[-1]=='/')
199: --after;
200: }
201:
202: while(const char *before=strchr(after, '/')) {
203: String& step=*NEW String(pool());
204: if(after!=info.path_translated) {
205: step.APPEND_CLEAN(
206: info.path_translated, before+1/* / */-info.path_translated,
207: "path-translated-scanned", ladder.size());
208: step << AUTO_FILE_NAME;
209: ladder+=&step;
210: }
211: after=before+1;
212: }
1.142 parser 213: for(int i=0; i<ladder.size(); i++) {
1.115 paf 214: const String& sfile_spec=*ladder.get_string(i);
215: main_class=use_file(sfile_spec, false/*ignore read problem*/,
216: main_class_name, main_class);
1.72 paf 217: }
218: }
1.34 paf 219:
1.125 paf 220: // compile requested file
1.77 paf 221: String& spath_translated=*NEW String(pool());
1.136 parser 222: spath_translated.APPEND_TAINTED(info.path_translated, 0, "user-request", 0);
1.77 paf 223: main_class=use_file(spath_translated, true/*don't ignore read problem*/,
1.72 paf 224: main_class_name, main_class);
1.7 paf 225:
1.124 paf 226: // configure not-root=user options
1.129 paf 227: OP.configure_user(*this);
1.124 paf 228: methoded_array->configure_user(*this);
229:
1.85 paf 230: // $MAIN:DEFAULTS
1.78 paf 231: Value *defaults=main_class->get_element(*defaults_name);
1.75 paf 232: // value must be allocated on request's pool for that pool used on
233: // meaning constructing @see attributed_meaning_to_string
1.80 paf 234: default_content_type=defaults?defaults->get_element(*content_type_name):0;
1.117 paf 235: if(Value *element=main_class->get_element(*user_html_name))
1.87 paf 236: if(Table *table=element->get_table())
1.147 ! parser 237: pool().set_tag(NEW Dictionary(*table));
1.85 paf 238:
239: // $MAIN:MIME-TYPES
240: if(Value *element=main_class->get_element(*mime_types_name))
241: if(Table *table=element->get_table())
242: mime_types=table;
1.96 paf 243:
1.117 paf 244: /*
245: $MAIN:CTYPE[
1.147 ! parser 246: $.white-space[
1.117 paf 247: ^#09^#0A^#0B^#0C^#0D^#20]
1.147 ! parser 248: $.digit[
1.117 paf 249: 0123456789]
1.147 ! parser 250: $.hex-digit[
1.117 paf 251: 0123456789ABCDEFabcdef]
1.147 ! parser 252: $.letter[
1.117 paf 253: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]
1.147 ! parser 254: $.word[
1.117 paf 255: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz]
256:
1.147 ! parser 257: $.lowercase[
1.117 paf 258: AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz]
259: ]
260: */
261: if(Value *ctype=main_class->get_element(*ctype_name)) {
262: // lowcase, flipcase, bits digit+word+whitespace, masks
263: pcre_tables=(unsigned char *)calloc(tables_length);
264: prepare_case_tables(pcre_tables);
265:
266: element2ctypes(pcre_tables, *ctype, *ctype_white_space_name, ctype_space, cbit_space, false);
267: element2ctypes(pcre_tables, *ctype, *ctype_digit_name, ctype_digit, cbit_digit);
268: element2ctypes(pcre_tables, *ctype, *ctype_hex_digit_name, ctype_xdigit);
269: element2ctypes(pcre_tables, *ctype, *ctype_letter_name, ctype_letter);
270: element2ctypes(pcre_tables, *ctype, *ctype_word_name, ctype_word, cbit_word);
271: cstr2ctypes(pcre_tables, (const unsigned char *)"*+?{^.$|()[", ctype_meta);
272:
273: element2case(pcre_tables, *ctype, *ctype_lowercase_name);
1.132 parser 274: } else
275: pcre_tables=pcre_default_tables;
1.102 paf 276:
1.124 paf 277: // filling form fields
278: form.fill_fields(*this);
1.102 paf 279:
1.124 paf 280: // filling cookies
281: cookie.fill_fields(*this);
1.25 paf 282:
283: // execute @main[]
1.143 parser 284: const String *body_string=execute_virtual_method(
285: *main_class, *main_method_name);
1.41 paf 286: if(!body_string)
1.25 paf 287: THROW(0,0,
1.145 parser 288: 0,
289: "'"MAIN_METHOD_NAME"' method not found");
1.79 paf 290:
1.91 paf 291: VString body_vstring_before_post_process(*body_string);
292: VString *body_vstring_after_post_process=&body_vstring_before_post_process;
293:
1.119 paf 294: // @postprocess
1.91 paf 295: if(Value *value=main_class->get_element(*post_process_method_name))
296: if(Junction *junction=value->get_junction())
297: if(const Method *method=junction->method) {
298: // preparing to pass parameters to
1.119 paf 299: // @postprocess[data]
1.146 parser 300: VMethodFrame frame(pool(), value->name(), *junction);
1.91 paf 301: frame.set_self(*main_class);
302:
303: frame.store_param(method->name,
304: &body_vstring_before_post_process);
305: body_vstring_after_post_process=
306: NEW VString(*execute_method(frame, *method));
307: }
1.90 paf 308:
1.144 parser 309: bool origins_mode=main_class->get_element(*origins_mode_name)!=0;
310:
311: const VFile *body_file=body_vstring_after_post_process->as_vfile(
312: String::UL_UNSPECIFIED, origins_mode);
1.41 paf 313:
1.45 paf 314: // extract response body
315: Value *body_value=static_cast<Value *>(
316: response.fields().get(*body_name));
1.119 paf 317: if(body_value) // there is some $response.body
1.90 paf 318: body_file=body_value->as_vfile();
1.144 parser 319: else if(origins_mode)
320: response.fields().put(*content_type_name,
321: NEW VString(*NEW String(pool(), ORIGINS_CONTENT_TYPE)));
1.45 paf 322:
323: // OK. write out the result
1.90 paf 324: output_result(*body_file, header_only);
1.3 paf 325: }
1.76 paf 326: CATCH(e) { // request handling problem
327: // we're returning not result, but error explanation
1.30 paf 328: TRY {
1.76 paf 329: // log the beast
330: const String *problem_source=e.problem_source();
1.114 paf 331: if(problem_source && problem_source->size())
1.76 paf 332: SAPI::log(pool(),
333: #ifndef NO_STRING_ORIGIN
334: "%s(%d): "
335: #endif
336: "'%s' %s [%s %s]",
337: #ifndef NO_STRING_ORIGIN
338: problem_source->origin().file?problem_source->origin().file:"global",
339: problem_source->origin().line,
340: #endif
1.99 paf 341: problem_source->cstr(String::UL_AS_IS),
1.76 paf 342: e.comment(),
1.99 paf 343: e.type()?e.type()->cstr(String::UL_AS_IS):"-",
344: e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76 paf 345: );
346: else
347: SAPI::log(pool(),
348: "%s [%s %s]",
349: e.comment(),
1.99 paf 350: e.type()?e.type()->cstr(String::UL_AS_IS):"-",
351: e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76 paf 352: );
1.43 paf 353:
354: // reset language to default
355: flang=fdefault_lang;
1.135 parser 356: if(flang==String::UL_USER_HTML)
1.137 parser 357: flang=String::UL_HTML; // no _ & Co conversions in @exception[params]
1.43 paf 358:
359: // reset response
360: response.fields().clear();
361:
362: // this is what we'd return in $response:body
1.41 paf 363: const String *body_string=0;
1.43 paf 364:
1.30 paf 365: if(main_class) { // we've managed to end up with some main_class
366: // maybe we'd be lucky enough as to report an error
367: // in a gracefull way...
368: if(Value *value=main_class->get_element(*exception_method_name))
369: if(Junction *junction=value->get_junction())
370: if(const Method *method=junction->method) {
371: // preparing to pass parameters to
372: // @exception[origin;source;comment;type;code]
1.146 parser 373: VMethodFrame frame(pool(), value->name(), *junction);
1.38 paf 374: frame.set_self(*main_class);
1.30 paf 375:
376: const String *problem_source=e.problem_source();
377: // origin
1.38 paf 378: Value *origin_value=0;
1.30 paf 379: #ifndef NO_STRING_ORIGIN
1.114 paf 380: if(problem_source && problem_source->size()) {
1.38 paf 381: const Origin& origin=problem_source->origin();
382: if(origin.file) {
383: char *buf=(char *)malloc(MAX_STRING);
1.106 paf 384: size_t buf_size=snprintf(buf, MAX_STRING, "%s(%d):",
1.38 paf 385: origin.file, 1+origin.line);
1.44 paf 386: String *origin_file_line=NEW String(pool(),
1.106 paf 387: buf, buf_size, true);
1.38 paf 388: origin_value=NEW VString(*origin_file_line);
389: }
1.30 paf 390: }
391: #endif
1.91 paf 392: frame.store_param(method->name,
1.138 parser 393: origin_value?origin_value:NEW VVoid(pool()));
1.28 paf 394:
1.30 paf 395: // source
1.38 paf 396: Value *source_value=0;
1.114 paf 397: if(problem_source && problem_source->size()) {
1.47 paf 398: String& problem_source_copy=*NEW String(pool());
399: problem_source_copy.append(*problem_source,
400: flang, true);
1.43 paf 401: source_value=NEW VString(problem_source_copy);
402: }
1.91 paf 403: frame.store_param(method->name,
1.138 parser 404: source_value?source_value:NEW VVoid(pool()));
1.30 paf 405:
406: // comment
1.44 paf 407: String *comment_value=NEW String(pool(),
1.106 paf 408: e.comment(), 0, true);
1.91 paf 409: frame.store_param(method->name,
1.30 paf 410: NEW VString(*comment_value));
411:
412: // type
413: Value *type_value;
1.43 paf 414: if(e.type()) {
1.47 paf 415: String& type_copy=*NEW String(pool());
416: type_value=NEW VString(type_copy.append(*e.type(),
417: flang, true));
1.43 paf 418: } else
1.138 parser 419: type_value=NEW VVoid(pool());
1.91 paf 420: frame.store_param(method->name, type_value);
1.30 paf 421:
422: // code
423: Value *code_value;
1.43 paf 424: if(e.code()) {
1.47 paf 425: String& code_copy=*NEW String(pool());
426: code_value=NEW VString(code_copy.append(*e.code(),
427: flang, true));
1.43 paf 428: } else
1.138 parser 429: code_value=NEW VVoid(pool());
1.91 paf 430: frame.store_param(method->name, code_value);
1.30 paf 431:
1.43 paf 432: // future $response:body=
433: // execute ^exception[origin;source;comment;type;code]
434: body_string=execute_method(frame, *method);
1.30 paf 435: }
436: }
437:
1.41 paf 438: if(!body_string) { // couldn't report an error beautifully?
439: // doing that ugly
440:
441: // make up result: $origin $source $comment $type $code
442: char *buf=(char *)malloc(MAX_STRING);
1.30 paf 443: size_t printed=0;
444: const String *problem_source=e.problem_source();
445: if(problem_source) {
1.16 paf 446: #ifndef NO_STRING_ORIGIN
1.30 paf 447: const Origin& origin=problem_source->origin();
448: if(origin.file)
1.41 paf 449: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ",
1.30 paf 450: origin.file, 1+origin.line);
1.28 paf 451: #endif
1.41 paf 452: printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ",
1.99 paf 453: problem_source->cstr(String::UL_AS_IS));
1.30 paf 454: }
1.41 paf 455: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s",
1.30 paf 456: e.comment());
457: const String *type=e.type();
458: if(type) {
1.41 paf 459: printed+=snprintf(buf+printed, MAX_STRING-printed, " type: %s",
1.99 paf 460: type->cstr(String::UL_AS_IS));
1.30 paf 461: const String *code=e.code();
462: if(code)
1.41 paf 463: printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s",
1.99 paf 464: code->cstr(String::UL_AS_IS));
1.30 paf 465: }
1.43 paf 466:
467: // future $response:content-type
1.49 paf 468: response.fields().put(*content_type_name,
1.82 paf 469: NEW VString(*NEW String(pool(), UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.43 paf 470: // future $response:body
1.44 paf 471: body_string=NEW String(pool(), buf);
1.30 paf 472: }
1.41 paf 473:
1.144 parser 474: VString body_vstring(*body_string);
1.90 paf 475: const VFile *body_file=body_vstring.as_vfile();
476:
1.45 paf 477: // ERROR. write it out
1.90 paf 478: output_result(*body_file, header_only);
1.3 paf 479: }
1.30 paf 480: CATCH(e) {
1.41 paf 481: // exception in request exception handler
482: // remember to rethrow it
483: rethrow_me=e; need_rethrow=true;
1.1 paf 484: }
1.30 paf 485: END_CATCH
1.1 paf 486: }
1.41 paf 487: END_CATCH // do not use pool() after this point - no exception handler set
488: // any throw() would try to use zero exception() pointer
489:
1.91 paf 490: if(need_rethrow) // were there an exception for us to rethrow?
1.62 paf 491: THROW(rethrow_me.type(), rethrow_me.code(),
1.41 paf 492: rethrow_me.problem_source(),
493: rethrow_me.comment());
1.1 paf 494: }
495:
1.77 paf 496: VStateless_class *Request::use_file(const String& file_spec, bool fail_on_read_problem,
1.26 paf 497: const String *name,
498: VStateless_class *base_class) {
1.73 paf 499: // cyclic dependence check
1.77 paf 500: if(used_files.get(file_spec))
1.73 paf 501: return base_class;
1.77 paf 502: used_files.put(file_spec, (Hash::Val *)true);
1.73 paf 503:
504:
1.77 paf 505: char *source=file_read_text(pool(), file_spec, fail_on_read_problem);
1.3 paf 506: if(!source)
1.12 paf 507: return base_class;
1.3 paf 508:
1.77 paf 509: return use_buf(source, file_spec.cstr(), 0/*new class*/, name, base_class);
1.16 paf 510: }
511:
1.67 paf 512: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26 paf 513: VStateless_class *aclass, const String *name,
514: VStateless_class *base_class) {
1.5 paf 515: // compile loaded class
1.26 paf 516: VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1 paf 517:
1.4 paf 518: // locate and execute possible @auto[] static method
1.143 parser 519: execute_nonvirtual_method(cclass, *auto_method_name, false /*no result needed*/);
1.16 paf 520: return &cclass;
1.19 paf 521: }
522:
1.77 paf 523: const String& Request::relative(const char *apath, const String& relative_name) {
524: int lpath_buf_size=strlen(apath)+1;
525: char *lpath=(char *)malloc(lpath_buf_size);
526: memcpy(lpath, apath, lpath_buf_size);
1.120 paf 527: if(!rsplit(lpath, '/'))
528: strcpy(lpath, ".");
1.77 paf 529: String& result=*NEW String(pool(), lpath);
1.104 paf 530: result << "/" << relative_name;
1.19 paf 531: return result;
532: }
533:
1.77 paf 534: const String& Request::absolute(const String& relative_name) {
535: char *relative_name_cstr=relative_name.cstr();
536: if(relative_name_cstr[0]=='/') {
537: String& result=*NEW String(pool(), info.document_root);
1.104 paf 538: result << relative_name;
1.19 paf 539: return result;
540: } else
1.77 paf 541: return relative(info.path_translated, relative_name);
1.1 paf 542: }
1.45 paf 543:
1.101 paf 544: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning,
545: void *info) {
546: String *attribute_to_exclude=static_cast<String *>(info);
547: if(aattribute==*attribute_to_exclude)
548: return;
549:
550: Value& lmeaning=*static_cast<Value *>(ameaning);
551: Pool& pool=lmeaning.pool();
552:
553: SAPI::add_header_attribute(pool,
1.116 paf 554: aattribute.cstr(String::UL_AS_IS),
1.101 paf 555: attributed_meaning_to_string(lmeaning, String::UL_HTTP_HEADER).cstr());
556: }
1.90 paf 557: void Request::output_result(const VFile& body_file, bool header_only) {
1.51 paf 558: // header: cookies
559: cookie.output_result();
560:
1.90 paf 561: // set content-type
562: if(String *body_file_content_type=static_cast<String *>(
563: body_file.fields().get(*vfile_mime_type_name))) {
564: // body file content type
565: response.fields().put(*content_type_name, body_file_content_type);
566: } else {
567: // default content type
568: response.fields().put_dont_replace(*content_type_name,
569: default_content_type?default_content_type
570: :NEW VString(*NEW String(pool(), DEFAULT_CONTENT_TYPE)));
1.92 paf 571: }
572:
573: // content-disposition
1.111 paf 574: if(VString *vfile_name=static_cast<VString *>(body_file.fields().get(*name_name)))
575: if(vfile_name->string()!=NONAME_DAT) {
576: VHash& vhash=*NEW VHash(pool());
577: vhash.hash().put(*content_disposition_filename_name, vfile_name);
578: response.fields().put(*content_disposition_name, &vhash);
579: }
1.49 paf 580:
1.62 paf 581: // prepare header: $response:fields without :body
1.73 paf 582: response.fields().for_each(add_header_attribute, /*excluding*/ body_name);
1.50 paf 583:
1.62 paf 584: // prepare...
1.90 paf 585: const void *body=body_file.value_ptr();
586: size_t content_length=body_file.value_size();
1.50 paf 587:
1.62 paf 588: // prepare header: content-length
1.65 paf 589: if(content_length) { // useful for redirecting [header "location: http://..."]
590: char content_length_cstr[MAX_NUMBER];
1.108 paf 591: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.69 paf 592: SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.65 paf 593: }
1.62 paf 594:
595: // send header
1.69 paf 596: SAPI::send_header(pool());
1.71 paf 597:
1.62 paf 598: // send body
599: if(!header_only)
1.69 paf 600: SAPI::send_body(pool(), body, content_length);
1.50 paf 601: }
1.104 paf 602:
603: const String& Request::mime_type_of(const char *user_file_name_cstr) {
604: if(mime_types)
605: if(const char *cext=strrchr(user_file_name_cstr, '.')) {
606: String sext(pool(), ++cext);
607: if(mime_types->locate(0, sext))
608: if(const String *result=mime_types->item(1))
609: return *result;
610: else
611: THROW(0, 0,
612: mime_types->origin_string(),
613: "MIME-TYPE table column elements must not be empty");
614: }
615: return *NEW String(pool(), "application/octet-stream");
1.133 parser 616: }
E-mail: