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