Annotation of parser3/src/main/pa_request.C, revision 1.102
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.102 ! paf 8: $Id: pa_request.C,v 1.101 2001/04/07 10:34:45 paf Exp $
1.1 paf 9: */
10:
1.70 paf 11: #include "pa_config_includes.h"
1.19 paf 12:
1.96 paf 13: #include <locale.h>
14:
1.69 paf 15: #include "pa_sapi.h"
1.53 paf 16: #include "pa_common.h"
1.1 paf 17: #include "pa_request.h"
1.3 paf 18: #include "pa_wwrapper.h"
19: #include "pa_vclass.h"
1.100 paf 20: #include "_op.h"
1.17 paf 21: #include "_table.h"
1.82 paf 22: #include "_file.h"
1.31 paf 23: #include "pa_globals.h"
1.30 paf 24: #include "pa_vint.h"
25: #include "pa_vmframe.h"
1.32 paf 26: #include "pa_types.h"
1.78 paf 27: #include "pa_vtable.h"
1.89 paf 28: #include "_random.h"
1.90 paf 29: #include "pa_vfile.h"
1.101 paf 30: #include "_mail.h"
1.3 paf 31:
1.72 paf 32: /// $limits.post_max_size default 10M
33: const size_t MAX_POST_SIZE_DEFAULT=10*0x400*400;
34:
1.82 paf 35: /// content type of exception response, when no @MAIN:exception handler defined
36: const char *UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain";
37:
38: /// content type of response when no $MAIN:defaults.content-type defined
39: const char *DEFAULT_CONTENT_TYPE="text/html";
40:
1.72 paf 41: //
1.28 paf 42: Request::Request(Pool& apool,
1.33 paf 43: Info& ainfo,
1.43 paf 44: String::Untaint_lang adefault_lang) : Pooled(apool),
1.3 paf 45: stack(apool),
1.100 paf 46: OP(apool),
1.41 paf 47: env(apool),
48: form(apool),
49: request(apool, *this),
50: response(apool),
1.51 paf 51: cookie(apool),
1.3 paf 52: fclasses(apool),
1.43 paf 53: fdefault_lang(adefault_lang), flang(adefault_lang),
54: info(ainfo),
1.85 paf 55: used_files(apool),
1.74 paf 56: default_content_type(0),
1.94 paf 57: mime_types(0),
1.95 paf 58: connection(0), protocol2library(0)
1.3 paf 59: {
1.27 paf 60: // root superclass,
1.3 paf 61: // parent of all classes,
62: // operators holder
1.100 paf 63: initialize_op_class(pool(), OP);
64: classes().put(*op_class_name, &OP);
1.27 paf 65: // table class
66: classes().put(*table_class_name, table_class);
1.82 paf 67: // file class
68: classes().put(*file_class_name, file_class);
1.89 paf 69: // random class
70: classes().put(*random_class_name, random_class);
1.3 paf 71: // env class
1.41 paf 72: classes().put(*env_class_name, &env);
1.27 paf 73: // form class
1.41 paf 74: classes().put(*form_class_name, &form);
1.40 paf 75: // request class
1.41 paf 76: classes().put(*request_class_name, &request);
77: // response class
78: classes().put(*response_class_name, &response);
1.51 paf 79: // cookie class
80: classes().put(*cookie_class_name, &cookie);
1.101 paf 81: // mail class
82: classes().put(*mail_class_name, mail_class);
1.45 paf 83: }
84:
1.64 paf 85: /**
86: load MAIN class, execute @main.
87: MAIN class consists of all the auto.p files we'd manage to find
88: plus
89: the file user requested us to process
90: all located classes become children of one another,
91: composing class we name 'MAIN'
1.96 paf 92:
1.102 ! paf 93: @test get read of setlocale
1.64 paf 94: */
1.63 paf 95: void Request::core(const char *root_auto_path, bool root_auto_fail,
96: const char *site_auto_path, bool site_auto_fail,
1.62 paf 97: bool header_only) {
1.29 paf 98: VStateless_class *main_class=0;
1.41 paf 99: bool need_rethrow=false; Exception rethrow_me;
1.3 paf 100: TRY {
1.29 paf 101: char *auto_filespec=(char *)malloc(MAX_STRING);
102:
1.63 paf 103: // loading root auto.p
104: if(root_auto_path) {
1.77 paf 105: String& filespec=*NEW String(pool());
1.88 paf 106: filespec.APPEND_CLEAN(root_auto_path, 0, "root_auto", 0);
1.77 paf 107: filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.29 paf 108: main_class=use_file(
1.77 paf 109: filespec, root_auto_fail,
1.37 paf 110: main_class_name, main_class);
1.29 paf 111: }
112:
1.85 paf 113: // $MAIN:LIMITS hash used here,
1.72 paf 114: // until someone with less privileges have overriden them
1.78 paf 115: {
116: Value *limits=main_class?main_class->get_element(*limits_name):0;
117: // $limits.post_max_size default 10M
118: Value *element=limits?limits->get_element(*post_max_size_name):0;
1.84 paf 119: size_t value=element?(size_t)element->as_double():0;
1.78 paf 120: size_t post_max_size=value?value:MAX_POST_SIZE_DEFAULT;
121:
122: form.fill_fields(*this, post_max_size);
123: }
1.72 paf 124:
1.78 paf 125: // filling cookies
1.72 paf 126: cookie.fill_fields(*this);
127:
1.64 paf 128: // loading site auto.p
1.63 paf 129: if(site_auto_path) {
1.77 paf 130: String& filespec=*NEW String(pool());
1.88 paf 131: filespec.APPEND_CLEAN(site_auto_path, 0, "site_auto", 0);
1.77 paf 132: filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.37 paf 133: main_class=use_file(
1.77 paf 134: filespec, site_auto_fail,
1.37 paf 135: main_class_name, main_class);
1.29 paf 136: }
1.8 paf 137:
1.72 paf 138: // loading auto.p files from document_root/..
139: // to the one beside requested file.
140: // all assigned bases from upper dir
141: {
142: /* /document/root */
1.77 paf 143: char *file_spec=
1.72 paf 144: (char *)malloc(strlen(info.path_translated)+strlen(AUTO_FILE_NAME)+1);
1.83 paf 145: size_t document_root_size=strlen(info.document_root);
146: if(info.document_root[document_root_size-1]=='/')
147: document_root_size--;
148: memcpy(file_spec, info.document_root, document_root_size);
1.72 paf 149:
150: /* /requested/file.html */
151: char *branches=(char *)malloc(strlen(info.path_translated)+1);
1.83 paf 152: strcpy(branches, info.path_translated+document_root_size);
1.72 paf 153: char *next=branches;
1.83 paf 154: char *append_here=file_spec+document_root_size;
1.72 paf 155:
156: size_t slash_auto_p_size=strlen("/" AUTO_FILE_NAME);
157: while(true) {
158: char *step=lsplit(&next, '/');
159: if(next) { // not 'file.html' part
160: size_t step_size=strlen(step);
161: memcpy(append_here, step, step_size);
162: append_here+=step_size;
163: memcpy(append_here, "/" AUTO_FILE_NAME, slash_auto_p_size+1);
164: append_here++/* / */;
165:
1.77 paf 166: String& sfile_spec=*NEW String(pool());
1.88 paf 167: sfile_spec.APPEND_CLEAN(file_spec, 0, "scanned", 0);
1.77 paf 168: main_class=use_file(sfile_spec, false/*ignore read problem*/,
1.72 paf 169: main_class_name, main_class);
170: } else
171: break;
172: }
173: }
1.34 paf 174:
1.72 paf 175: // compiling requested file
1.77 paf 176: String& spath_translated=*NEW String(pool());
1.88 paf 177: spath_translated.APPEND_CLEAN(info.path_translated, 0, "user-request", 0);
1.77 paf 178: main_class=use_file(spath_translated, true/*don't ignore read problem*/,
1.72 paf 179: main_class_name, main_class);
1.7 paf 180:
1.85 paf 181: // $MAIN:DEFAULTS
1.78 paf 182: Value *defaults=main_class->get_element(*defaults_name);
1.75 paf 183: // value must be allocated on request's pool for that pool used on
184: // meaning constructing @see attributed_meaning_to_string
1.80 paf 185: default_content_type=defaults?defaults->get_element(*content_type_name):0;
1.78 paf 186: if(Value *element=main_class->get_element(*html_typo_name))
1.87 paf 187: if(Table *table=element->get_table())
188: pool().set_tag(table);
1.95 paf 189:
190: // $MAIN:SQL.drivers
191: if(Value *sql=main_class->get_element(*main_sql_name))
192: if(Value *element=sql->get_element(*main_sql_drivers_name))
193: protocol2library=element->get_table();
1.85 paf 194:
195: // $MAIN:MIME-TYPES
196: if(Value *element=main_class->get_element(*mime_types_name))
197: if(Table *table=element->get_table())
198: mime_types=table;
1.96 paf 199:
200: // $MAIN:LOCALE.ctype[Russian_Russia.1251]
201: /*
202: #define LC_ALL 0
203: #define LC_COLLATE 1
204: #define LC_CTYPE 2
205: #define LC_MONETARY 3
206: #define LC_NUMERIC 4
207: #define LC_TIME 5
208: */
209: if(Value *locale=main_class->get_element(*locale_name))
210: if(Value *element=locale->get_element(*locale_ctype_name)) {
211: const String& name=element->as_string();
212: if(!setlocale(LC_CTYPE, name.cstr()))
213: THROW(0, 0,
214: &name,
1.97 paf 215: "locale is invalid");
1.96 paf 216: }
1.102 ! paf 217:
! 218: // $MAIN:MAIL[$SMTP[mail.design.ru]]
! 219: if(Value *mail_element=main_class->get_element(*mail_name))
! 220: if(!(mail=mail_element->get_hash()))
! 221: THROW(0, 0,
! 222: 0,
! 223: "$MAIN:MAIL is not hash");
! 224:
1.25 paf 225:
226: // execute @main[]
1.41 paf 227: const String *body_string=execute_method(*main_class, *main_method_name);
228: if(!body_string)
1.25 paf 229: THROW(0,0,
230: 0,
231: "'"MAIN_METHOD_NAME"' method not found");
1.79 paf 232:
1.91 paf 233: VString body_vstring_before_post_process(*body_string);
234: VString *body_vstring_after_post_process=&body_vstring_before_post_process;
235:
236: // post-process
237: if(Value *value=main_class->get_element(*post_process_method_name))
238: if(Junction *junction=value->get_junction())
239: if(const Method *method=junction->method) {
240: // preparing to pass parameters to
241: // @post-process[data]
1.98 paf 242: VMethodFrame frame(pool(), *junction, false);
1.91 paf 243: frame.set_self(*main_class);
244:
245: frame.store_param(method->name,
246: &body_vstring_before_post_process);
247: body_vstring_after_post_process=
248: NEW VString(*execute_method(frame, *method));
249: }
1.90 paf 250:
1.91 paf 251: const VFile *body_file=body_vstring_after_post_process->as_vfile();
1.41 paf 252:
1.45 paf 253: // extract response body
254: Value *body_value=static_cast<Value *>(
255: response.fields().get(*body_name));
256: if(body_value) // there is some $request.body
1.90 paf 257: body_file=body_value->as_vfile();
1.45 paf 258:
259: // OK. write out the result
1.90 paf 260: output_result(*body_file, header_only);
1.3 paf 261: }
1.76 paf 262: CATCH(e) { // request handling problem
263: // we're returning not result, but error explanation
1.30 paf 264: TRY {
1.76 paf 265: // log the beast
266: const String *problem_source=e.problem_source();
267: if(problem_source)
268: SAPI::log(pool(),
269: #ifndef NO_STRING_ORIGIN
270: "%s(%d): "
271: #endif
272: "'%s' %s [%s %s]",
273: #ifndef NO_STRING_ORIGIN
274: problem_source->origin().file?problem_source->origin().file:"global",
275: problem_source->origin().line,
276: #endif
1.99 paf 277: problem_source->cstr(String::UL_AS_IS),
1.76 paf 278: e.comment(),
1.99 paf 279: e.type()?e.type()->cstr(String::UL_AS_IS):"-",
280: e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76 paf 281: );
282: else
283: SAPI::log(pool(),
284: "%s [%s %s]",
285: e.comment(),
1.99 paf 286: e.type()?e.type()->cstr(String::UL_AS_IS):"-",
287: e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76 paf 288: );
1.43 paf 289:
290: // reset language to default
291: flang=fdefault_lang;
292:
293: // reset response
294: response.fields().clear();
295:
296: // this is what we'd return in $response:body
1.41 paf 297: const String *body_string=0;
1.43 paf 298:
1.30 paf 299: if(main_class) { // we've managed to end up with some main_class
300: // maybe we'd be lucky enough as to report an error
301: // in a gracefull way...
302: if(Value *value=main_class->get_element(*exception_method_name))
303: if(Junction *junction=value->get_junction())
304: if(const Method *method=junction->method) {
305: // preparing to pass parameters to
306: // @exception[origin;source;comment;type;code]
1.98 paf 307: VMethodFrame frame(pool(), *junction, false);
1.38 paf 308: frame.set_self(*main_class);
1.30 paf 309:
310: const String *problem_source=e.problem_source();
311: // origin
1.38 paf 312: Value *origin_value=0;
1.30 paf 313: #ifndef NO_STRING_ORIGIN
1.38 paf 314: if(problem_source) {
315: const Origin& origin=problem_source->origin();
316: if(origin.file) {
317: char *buf=(char *)malloc(MAX_STRING);
1.42 paf 318: snprintf(buf, MAX_STRING, "%s(%d):",
1.38 paf 319: origin.file, 1+origin.line);
1.44 paf 320: String *origin_file_line=NEW String(pool(),
321: buf, true);
1.38 paf 322: origin_value=NEW VString(*origin_file_line);
323: }
1.30 paf 324: }
325: #endif
1.91 paf 326: frame.store_param(method->name,
1.38 paf 327: origin_value?origin_value:NEW VUnknown(pool()));
1.28 paf 328:
1.30 paf 329: // source
1.38 paf 330: Value *source_value=0;
1.43 paf 331: if(problem_source) {
1.47 paf 332: String& problem_source_copy=*NEW String(pool());
333: problem_source_copy.append(*problem_source,
334: flang, true);
1.43 paf 335: source_value=NEW VString(problem_source_copy);
336: }
1.91 paf 337: frame.store_param(method->name,
1.38 paf 338: source_value?source_value:NEW VUnknown(pool()));
1.30 paf 339:
340: // comment
1.44 paf 341: String *comment_value=NEW String(pool(),
342: e.comment(), true);
1.91 paf 343: frame.store_param(method->name,
1.30 paf 344: NEW VString(*comment_value));
345:
346: // type
347: Value *type_value;
1.43 paf 348: if(e.type()) {
1.47 paf 349: String& type_copy=*NEW String(pool());
350: type_value=NEW VString(type_copy.append(*e.type(),
351: flang, true));
1.43 paf 352: } else
1.30 paf 353: type_value=NEW VUnknown(pool());
1.91 paf 354: frame.store_param(method->name, type_value);
1.30 paf 355:
356: // code
357: Value *code_value;
1.43 paf 358: if(e.code()) {
1.47 paf 359: String& code_copy=*NEW String(pool());
360: code_value=NEW VString(code_copy.append(*e.code(),
361: flang, true));
1.43 paf 362: } else
1.30 paf 363: code_value=NEW VUnknown(pool());
1.91 paf 364: frame.store_param(method->name, code_value);
1.30 paf 365:
1.43 paf 366: // future $response:body=
367: // execute ^exception[origin;source;comment;type;code]
368: body_string=execute_method(frame, *method);
1.30 paf 369: }
370: }
371:
1.41 paf 372: if(!body_string) { // couldn't report an error beautifully?
373: // doing that ugly
374:
375: // make up result: $origin $source $comment $type $code
376: char *buf=(char *)malloc(MAX_STRING);
1.30 paf 377: size_t printed=0;
378: const String *problem_source=e.problem_source();
379: if(problem_source) {
1.16 paf 380: #ifndef NO_STRING_ORIGIN
1.30 paf 381: const Origin& origin=problem_source->origin();
382: if(origin.file)
1.41 paf 383: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ",
1.30 paf 384: origin.file, 1+origin.line);
1.28 paf 385: #endif
1.41 paf 386: printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ",
1.99 paf 387: problem_source->cstr(String::UL_AS_IS));
1.30 paf 388: }
1.41 paf 389: printed+=snprintf(buf+printed, MAX_STRING-printed, "%s",
1.30 paf 390: e.comment());
391: const String *type=e.type();
392: if(type) {
1.41 paf 393: printed+=snprintf(buf+printed, MAX_STRING-printed, " type: %s",
1.99 paf 394: type->cstr(String::UL_AS_IS));
1.30 paf 395: const String *code=e.code();
396: if(code)
1.41 paf 397: printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s",
1.99 paf 398: code->cstr(String::UL_AS_IS));
1.30 paf 399: }
1.43 paf 400:
401: // future $response:content-type
1.49 paf 402: response.fields().put(*content_type_name,
1.82 paf 403: NEW VString(*NEW String(pool(), UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.43 paf 404: // future $response:body
1.44 paf 405: body_string=NEW String(pool(), buf);
1.30 paf 406: }
1.41 paf 407:
1.90 paf 408: const VString body_vstring(*body_string);
409: const VFile *body_file=body_vstring.as_vfile();
410:
1.45 paf 411: // ERROR. write it out
1.90 paf 412: output_result(*body_file, header_only);
1.3 paf 413: }
1.30 paf 414: CATCH(e) {
1.41 paf 415: // exception in request exception handler
416: // remember to rethrow it
417: rethrow_me=e; need_rethrow=true;
1.1 paf 418: }
1.30 paf 419: END_CATCH
1.1 paf 420: }
1.41 paf 421: END_CATCH // do not use pool() after this point - no exception handler set
422: // any throw() would try to use zero exception() pointer
423:
1.91 paf 424: if(need_rethrow) // were there an exception for us to rethrow?
1.62 paf 425: THROW(rethrow_me.type(), rethrow_me.code(),
1.41 paf 426: rethrow_me.problem_source(),
427: rethrow_me.comment());
1.1 paf 428: }
429:
1.77 paf 430: VStateless_class *Request::use_file(const String& file_spec, bool fail_on_read_problem,
1.26 paf 431: const String *name,
432: VStateless_class *base_class) {
1.73 paf 433: // cyclic dependence check
1.77 paf 434: if(used_files.get(file_spec))
1.73 paf 435: return base_class;
1.77 paf 436: used_files.put(file_spec, (Hash::Val *)true);
1.73 paf 437:
438:
1.77 paf 439: char *source=file_read_text(pool(), file_spec, fail_on_read_problem);
1.3 paf 440: if(!source)
1.12 paf 441: return base_class;
1.3 paf 442:
1.77 paf 443: return use_buf(source, file_spec.cstr(), 0/*new class*/, name, base_class);
1.16 paf 444: }
445:
1.67 paf 446: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26 paf 447: VStateless_class *aclass, const String *name,
448: VStateless_class *base_class) {
1.5 paf 449: // compile loaded class
1.26 paf 450: VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1 paf 451:
1.4 paf 452: // locate and execute possible @auto[] static method
1.25 paf 453: execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16 paf 454: return &cclass;
1.20 paf 455: }
456:
1.52 paf 457: /**
458: - fail_if_junction(true, junction = fail
459: - fail_if_junction(false, not junction = fail
460: */
1.62 paf 461: void Request::fail_if_junction_(bool is, Value& value,
462: const String& method_name, const char *msg) {
1.20 paf 463:
464: if((value.get_junction()!=0) ^ !is)
465: THROW(0, 0,
466: &method_name,
467: msg);
1.19 paf 468: }
469:
1.77 paf 470: const String& Request::relative(const char *apath, const String& relative_name) {
471: int lpath_buf_size=strlen(apath)+1;
472: char *lpath=(char *)malloc(lpath_buf_size);
473: memcpy(lpath, apath, lpath_buf_size);
474: rsplit(lpath, '/');
475: String& result=*NEW String(pool(), lpath);
476: result.APPEND_CONST("/");
477: result.append(relative_name, String::UL_PASS_APPENDED);
1.19 paf 478: return result;
479: }
480:
1.77 paf 481: const String& Request::absolute(const String& relative_name) {
482: char *relative_name_cstr=relative_name.cstr();
483: if(relative_name_cstr[0]=='/') {
484: String& result=*NEW String(pool(), info.document_root);
485: result.append(relative_name, String::UL_PASS_APPENDED);
1.19 paf 486: return result;
487: } else
1.77 paf 488: return relative(info.path_translated, relative_name);
1.1 paf 489: }
1.45 paf 490:
1.101 paf 491: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning,
492: void *info) {
493: String *attribute_to_exclude=static_cast<String *>(info);
494: if(aattribute==*attribute_to_exclude)
495: return;
496:
497: Value& lmeaning=*static_cast<Value *>(ameaning);
498: Pool& pool=lmeaning.pool();
499:
500: String attribute(pool);
501: SAPI::add_header_attribute(pool,
502: attribute.append(aattribute, String::UL_HTTP_HEADER, true).cstr(),
503: attributed_meaning_to_string(lmeaning, String::UL_HTTP_HEADER).cstr());
504: }
1.90 paf 505: void Request::output_result(const VFile& body_file, bool header_only) {
1.51 paf 506: // header: cookies
507: cookie.output_result();
508:
1.90 paf 509: // set content-type
510: if(String *body_file_content_type=static_cast<String *>(
511: body_file.fields().get(*vfile_mime_type_name))) {
512: // body file content type
513: response.fields().put(*content_type_name, body_file_content_type);
514: } else {
515: // default content type
516: response.fields().put_dont_replace(*content_type_name,
517: default_content_type?default_content_type
518: :NEW VString(*NEW String(pool(), DEFAULT_CONTENT_TYPE)));
1.92 paf 519: }
520:
521: // content-disposition
522: if(VString *vfile_name=static_cast<VString *>(body_file.fields().get(*name_name))) {
523: VHash& vhash=*NEW VHash(pool());
524: vhash.hash().put(*content_disposition_filename_name, vfile_name);
525: response.fields().put(*content_disposition_name, &vhash);
1.90 paf 526: }
1.49 paf 527:
1.62 paf 528: // prepare header: $response:fields without :body
1.73 paf 529: response.fields().for_each(add_header_attribute, /*excluding*/ body_name);
1.50 paf 530:
1.62 paf 531: // prepare...
1.90 paf 532: const void *body=body_file.value_ptr();
533: size_t content_length=body_file.value_size();
1.50 paf 534:
1.62 paf 535: // prepare header: content-length
1.65 paf 536: if(content_length) { // useful for redirecting [header "location: http://..."]
537: char content_length_cstr[MAX_NUMBER];
1.66 paf 538: snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.69 paf 539: SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.65 paf 540: }
1.62 paf 541:
542: // send header
1.69 paf 543: SAPI::send_header(pool());
1.71 paf 544:
1.62 paf 545: // send body
546: if(!header_only)
1.69 paf 547: SAPI::send_body(pool(), body, content_length);
1.50 paf 548: }
E-mail: