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