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