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