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