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