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