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