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