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