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