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