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