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