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