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