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