|
|
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.41 ! paf 6: $Id: pa_request.C,v 1.40 2001/03/16 12:30:24 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.30 paf 23: #define NEW_STRING(name, value) name=NEW String(pool()); name->APPEND_CONST(value)
24: #define LOCAL_STRING(name, value) String name(pool()); name.APPEND_CONST(value)
1.3 paf 25:
1.28 paf 26: Request::Request(Pool& apool,
1.33 paf 27: Info& ainfo,
28: String::Untaint_lang alang) : Pooled(apool),
1.3 paf 29: stack(apool),
1.41 ! paf 30: ROOT(apool),
! 31: env(apool),
! 32: form(apool),
! 33: request(apool, *this),
! 34: response(apool),
1.3 paf 35: fclasses(apool),
1.28 paf 36: flang(alang),
1.33 paf 37: info(ainfo)
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);
46: // table_class->set_name(*table_class_name);
1.3 paf 47:
48: // env class
1.41 ! paf 49: classes().put(*env_class_name, &env);
1.27 paf 50: // form class
1.41 ! paf 51: classes().put(*form_class_name, &form);
1.40 paf 52: // request class
1.41 ! paf 53: classes().put(*request_class_name, &request);
! 54: // response class
! 55: classes().put(*response_class_name, &response);
1.3 paf 56: }
1.1 paf 57:
1.41 ! paf 58: void Request::core(Exception& system_exception,
! 59: const char *sys_auto_path1,
! 60: const char *sys_auto_path2) {
1.29 paf 61: VStateless_class *main_class=0;
1.41 ! paf 62: bool need_rethrow=false; Exception rethrow_me;
1.3 paf 63: TRY {
1.29 paf 64: char *auto_filespec=(char *)malloc(MAX_STRING);
65:
66: // load MAIN class,
67: // it consists of all the auto.p files we'd manage to find
68: // plus
69: // the file user requested us to process
70: // all located classes become children of one another,
71: // composing class we name 'MAIN'
72:
73: // loading system auto.p 1
74: if(sys_auto_path1) {
1.30 paf 75: strncpy(auto_filespec, sys_auto_path1, MAX_STRING-strlen(AUTO_FILE_NAME));
1.29 paf 76: strcat(auto_filespec, AUTO_FILE_NAME);
77: main_class=use_file(
78: auto_filespec, false/*ignore possible read problem*/,
1.37 paf 79: main_class_name, main_class);
1.29 paf 80: }
81:
82: // loading system auto.p 2
83: if(sys_auto_path2) {
1.30 paf 84: strncpy(auto_filespec, sys_auto_path2, MAX_STRING-strlen(AUTO_FILE_NAME));
1.29 paf 85: strcat(auto_filespec, AUTO_FILE_NAME);
1.37 paf 86: main_class=use_file(
1.29 paf 87: auto_filespec, false/*ignore possible read problem*/,
1.37 paf 88: main_class_name, main_class);
1.29 paf 89: }
1.8 paf 90:
1.34 paf 91: // $MAIN:limits hash used here,
92: // until someone with less privileges have overriden them
93: Value *limits=main_class?main_class->get_element(*limits_name):0;
94: Value *element;
1.35 paf 95: // $limits.post_max_size default 10M
1.34 paf 96: element=limits?limits->get_element(*post_max_size_name):0;
1.36 paf 97: int value=element?(size_t)element->get_double():0;
98: int post_max_size=value?value:10*0x400*400;
1.34 paf 99:
1.41 ! paf 100: //response.fields().put(*content_type_name, TODO);
! 101:
! 102: form.fill_fields(*this, post_max_size);
1.7 paf 103:
104: // TODO: load site auto.p files, all assigned bases from upper dir
1.39 paf 105: /*char *site_auto_file="Y:\\parser3\\src\\auto.p";
1.16 paf 106: main_class=use_file(
1.39 paf 107: site_auto_file, false/*ignore possible read problem* /,
108: main_class_name, main_class);*/
1.7 paf 109:
110: // there must be some auto.p
1.12 paf 111: if(!main_class)
1.7 paf 112: THROW(0,0,
113: 0,
1.24 paf 114: "no 'auto.p' found (nither system nor any site's)");
1.5 paf 115:
1.7 paf 116: // compiling requested file
1.35 paf 117: main_class=use_file(info.path_translated, true/*don't ignore read problem*/,
1.25 paf 118: main_class_name, main_class);
119:
120: // execute @main[]
1.41 ! paf 121: const String *body_string=execute_method(*main_class, *main_method_name);
! 122: if(!body_string)
1.25 paf 123: THROW(0,0,
124: 0,
125: "'"MAIN_METHOD_NAME"' method not found");
1.41 ! paf 126:
! 127: // store 'body' unless response already have one
! 128: response.fields().put_dont_replace(*body_name, NEW VString(*body_string));
1.3 paf 129: }
130: CATCH(e) {
1.30 paf 131: TRY {
132: // we're returning not result, but error explanation
1.41 ! paf 133: const String *body_string=0;
1.30 paf 134:
135: if(main_class) { // we've managed to end up with some main_class
136: // maybe we'd be lucky enough as to report an error
137: // in a gracefull way...
138: if(Value *value=main_class->get_element(*exception_method_name))
139: if(Junction *junction=value->get_junction())
140: if(const Method *method=junction->method) {
141: // preparing to pass parameters to
142: // @exception[origin;source;comment;type;code]
1.38 paf 143: VMethodFrame frame(pool(), *junction);
144: frame.set_self(*main_class);
1.30 paf 145:
146: const String *problem_source=e.problem_source();
147: // origin
148: LOCAL_STRING(origin_name, "origin");
1.38 paf 149: Value *origin_value=0;
1.30 paf 150: #ifndef NO_STRING_ORIGIN
1.38 paf 151: if(problem_source) {
152: const Origin& origin=problem_source->origin();
153: if(origin.file) {
154: char *buf=(char *)malloc(MAX_STRING);
155: snprintf(buf, MAX_STRING, "%s(%d): ",
156: origin.file, 1+origin.line);
157: String *NEW_STRING(origin_file_line, buf);
158: origin_value=NEW VString(*origin_file_line);
159: }
1.30 paf 160: }
161: #endif
1.38 paf 162: frame.store_param(origin_name,
163: origin_value?origin_value:NEW VUnknown(pool()));
1.28 paf 164:
1.30 paf 165: // source
166: LOCAL_STRING(source_name, "source");
1.38 paf 167: Value *source_value=0;
168: if(problem_source)
169: source_value=NEW VString(*problem_source);
170: frame.store_param(source_name,
171: source_value?source_value:NEW VUnknown(pool()));
1.30 paf 172:
173: // comment
174: LOCAL_STRING(comment_name, "comment");
175: String *NEW_STRING(comment_value, e.comment());
1.38 paf 176: frame.store_param(comment_name,
1.30 paf 177: NEW VString(*comment_value));
178:
179: // type
180: LOCAL_STRING(type_name, "type");
181: Value *type_value;
182: if(e.type())
183: type_value=NEW VString(*e.type());
184: else
185: type_value=NEW VUnknown(pool());
1.38 paf 186: frame.store_param(type_name, type_value);
1.30 paf 187:
188: // code
189: LOCAL_STRING(code_name, "code");
190: Value *code_value;
191: if(e.code())
192: code_value=NEW VString(*e.code());
193: else
194: code_value=NEW VUnknown(pool());
1.38 paf 195: frame.store_param(code_name, code_value);
1.30 paf 196:
1.41 ! paf 197: {
! 198: Temp_lang temp_lang(*this,
! 199: String::Untaint_lang::HTML_TYPO);
! 200: body_string=execute_method(frame, *method);
! 201: }
1.30 paf 202: }
203: }
204:
1.41 ! paf 205: if(!body_string) { // couldn't report an error beautifully?
! 206: // doing that ugly
! 207:
! 208: // assign content-type
! 209: String &content_type_value=*NEW String(pool());
! 210: content_type_value.APPEND_CONST("text/plain");
! 211: response.fields().put(*content_type_name,
! 212: NEW VString(content_type_value));
! 213:
! 214: // make up result: $origin $source $comment $type $code
! 215: char *buf=(char *)malloc(MAX_STRING);
1.30 paf 216: size_t printed=0;
217: const String *problem_source=e.problem_source();
218: if(problem_source) {
1.16 paf 219: #ifndef NO_STRING_ORIGIN
1.30 paf 220: const Origin& origin=problem_source->origin();
221: if(origin.file)
1.41 ! paf 222: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ",
1.30 paf 223: origin.file, 1+origin.line);
1.28 paf 224: #endif
1.41 ! paf 225: printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ",
1.30 paf 226: problem_source->cstr());
227: }
1.41 ! paf 228: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s",
1.30 paf 229: e.comment());
230: const String *type=e.type();
231: if(type) {
1.41 ! paf 232: printed+=snprintf(buf+printed, MAX_STRING-printed, " type: %s",
1.30 paf 233: type->cstr());
234: const String *code=e.code();
235: if(code)
1.41 ! paf 236: printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s",
1.30 paf 237: code->cstr());
238: }
1.41 ! paf 239: String *error_string=NEW String(pool()); error_string->APPEND_CONST(buf);
! 240: body_string=error_string;
1.30 paf 241: }
1.41 ! paf 242:
! 243: // store 'body' overwriting any response already had
! 244: response.fields().put(*body_name, NEW VString(*body_string));
1.3 paf 245: }
1.30 paf 246: CATCH(e) {
1.41 ! paf 247: // exception in request exception handler
! 248: // remember to rethrow it
! 249: rethrow_me=e; need_rethrow=true;
1.1 paf 250: }
1.30 paf 251: END_CATCH
1.1 paf 252: }
1.41 ! paf 253: END_CATCH // do not use pool() after this point - no exception handler set
! 254: // any throw() would try to use zero exception() pointer
! 255:
! 256: if(need_rethrow) // there were an exception set for us to rethrow?
! 257: system_exception._throw(rethrow_me.type(), rethrow_me.code(),
! 258: rethrow_me.problem_source(),
! 259: rethrow_me.comment());
1.28 paf 260:
1.1 paf 261: }
262:
1.26 paf 263: VStateless_class *Request::use_file(
264: const char *file, bool fail_on_read_problem,
265: const String *name,
266: VStateless_class *base_class) {
1.3 paf 267: // TODO: обнаружить|решить cyclic dependences
1.5 paf 268: char *source=file_read(pool(), file, fail_on_read_problem);
1.3 paf 269: if(!source)
1.12 paf 270: return base_class;
1.3 paf 271:
1.25 paf 272: return use_buf(source, file, 0/*new class*/, name, base_class);
1.16 paf 273: }
274:
1.26 paf 275: VStateless_class *Request::use_buf(
276: const char *source, const char *file,
277: VStateless_class *aclass, const String *name,
278: VStateless_class *base_class) {
1.5 paf 279: // compile loaded class
1.26 paf 280: VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1 paf 281:
1.4 paf 282: // locate and execute possible @auto[] static method
1.25 paf 283: execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16 paf 284: return &cclass;
1.20 paf 285: }
286:
287: void Request::fail_if_junction_(bool is,
1.26 paf 288: Value& value, const String& method_name, char *msg) {
1.20 paf 289:
290: // fail_if_junction(true, junction = fail
291: // fail_if_junction(false, not junction = fail
292: if((value.get_junction()!=0) ^ !is)
293: THROW(0, 0,
294: &method_name,
295: msg);
1.19 paf 296: }
297:
298: char *Request::relative(const char *path, const char *file) {
1.21 paf 299: char *result=(char *)malloc(strlen(path)+strlen(file)+1);
1.19 paf 300: strcpy(result, path);
1.32 paf 301: rsplit(result, PATH_DELIMITER_CHAR);
302: strcat(result, PATH_DELIMITER_STRING);
1.19 paf 303: strcat(result, file);
304: return result;
305: }
306:
307: char *Request::absolute(const char *name) {
308: if(name[0]=='/') {
1.35 paf 309: char *result=(char *)malloc(strlen(info.document_root)+strlen(name)+1);
310: strcpy(result, info.document_root);
1.19 paf 311: strcat(result, name);
312: return result;
313: } else
1.40 paf 314: return relative(info.uri, name);
1.1 paf 315: }