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