Annotation of parser3/src/main/pa_request.C, revision 1.79
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.79 ! paf 8: $Id: pa_request.C,v 1.78 2001/03/24 19:12:20 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.74 paf 176: default_content_type=defaults?
177: defaults->get_element(*content_type_name)
1.76 paf 178: :NEW VString(*NEW String(pool(), "text/html"));
1.79 ! paf 179: Table *typo_table=0;
1.78 paf 180: if(Value *element=main_class->get_element(*html_typo_name))
181: if(VTable *vtable=element->get_vtable())
1.79 ! paf 182: typo_table=&vtable->table();
! 183: pool().set_tag(typo_table?typo_table:default_typo_table);
1.25 paf 184:
185: // execute @main[]
1.41 paf 186: const String *body_string=execute_method(*main_class, *main_method_name);
187: if(!body_string)
1.25 paf 188: THROW(0,0,
189: 0,
190: "'"MAIN_METHOD_NAME"' method not found");
1.79 ! paf 191:
! 192: // post-process
! 193: // todo
1.41 paf 194:
1.45 paf 195: // extract response body
196: Value *body_value=static_cast<Value *>(
197: response.fields().get(*body_name));
198: if(body_value) // there is some $request.body
199: body_string=&body_value->as_string();// TODO: IMAGE&FILE
200:
201: // OK. write out the result
1.62 paf 202: output_result(*body_string, header_only);
1.3 paf 203: }
1.76 paf 204: CATCH(e) { // request handling problem
205: // we're returning not result, but error explanation
1.30 paf 206: TRY {
1.76 paf 207: // log the beast
208: const String *problem_source=e.problem_source();
209: if(problem_source)
210: SAPI::log(pool(),
211: #ifndef NO_STRING_ORIGIN
212: "%s(%d): "
213: #endif
214: "'%s' %s [%s %s]",
215: #ifndef NO_STRING_ORIGIN
216: problem_source->origin().file?problem_source->origin().file:"global",
217: problem_source->origin().line,
218: #endif
219: problem_source->cstr(),
220: e.comment(),
221: e.type()?e.type()->cstr():"-",
222: e.code()?e.code()->cstr():"-"
223: );
224: else
225: SAPI::log(pool(),
226: "%s [%s %s]",
227: e.comment(),
228: e.type()?e.type()->cstr():"-",
229: e.code()?e.code()->cstr():"-"
230: );
1.43 paf 231:
232: // reset language to default
233: flang=fdefault_lang;
234:
235: // reset response
236: response.fields().clear();
237:
238: // this is what we'd return in $response:body
1.41 paf 239: const String *body_string=0;
1.43 paf 240:
1.30 paf 241: if(main_class) { // we've managed to end up with some main_class
242: // maybe we'd be lucky enough as to report an error
243: // in a gracefull way...
244: if(Value *value=main_class->get_element(*exception_method_name))
245: if(Junction *junction=value->get_junction())
246: if(const Method *method=junction->method) {
247: // preparing to pass parameters to
248: // @exception[origin;source;comment;type;code]
1.38 paf 249: VMethodFrame frame(pool(), *junction);
250: frame.set_self(*main_class);
1.30 paf 251:
252: const String *problem_source=e.problem_source();
253: // origin
1.44 paf 254: String origin_name(pool(), "origin");
1.38 paf 255: Value *origin_value=0;
1.30 paf 256: #ifndef NO_STRING_ORIGIN
1.38 paf 257: if(problem_source) {
258: const Origin& origin=problem_source->origin();
259: if(origin.file) {
260: char *buf=(char *)malloc(MAX_STRING);
1.42 paf 261: snprintf(buf, MAX_STRING, "%s(%d):",
1.38 paf 262: origin.file, 1+origin.line);
1.44 paf 263: String *origin_file_line=NEW String(pool(),
264: buf, true);
1.38 paf 265: origin_value=NEW VString(*origin_file_line);
266: }
1.30 paf 267: }
268: #endif
1.38 paf 269: frame.store_param(origin_name,
270: origin_value?origin_value:NEW VUnknown(pool()));
1.28 paf 271:
1.30 paf 272: // source
1.44 paf 273: String source_name(pool(), "source");
1.38 paf 274: Value *source_value=0;
1.43 paf 275: if(problem_source) {
1.47 paf 276: String& problem_source_copy=*NEW String(pool());
277: problem_source_copy.append(*problem_source,
278: flang, true);
1.43 paf 279: source_value=NEW VString(problem_source_copy);
280: }
1.38 paf 281: frame.store_param(source_name,
282: source_value?source_value:NEW VUnknown(pool()));
1.30 paf 283:
284: // comment
1.44 paf 285: String comment_name(pool(), "comment");
286: String *comment_value=NEW String(pool(),
287: e.comment(), true);
1.38 paf 288: frame.store_param(comment_name,
1.30 paf 289: NEW VString(*comment_value));
290:
291: // type
1.44 paf 292: String type_name(pool(), "type");
1.30 paf 293: Value *type_value;
1.43 paf 294: if(e.type()) {
1.47 paf 295: String& type_copy=*NEW String(pool());
296: type_value=NEW VString(type_copy.append(*e.type(),
297: flang, true));
1.43 paf 298: } else
1.30 paf 299: type_value=NEW VUnknown(pool());
1.38 paf 300: frame.store_param(type_name, type_value);
1.30 paf 301:
302: // code
1.44 paf 303: String code_name(pool(), "code");
1.30 paf 304: Value *code_value;
1.43 paf 305: if(e.code()) {
1.47 paf 306: String& code_copy=*NEW String(pool());
307: code_value=NEW VString(code_copy.append(*e.code(),
308: flang, true));
1.43 paf 309: } else
1.30 paf 310: code_value=NEW VUnknown(pool());
1.38 paf 311: frame.store_param(code_name, code_value);
1.30 paf 312:
1.43 paf 313: // future $response:body=
314: // execute ^exception[origin;source;comment;type;code]
315: body_string=execute_method(frame, *method);
1.30 paf 316: }
317: }
318:
1.41 paf 319: if(!body_string) { // couldn't report an error beautifully?
320: // doing that ugly
321:
322: // make up result: $origin $source $comment $type $code
323: char *buf=(char *)malloc(MAX_STRING);
1.30 paf 324: size_t printed=0;
325: const String *problem_source=e.problem_source();
326: if(problem_source) {
1.16 paf 327: #ifndef NO_STRING_ORIGIN
1.30 paf 328: const Origin& origin=problem_source->origin();
329: if(origin.file)
1.41 paf 330: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ",
1.30 paf 331: origin.file, 1+origin.line);
1.28 paf 332: #endif
1.41 paf 333: printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ",
1.30 paf 334: problem_source->cstr());
335: }
1.41 paf 336: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s",
1.30 paf 337: e.comment());
338: const String *type=e.type();
339: if(type) {
1.41 paf 340: printed+=snprintf(buf+printed, MAX_STRING-printed, " type: %s",
1.30 paf 341: type->cstr());
342: const String *code=e.code();
343: if(code)
1.41 paf 344: printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s",
1.30 paf 345: code->cstr());
346: }
1.43 paf 347:
348: // future $response:content-type
1.49 paf 349: response.fields().put(*content_type_name,
350: NEW VString(*NEW String(pool(), "text/plain")));
1.43 paf 351: // future $response:body
1.44 paf 352: body_string=NEW String(pool(), buf);
1.30 paf 353: }
1.41 paf 354:
1.45 paf 355: // ERROR. write it out
1.62 paf 356: output_result(*body_string, header_only);
1.3 paf 357: }
1.30 paf 358: CATCH(e) {
1.41 paf 359: // exception in request exception handler
360: // remember to rethrow it
361: rethrow_me=e; need_rethrow=true;
1.1 paf 362: }
1.30 paf 363: END_CATCH
1.1 paf 364: }
1.41 paf 365: END_CATCH // do not use pool() after this point - no exception handler set
366: // any throw() would try to use zero exception() pointer
367:
368: if(need_rethrow) // there were an exception set for us to rethrow?
1.62 paf 369: THROW(rethrow_me.type(), rethrow_me.code(),
1.41 paf 370: rethrow_me.problem_source(),
371: rethrow_me.comment());
1.28 paf 372:
1.1 paf 373: }
374:
1.77 paf 375: VStateless_class *Request::use_file(const String& file_spec, bool fail_on_read_problem,
1.26 paf 376: const String *name,
377: VStateless_class *base_class) {
1.73 paf 378: // cyclic dependence check
1.77 paf 379: if(used_files.get(file_spec))
1.73 paf 380: return base_class;
1.77 paf 381: used_files.put(file_spec, (Hash::Val *)true);
1.73 paf 382:
383:
1.77 paf 384: char *source=file_read_text(pool(), file_spec, fail_on_read_problem);
1.3 paf 385: if(!source)
1.12 paf 386: return base_class;
1.3 paf 387:
1.77 paf 388: return use_buf(source, file_spec.cstr(), 0/*new class*/, name, base_class);
1.16 paf 389: }
390:
1.67 paf 391: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26 paf 392: VStateless_class *aclass, const String *name,
393: VStateless_class *base_class) {
1.5 paf 394: // compile loaded class
1.26 paf 395: VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1 paf 396:
1.4 paf 397: // locate and execute possible @auto[] static method
1.25 paf 398: execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16 paf 399: return &cclass;
1.20 paf 400: }
401:
1.52 paf 402: /**
403: - fail_if_junction(true, junction = fail
404: - fail_if_junction(false, not junction = fail
405: */
1.62 paf 406: void Request::fail_if_junction_(bool is, Value& value,
407: const String& method_name, const char *msg) {
1.20 paf 408:
409: if((value.get_junction()!=0) ^ !is)
410: THROW(0, 0,
411: &method_name,
412: msg);
1.19 paf 413: }
414:
1.77 paf 415: const String& Request::relative(const char *apath, const String& relative_name) {
416: int lpath_buf_size=strlen(apath)+1;
417: char *lpath=(char *)malloc(lpath_buf_size);
418: memcpy(lpath, apath, lpath_buf_size);
419: rsplit(lpath, '/');
420: String& result=*NEW String(pool(), lpath);
421: result.APPEND_CONST("/");
422: result.append(relative_name, String::UL_PASS_APPENDED);
1.19 paf 423: return result;
424: }
425:
1.77 paf 426: const String& Request::absolute(const String& relative_name) {
427: char *relative_name_cstr=relative_name.cstr();
428: if(relative_name_cstr[0]=='/') {
429: String& result=*NEW String(pool(), info.document_root);
430: result.append(relative_name, String::UL_PASS_APPENDED);
1.19 paf 431: return result;
432: } else
1.77 paf 433: return relative(info.path_translated, relative_name);
1.1 paf 434: }
1.45 paf 435:
1.62 paf 436: void Request::output_result(const String& body_string, bool header_only) {
1.51 paf 437: // header: cookies
438: cookie.output_result();
439:
1.49 paf 440: // set default content-type
1.74 paf 441: response.fields().put_dont_replace(*content_type_name, default_content_type);
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: