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