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