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