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