Annotation of parser3/src/main/pa_request.C, revision 1.98
1.54 paf 1: /** @file
1.55 paf 2: Parser: request class main part. @see compile.C and execute.C.
3:
1.9 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.55 paf 5:
1.14 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.9 paf 7:
1.98 ! paf 8: $Id: pa_request.C,v 1.97 2001/04/05 18:23:00 paf Exp $
1.1 paf 9: */
10:
1.70 paf 11: #include "pa_config_includes.h"
1.19 paf 12:
1.96 paf 13: #include <locale.h>
14:
1.69 paf 15: #include "pa_sapi.h"
1.53 paf 16: #include "pa_common.h"
1.1 paf 17: #include "pa_request.h"
1.3 paf 18: #include "pa_wwrapper.h"
19: #include "pa_vclass.h"
1.11 paf 20: #include "_root.h"
1.17 paf 21: #include "_table.h"
1.82 paf 22: #include "_file.h"
1.31 paf 23: #include "pa_globals.h"
1.30 paf 24: #include "pa_vint.h"
25: #include "pa_vmframe.h"
1.32 paf 26: #include "pa_types.h"
1.78 paf 27: #include "pa_vtable.h"
1.89 paf 28: #include "_random.h"
1.90 paf 29: #include "pa_vfile.h"
1.3 paf 30:
1.72 paf 31: /// $limits.post_max_size default 10M
32: const size_t MAX_POST_SIZE_DEFAULT=10*0x400*400;
33:
1.82 paf 34: /// content type of exception response, when no @MAIN:exception handler defined
35: const char *UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain";
36:
37: /// content type of response when no $MAIN:defaults.content-type defined
38: const char *DEFAULT_CONTENT_TYPE="text/html";
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.41 paf 45: ROOT(apool),
46: env(apool),
47: form(apool),
48: request(apool, *this),
49: response(apool),
1.51 paf 50: cookie(apool),
1.3 paf 51: fclasses(apool),
1.43 paf 52: fdefault_lang(adefault_lang), flang(adefault_lang),
53: info(ainfo),
1.85 paf 54: used_files(apool),
1.74 paf 55: default_content_type(0),
1.94 paf 56: mime_types(0),
1.95 paf 57: connection(0), protocol2library(0)
1.3 paf 58: {
1.27 paf 59: // root superclass,
1.3 paf 60: // parent of all classes,
61: // operators holder
1.41 paf 62: initialize_root_class(pool(), ROOT);
63: classes().put(*root_class_name, &ROOT);
1.27 paf 64: // table class
65: classes().put(*table_class_name, table_class);
1.82 paf 66: // file class
67: classes().put(*file_class_name, file_class);
1.89 paf 68: // random class
69: classes().put(*random_class_name, random_class);
1.3 paf 70: // env class
1.41 paf 71: classes().put(*env_class_name, &env);
1.27 paf 72: // form class
1.41 paf 73: classes().put(*form_class_name, &form);
1.40 paf 74: // request class
1.41 paf 75: classes().put(*request_class_name, &request);
76: // response class
77: classes().put(*response_class_name, &response);
1.51 paf 78: // cookie class
79: classes().put(*cookie_class_name, &cookie);
1.3 paf 80: }
1.1 paf 81:
1.62 paf 82: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning,
83: void *info) {
1.48 paf 84: String *attribute_to_exclude=static_cast<String *>(info);
85: if(aattribute==*attribute_to_exclude)
1.45 paf 86: return;
87:
1.62 paf 88: Value& lmeaning=*static_cast<Value *>(ameaning);
89: Pool& pool=lmeaning.pool();
1.45 paf 90:
1.62 paf 91: String attribute(pool);
1.69 paf 92: SAPI::add_header_attribute(pool,
1.62 paf 93: attribute.append(aattribute, String::UL_HEADER, true).cstr(),
94: attributed_meaning_to_string(lmeaning).cstr());
1.45 paf 95: }
96:
1.64 paf 97: /**
98: load MAIN class, execute @main.
99: MAIN class consists of all the auto.p files we'd manage to find
100: plus
101: the file user requested us to process
102: all located classes become children of one another,
103: composing class we name 'MAIN'
1.96 paf 104:
105: @todo figure out 'setlocale' thread safety
1.64 paf 106: */
1.63 paf 107: void Request::core(const char *root_auto_path, bool root_auto_fail,
108: const char *site_auto_path, bool site_auto_fail,
1.62 paf 109: bool header_only) {
1.29 paf 110: VStateless_class *main_class=0;
1.41 paf 111: bool need_rethrow=false; Exception rethrow_me;
1.3 paf 112: TRY {
1.29 paf 113: char *auto_filespec=(char *)malloc(MAX_STRING);
114:
1.63 paf 115: // loading root auto.p
116: if(root_auto_path) {
1.77 paf 117: String& filespec=*NEW String(pool());
1.88 paf 118: filespec.APPEND_CLEAN(root_auto_path, 0, "root_auto", 0);
1.77 paf 119: filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.29 paf 120: main_class=use_file(
1.77 paf 121: filespec, root_auto_fail,
1.37 paf 122: main_class_name, main_class);
1.29 paf 123: }
124:
1.85 paf 125: // $MAIN:LIMITS hash used here,
1.72 paf 126: // until someone with less privileges have overriden them
1.78 paf 127: {
128: Value *limits=main_class?main_class->get_element(*limits_name):0;
129: // $limits.post_max_size default 10M
130: Value *element=limits?limits->get_element(*post_max_size_name):0;
1.84 paf 131: size_t value=element?(size_t)element->as_double():0;
1.78 paf 132: size_t post_max_size=value?value:MAX_POST_SIZE_DEFAULT;
133:
134: form.fill_fields(*this, post_max_size);
135: }
1.72 paf 136:
1.78 paf 137: // filling cookies
1.72 paf 138: cookie.fill_fields(*this);
139:
1.64 paf 140: // loading site auto.p
1.63 paf 141: if(site_auto_path) {
1.77 paf 142: String& filespec=*NEW String(pool());
1.88 paf 143: filespec.APPEND_CLEAN(site_auto_path, 0, "site_auto", 0);
1.77 paf 144: filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.37 paf 145: main_class=use_file(
1.77 paf 146: filespec, site_auto_fail,
1.37 paf 147: main_class_name, main_class);
1.29 paf 148: }
1.8 paf 149:
1.72 paf 150: // loading auto.p files from document_root/..
151: // to the one beside requested file.
152: // all assigned bases from upper dir
153: {
154: /* /document/root */
1.77 paf 155: char *file_spec=
1.72 paf 156: (char *)malloc(strlen(info.path_translated)+strlen(AUTO_FILE_NAME)+1);
1.83 paf 157: size_t document_root_size=strlen(info.document_root);
158: if(info.document_root[document_root_size-1]=='/')
159: document_root_size--;
160: memcpy(file_spec, info.document_root, document_root_size);
1.72 paf 161:
162: /* /requested/file.html */
163: char *branches=(char *)malloc(strlen(info.path_translated)+1);
1.83 paf 164: strcpy(branches, info.path_translated+document_root_size);
1.72 paf 165: char *next=branches;
1.83 paf 166: char *append_here=file_spec+document_root_size;
1.72 paf 167:
168: size_t slash_auto_p_size=strlen("/" AUTO_FILE_NAME);
169: while(true) {
170: char *step=lsplit(&next, '/');
171: if(next) { // not 'file.html' part
172: size_t step_size=strlen(step);
173: memcpy(append_here, step, step_size);
174: append_here+=step_size;
175: memcpy(append_here, "/" AUTO_FILE_NAME, slash_auto_p_size+1);
176: append_here++/* / */;
177:
1.77 paf 178: String& sfile_spec=*NEW String(pool());
1.88 paf 179: sfile_spec.APPEND_CLEAN(file_spec, 0, "scanned", 0);
1.77 paf 180: main_class=use_file(sfile_spec, false/*ignore read problem*/,
1.72 paf 181: main_class_name, main_class);
182: } else
183: break;
184: }
185: }
1.34 paf 186:
1.72 paf 187: // compiling requested file
1.77 paf 188: String& spath_translated=*NEW String(pool());
1.88 paf 189: spath_translated.APPEND_CLEAN(info.path_translated, 0, "user-request", 0);
1.77 paf 190: main_class=use_file(spath_translated, true/*don't ignore read problem*/,
1.72 paf 191: main_class_name, main_class);
1.7 paf 192:
1.85 paf 193: // $MAIN:DEFAULTS
1.78 paf 194: Value *defaults=main_class->get_element(*defaults_name);
1.75 paf 195: // value must be allocated on request's pool for that pool used on
196: // meaning constructing @see attributed_meaning_to_string
1.80 paf 197: default_content_type=defaults?defaults->get_element(*content_type_name):0;
1.78 paf 198: if(Value *element=main_class->get_element(*html_typo_name))
1.87 paf 199: if(Table *table=element->get_table())
200: pool().set_tag(table);
1.95 paf 201:
202: // $MAIN:SQL.drivers
203: if(Value *sql=main_class->get_element(*main_sql_name))
204: if(Value *element=sql->get_element(*main_sql_drivers_name))
205: protocol2library=element->get_table();
1.85 paf 206:
207: // $MAIN:MIME-TYPES
208: if(Value *element=main_class->get_element(*mime_types_name))
209: if(Table *table=element->get_table())
210: mime_types=table;
1.96 paf 211:
212: // $MAIN:LOCALE.ctype[Russian_Russia.1251]
213: /*
214: #define LC_ALL 0
215: #define LC_COLLATE 1
216: #define LC_CTYPE 2
217: #define LC_MONETARY 3
218: #define LC_NUMERIC 4
219: #define LC_TIME 5
220: */
221: if(Value *locale=main_class->get_element(*locale_name))
222: if(Value *element=locale->get_element(*locale_ctype_name)) {
223: const String& name=element->as_string();
224: if(!setlocale(LC_CTYPE, name.cstr()))
225: THROW(0, 0,
226: &name,
1.97 paf 227: "locale is invalid");
1.96 paf 228: }
1.25 paf 229:
230: // execute @main[]
1.41 paf 231: const String *body_string=execute_method(*main_class, *main_method_name);
232: if(!body_string)
1.25 paf 233: THROW(0,0,
234: 0,
235: "'"MAIN_METHOD_NAME"' method not found");
1.79 paf 236:
1.91 paf 237: VString body_vstring_before_post_process(*body_string);
238: VString *body_vstring_after_post_process=&body_vstring_before_post_process;
239:
240: // post-process
241: if(Value *value=main_class->get_element(*post_process_method_name))
242: if(Junction *junction=value->get_junction())
243: if(const Method *method=junction->method) {
244: // preparing to pass parameters to
245: // @post-process[data]
1.98 ! paf 246: VMethodFrame frame(pool(), *junction, false);
1.91 paf 247: frame.set_self(*main_class);
248:
249: frame.store_param(method->name,
250: &body_vstring_before_post_process);
251: body_vstring_after_post_process=
252: NEW VString(*execute_method(frame, *method));
253: }
1.90 paf 254:
1.91 paf 255: const VFile *body_file=body_vstring_after_post_process->as_vfile();
1.41 paf 256:
1.45 paf 257: // extract response body
258: Value *body_value=static_cast<Value *>(
259: response.fields().get(*body_name));
260: if(body_value) // there is some $request.body
1.90 paf 261: body_file=body_value->as_vfile();
1.45 paf 262:
263: // OK. write out the result
1.90 paf 264: output_result(*body_file, header_only);
1.3 paf 265: }
1.76 paf 266: CATCH(e) { // request handling problem
267: // we're returning not result, but error explanation
1.30 paf 268: TRY {
1.76 paf 269: // log the beast
270: const String *problem_source=e.problem_source();
271: if(problem_source)
272: SAPI::log(pool(),
273: #ifndef NO_STRING_ORIGIN
274: "%s(%d): "
275: #endif
276: "'%s' %s [%s %s]",
277: #ifndef NO_STRING_ORIGIN
278: problem_source->origin().file?problem_source->origin().file:"global",
279: problem_source->origin().line,
280: #endif
281: problem_source->cstr(),
282: e.comment(),
283: e.type()?e.type()->cstr():"-",
284: e.code()?e.code()->cstr():"-"
285: );
286: else
287: SAPI::log(pool(),
288: "%s [%s %s]",
289: e.comment(),
290: e.type()?e.type()->cstr():"-",
291: e.code()?e.code()->cstr():"-"
292: );
1.43 paf 293:
294: // reset language to default
295: flang=fdefault_lang;
296:
297: // reset response
298: response.fields().clear();
299:
300: // this is what we'd return in $response:body
1.41 paf 301: const String *body_string=0;
1.43 paf 302:
1.30 paf 303: if(main_class) { // we've managed to end up with some main_class
304: // maybe we'd be lucky enough as to report an error
305: // in a gracefull way...
306: if(Value *value=main_class->get_element(*exception_method_name))
307: if(Junction *junction=value->get_junction())
308: if(const Method *method=junction->method) {
309: // preparing to pass parameters to
310: // @exception[origin;source;comment;type;code]
1.98 ! paf 311: VMethodFrame frame(pool(), *junction, false);
1.38 paf 312: frame.set_self(*main_class);
1.30 paf 313:
314: const String *problem_source=e.problem_source();
315: // origin
1.38 paf 316: Value *origin_value=0;
1.30 paf 317: #ifndef NO_STRING_ORIGIN
1.38 paf 318: if(problem_source) {
319: const Origin& origin=problem_source->origin();
320: if(origin.file) {
321: char *buf=(char *)malloc(MAX_STRING);
1.42 paf 322: snprintf(buf, MAX_STRING, "%s(%d):",
1.38 paf 323: origin.file, 1+origin.line);
1.44 paf 324: String *origin_file_line=NEW String(pool(),
325: buf, true);
1.38 paf 326: origin_value=NEW VString(*origin_file_line);
327: }
1.30 paf 328: }
329: #endif
1.91 paf 330: frame.store_param(method->name,
1.38 paf 331: origin_value?origin_value:NEW VUnknown(pool()));
1.28 paf 332:
1.30 paf 333: // source
1.38 paf 334: Value *source_value=0;
1.43 paf 335: if(problem_source) {
1.47 paf 336: String& problem_source_copy=*NEW String(pool());
337: problem_source_copy.append(*problem_source,
338: flang, true);
1.43 paf 339: source_value=NEW VString(problem_source_copy);
340: }
1.91 paf 341: frame.store_param(method->name,
1.38 paf 342: source_value?source_value:NEW VUnknown(pool()));
1.30 paf 343:
344: // comment
1.44 paf 345: String *comment_value=NEW String(pool(),
346: e.comment(), true);
1.91 paf 347: frame.store_param(method->name,
1.30 paf 348: NEW VString(*comment_value));
349:
350: // type
351: Value *type_value;
1.43 paf 352: if(e.type()) {
1.47 paf 353: String& type_copy=*NEW String(pool());
354: type_value=NEW VString(type_copy.append(*e.type(),
355: flang, true));
1.43 paf 356: } else
1.30 paf 357: type_value=NEW VUnknown(pool());
1.91 paf 358: frame.store_param(method->name, type_value);
1.30 paf 359:
360: // code
361: Value *code_value;
1.43 paf 362: if(e.code()) {
1.47 paf 363: String& code_copy=*NEW String(pool());
364: code_value=NEW VString(code_copy.append(*e.code(),
365: flang, true));
1.43 paf 366: } else
1.30 paf 367: code_value=NEW VUnknown(pool());
1.91 paf 368: frame.store_param(method->name, code_value);
1.30 paf 369:
1.43 paf 370: // future $response:body=
371: // execute ^exception[origin;source;comment;type;code]
372: body_string=execute_method(frame, *method);
1.30 paf 373: }
374: }
375:
1.41 paf 376: if(!body_string) { // couldn't report an error beautifully?
377: // doing that ugly
378:
379: // make up result: $origin $source $comment $type $code
380: char *buf=(char *)malloc(MAX_STRING);
1.30 paf 381: size_t printed=0;
382: const String *problem_source=e.problem_source();
383: if(problem_source) {
1.16 paf 384: #ifndef NO_STRING_ORIGIN
1.30 paf 385: const Origin& origin=problem_source->origin();
386: if(origin.file)
1.41 paf 387: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ",
1.30 paf 388: origin.file, 1+origin.line);
1.28 paf 389: #endif
1.41 paf 390: printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ",
1.30 paf 391: problem_source->cstr());
392: }
1.41 paf 393: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s",
1.30 paf 394: e.comment());
395: const String *type=e.type();
396: if(type) {
1.41 paf 397: printed+=snprintf(buf+printed, MAX_STRING-printed, " type: %s",
1.30 paf 398: type->cstr());
399: const String *code=e.code();
400: if(code)
1.41 paf 401: printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s",
1.30 paf 402: code->cstr());
403: }
1.43 paf 404:
405: // future $response:content-type
1.49 paf 406: response.fields().put(*content_type_name,
1.82 paf 407: NEW VString(*NEW String(pool(), UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.43 paf 408: // future $response:body
1.44 paf 409: body_string=NEW String(pool(), buf);
1.30 paf 410: }
1.41 paf 411:
1.90 paf 412: const VString body_vstring(*body_string);
413: const VFile *body_file=body_vstring.as_vfile();
414:
1.45 paf 415: // ERROR. write it out
1.90 paf 416: output_result(*body_file, header_only);
1.3 paf 417: }
1.30 paf 418: CATCH(e) {
1.41 paf 419: // exception in request exception handler
420: // remember to rethrow it
421: rethrow_me=e; need_rethrow=true;
1.1 paf 422: }
1.30 paf 423: END_CATCH
1.1 paf 424: }
1.41 paf 425: END_CATCH // do not use pool() after this point - no exception handler set
426: // any throw() would try to use zero exception() pointer
427:
1.91 paf 428: if(need_rethrow) // were there an exception for us to rethrow?
1.62 paf 429: THROW(rethrow_me.type(), rethrow_me.code(),
1.41 paf 430: rethrow_me.problem_source(),
431: rethrow_me.comment());
1.1 paf 432: }
433:
1.77 paf 434: VStateless_class *Request::use_file(const String& file_spec, bool fail_on_read_problem,
1.26 paf 435: const String *name,
436: VStateless_class *base_class) {
1.73 paf 437: // cyclic dependence check
1.77 paf 438: if(used_files.get(file_spec))
1.73 paf 439: return base_class;
1.77 paf 440: used_files.put(file_spec, (Hash::Val *)true);
1.73 paf 441:
442:
1.77 paf 443: char *source=file_read_text(pool(), file_spec, fail_on_read_problem);
1.3 paf 444: if(!source)
1.12 paf 445: return base_class;
1.3 paf 446:
1.77 paf 447: return use_buf(source, file_spec.cstr(), 0/*new class*/, name, base_class);
1.16 paf 448: }
449:
1.67 paf 450: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26 paf 451: VStateless_class *aclass, const String *name,
452: VStateless_class *base_class) {
1.5 paf 453: // compile loaded class
1.26 paf 454: VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1 paf 455:
1.4 paf 456: // locate and execute possible @auto[] static method
1.25 paf 457: execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16 paf 458: return &cclass;
1.20 paf 459: }
460:
1.52 paf 461: /**
462: - fail_if_junction(true, junction = fail
463: - fail_if_junction(false, not junction = fail
464: */
1.62 paf 465: void Request::fail_if_junction_(bool is, Value& value,
466: const String& method_name, const char *msg) {
1.20 paf 467:
468: if((value.get_junction()!=0) ^ !is)
469: THROW(0, 0,
470: &method_name,
471: msg);
1.19 paf 472: }
473:
1.77 paf 474: const String& Request::relative(const char *apath, const String& relative_name) {
475: int lpath_buf_size=strlen(apath)+1;
476: char *lpath=(char *)malloc(lpath_buf_size);
477: memcpy(lpath, apath, lpath_buf_size);
478: rsplit(lpath, '/');
479: String& result=*NEW String(pool(), lpath);
480: result.APPEND_CONST("/");
481: result.append(relative_name, String::UL_PASS_APPENDED);
1.19 paf 482: return result;
483: }
484:
1.77 paf 485: const String& Request::absolute(const String& relative_name) {
486: char *relative_name_cstr=relative_name.cstr();
487: if(relative_name_cstr[0]=='/') {
488: String& result=*NEW String(pool(), info.document_root);
489: result.append(relative_name, String::UL_PASS_APPENDED);
1.19 paf 490: return result;
491: } else
1.77 paf 492: return relative(info.path_translated, relative_name);
1.1 paf 493: }
1.45 paf 494:
1.90 paf 495: void Request::output_result(const VFile& body_file, bool header_only) {
1.51 paf 496: // header: cookies
497: cookie.output_result();
498:
1.90 paf 499: // set content-type
500: if(String *body_file_content_type=static_cast<String *>(
501: body_file.fields().get(*vfile_mime_type_name))) {
502: // body file content type
503: response.fields().put(*content_type_name, body_file_content_type);
504: } else {
505: // default content type
506: response.fields().put_dont_replace(*content_type_name,
507: default_content_type?default_content_type
508: :NEW VString(*NEW String(pool(), DEFAULT_CONTENT_TYPE)));
1.92 paf 509: }
510:
511: // content-disposition
512: if(VString *vfile_name=static_cast<VString *>(body_file.fields().get(*name_name))) {
513: VHash& vhash=*NEW VHash(pool());
514: vhash.hash().put(*content_disposition_filename_name, vfile_name);
515: response.fields().put(*content_disposition_name, &vhash);
1.90 paf 516: }
1.49 paf 517:
1.62 paf 518: // prepare header: $response:fields without :body
1.73 paf 519: response.fields().for_each(add_header_attribute, /*excluding*/ body_name);
1.50 paf 520:
1.62 paf 521: // prepare...
1.90 paf 522: const void *body=body_file.value_ptr();
523: size_t content_length=body_file.value_size();
1.50 paf 524:
1.62 paf 525: // prepare header: content-length
1.65 paf 526: if(content_length) { // useful for redirecting [header "location: http://..."]
527: char content_length_cstr[MAX_NUMBER];
1.66 paf 528: snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.69 paf 529: SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.65 paf 530: }
1.62 paf 531:
532: // send header
1.69 paf 533: SAPI::send_header(pool());
1.71 paf 534:
1.62 paf 535: // send body
536: if(!header_only)
1.69 paf 537: SAPI::send_body(pool(), body, content_length);
1.50 paf 538: }
E-mail: