Annotation of parser3/src/include/pa_request.h, revision 1.160.2.4
1.61 paf 1: /** @file
1.62 paf 2: Parser: request class decl.
3:
1.160 paf 4: Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.121 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_REQUEST_H
9: #define PA_REQUEST_H
1.141 paf 10:
1.160.2.4! paf 11: static const char* IDENT_REQUEST_H="$Date: 2003/01/29 09:16:15 $";
1.1 paf 12:
13: #include "pa_pool.h"
1.5 paf 14: #include "pa_hash.h"
1.7 paf 15: #include "pa_wcontext.h"
1.6 paf 16: #include "pa_value.h"
1.7 paf 17: #include "pa_stack.h"
1.12 paf 18: #include "pa_vclass.h"
1.45 paf 19: #include "pa_vobject.h"
1.47 paf 20: #include "pa_venv.h"
1.108 paf 21: #include "pa_vstatus.h"
1.48 paf 22: #include "pa_vform.h"
1.140 paf 23: #include "pa_vmail.h"
1.90 parser 24: #include "pa_vmath.h"
1.56 paf 25: #include "pa_vrequest.h"
1.57 paf 26: #include "pa_vresponse.h"
1.60 paf 27: #include "pa_vcookie.h"
1.77 paf 28: #include "pa_sql_driver_manager.h"
1.44 paf 29:
1.112 paf 30: #ifdef RESOURCES_DEBUG
31: #include <sys/resource.h>
32: #endif
33:
1.128 paf 34: // consts
35:
1.134 paf 36: #define MAIN_METHOD_NAME "main"
1.128 paf 37: const uint ANTI_ENDLESS_EXECUTE_RECOURSION=500;
38:
39: // defines
40:
1.7 paf 41: #ifndef NO_STRING_ORIGIN
1.40 paf 42: # define COMPILE_PARAMS \
1.154 paf 43: VStateless_class& aclass, const char *source \
44: , const char *file
45: # define COMPILE(aclass, source, file) \
46: real_compile(aclass, source, file)
1.7 paf 47: #else
1.40 paf 48: # define COMPILE_PARAMS \
1.154 paf 49: VStateless_class& aclass, const char *source
50: # define COMPILE(aclass, source, file) \
51: real_compile(aclass, source)
1.7 paf 52: #endif
1.4 paf 53:
1.39 paf 54: class Temp_lang;
1.84 paf 55: class Methoded;
1.116 paf 56: class VMethodFrame;
1.1 paf 57:
1.160.2.3 paf 58: /// some information from web server
59: class Info {
60: public:
61: const char *document_root;
62: const char *path_translated;
63: const char *method;
64: const char *query_string;
65: const char *uri;
66: const char *content_type;
67: size_t content_length;
68: const char *cookie;
69: bool mail_received;
70: };
71:
1.61 paf 72: /// Main workhorse.
1.7 paf 73: class Request : public Pooled {
1.107 paf 74: friend class Temp_lang;
1.117 paf 75: friend class Temp_connection;
1.148 paf 76: friend class Request_context_saver;
1.155 paf 77: friend class Temp_request_self;
1.1 paf 78: public:
1.112 paf 79:
1.160.2.1 paf 80: Pool& pool() { return fpool; }
81:
1.112 paf 82: #ifdef RESOURCES_DEBUG
83: /// measures
84: double sql_connect_time;
85: double sql_request_time;
86: #endif
1.61 paf 87:
1.160.2.1 paf 88: Request(
1.160.2.3 paf 89: Request_info& ainfo,
1.111 paf 90: uchar adefault_lang, ///< all tainted data default untainting lang
1.110 paf 91: bool status_allowed ///< status class allowed
1.50 paf 92: );
1.118 paf 93: ~Request();
1.1 paf 94:
1.61 paf 95: /// global classes
1.6 paf 96: Hash& classes() { return fclasses; }
97:
1.65 paf 98: /**
99: core request processing
100:
101: BEWARE: may throw exception to you: catch it!
102: */
103: void core(
1.138 paf 104: const char *config_filespec, ///< system config filespec
105: bool config_fail_on_read_problem, ///< fail if system config file not found
1.65 paf 106: bool header_only);
1.17 paf 107:
1.61 paf 108: /// executes ops
1.147 paf 109: void execute(const Array& ops); // execute.C
1.127 paf 110: /// execute ops with anti-recoursion check
1.128 paf 111: void recoursion_checked_execute(const String *name, const Array& ops) {
112: // anti_endless_execute_recoursion
113: if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
114: anti_endless_execute_recoursion=0; // give @exception a chance
115: throw Exception("parser.runtime",
116: name,
117: "call canceled - endless recursion detected");
118: }
1.147 paf 119: execute(ops); // execute it
1.128 paf 120: anti_endless_execute_recoursion--;
121: }
1.40 paf 122:
1.64 paf 123: /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.154 paf 124: VStateless_class *use_file(VStateless_class& aclass,
1.94 parser 125: const String& file_name,
1.149 paf 126: bool ignore_class_path=false,
1.154 paf 127: bool fail_on_read_problem=true, bool fail_on_file_absence=true); // core.C
1.64 paf 128: /// compiles a @a source buffer
1.154 paf 129: VStateless_class *use_buf(VStateless_class& aclass,
130: const char *source,
131: const String& filespec, const char *filespec_cstr); // core.C
1.28 paf 132:
1.129 paf 133: /// processes any code-junction there may be inside of @a value
1.131 paf 134: StringOrValue process(Value& input_value, bool intercept_string=true); // execute.C
1.129 paf 135: //@{ convinient helpers
136: const String& process_to_string(Value& input_value) {
137: return process(input_value, true/*intercept_string*/).as_string();
138: }
139: Value& process_to_value(Value& input_value, bool intercept_string=true) {
140: return process(input_value, intercept_string).as_value();
1.126 paf 141: }
142: //@}
1.131 paf 143:
1.126 paf 144:
1.131 paf 145: #define DEFINE_DUAL(modification) \
146: void write_##modification##_lang(StringOrValue dual) { \
147: if(const String *string=dual.get_string()) \
148: write_##modification##_lang(*string); \
149: else \
150: write_##modification##_lang(*dual.get_value()); \
151: }
1.132 paf 152: #define DEFINE_DUAL_CHECKED(modification) \
153: void write_##modification##_lang(StringOrValue dual, const String *origin) { \
154: if(const String *string=dual.get_string()) \
155: write_##modification##_lang(*string); \
156: else \
157: write_##modification##_lang(*dual.get_value(), origin); \
158: }
1.131 paf 159:
1.61 paf 160: /// appending, sure of clean string inside
1.65 paf 161: void write_no_lang(const String& astring) {
1.111 paf 162: wcontext->write(astring,
163: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.49 paf 164: }
1.131 paf 165: /// appending sure value, that would be converted to clean string
166: void write_no_lang(Value& avalue) {
167: if(wcontext->get_in_expression())
168: wcontext->write(avalue);
169: else
170: wcontext->write(avalue,
171: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
172: }
173: //DEFINE_DUAL(no)
174:
1.61 paf 175: /// appending string, passing language built into string being written
1.65 paf 176: void write_pass_lang(const String& astring) {
177: wcontext->write(astring, String::UL_PASS_APPENDED);
1.59 paf 178: }
1.131 paf 179: /// appending possible string, passing language built into string being written
180: void write_pass_lang(Value& avalue) {
181: wcontext->write(avalue, String::UL_PASS_APPENDED);
182: }
183: DEFINE_DUAL(pass)
184:
1.61 paf 185: /// appending possible string, assigning untaint language
1.40 paf 186: void write_assign_lang(Value& avalue) {
1.39 paf 187: wcontext->write(avalue, flang);
1.106 parser 188: }
1.132 paf 189: /// appending possible string, assigning untaint language
190: void write_assign_lang(Value& avalue, const String *origin) {
191: wcontext->write(avalue, flang, origin);
192: }
1.106 parser 193: /// appending string, assigning untaint language
194: void write_assign_lang(const String& astring) {
195: wcontext->write(astring, flang);
1.22 paf 196: }
1.131 paf 197: DEFINE_DUAL(assign)
1.132 paf 198: DEFINE_DUAL_CHECKED(assign)
1.22 paf 199:
1.64 paf 200: /// returns relative to @a path path to @a file
1.69 paf 201: const String& relative(const char *apath, const String& relative_name);
1.61 paf 202:
1.64 paf 203: /// returns an absolute @a path to relative @a name
1.69 paf 204: const String& absolute(const String& relative_name);
1.42 paf 205:
1.80 paf 206: /// returns the mime type of 'user_file_name_cstr'
207: const String& mime_type_of(const char *user_file_name_cstr);
208:
1.117 paf 209: /// returns current SQL connection if any
210: SQL_Connection *connection(const String *source) {
211: if(!fconnection && source)
1.125 paf 212: throw Exception("parser.runtime",
1.117 paf 213: source,
214: "outside of 'connect' operator");
215:
216: return fconnection;
1.133 paf 217: }
218:
1.154 paf 219: bool origins_mode();
1.117 paf 220:
1.158 paf 221: void interrupt() { finterrupted=true; }
222: bool interrupted() { return finterrupted; }
223:
1.17 paf 224: public:
1.22 paf 225:
1.61 paf 226: /// info from web server
1.160.2.4! paf 227: Request_info& info;
1.160.2.2 paf 228:
229: /// source, client, mail charsets
1.160.2.4! paf 230: Request_charsets charset_info;
1.53 paf 231:
1.134 paf 232: /// name of 'main' method
233: const String main_method_name;
234:
1.154 paf 235: /// 'MAIN' class conglomerat & operators are methods of this class
236: VClass& main_class;
1.86 paf 237: /// $env:fields
1.57 paf 238: VEnv env;
1.108 paf 239: /// $status:fields
240: VStatus status;
1.86 paf 241: /// $form:elements
1.57 paf 242: VForm form;
1.140 paf 243: /// $mail
244: VMail mail;
1.90 parser 245: /// $math:constants
246: VMath math;
1.86 paf 247: /// $request:elements
1.57 paf 248: VRequest request;
1.86 paf 249: /// $response:elements
1.57 paf 250: VResponse response;
1.86 paf 251: /// $cookie:elements
1.60 paf 252: VCookie cookie;
1.70 paf 253:
1.148 paf 254: /// classes configured data
255: Hash classes_conf;
256:
257: private:
1.160.2.1 paf 258:
259: Pool fpool;
1.148 paf 260:
1.123 paf 261: //@{ request processing status
1.148 paf 262: /// exception stack trace
263: Stack exception_trace;
1.123 paf 264: /// execution stack
265: Stack stack;
1.61 paf 266: /// contexts
1.146 paf 267: VMethodFrame *method_frame;
268: Value *rcontext;
1.22 paf 269: WContext *wcontext;
1.148 paf 270: /// current language
271: uchar flang;
272: /// current connection
273: SQL_Connection *fconnection;
1.123 paf 274: //@}
1.158 paf 275: /// interrupted flag, raised on signals [SIGPIPE]
276: bool finterrupted;
1.85 paf 277:
1.148 paf 278: public: // status read methods
1.76 paf 279:
1.148 paf 280: VMethodFrame *get_method_frame() { return method_frame; }
1.157 paf 281: Value *get_self();
1.87 paf 282:
1.7 paf 283: private: // core data
1.6 paf 284:
1.89 parser 285: /// classes
1.6 paf 286: Hash fclasses;
1.67 paf 287:
1.89 parser 288: /// already used files to avoid cyclic uses
1.67 paf 289: Hash used_files;
1.89 parser 290:
291: /** endless execute(execute(... preventing counter
292: @see ANTI_ENDLESS_EXECUTE_RECOURSION
293: */
294: uint anti_endless_execute_recoursion;
1.136 paf 295:
296: private:
297:
298: /// already executed some @conf method
299: bool configure_admin_done;
300:
301: void configure_admin(VStateless_class& conf_class, const String *source);
1.99 parser 302:
1.7 paf 303: private: // compile.C
304:
1.46 paf 305: VStateless_class& real_compile(COMPILE_PARAMS);
1.7 paf 306:
307: private: // execute.C
308:
1.139 paf 309: /// for @postprocess[body]
310: const String& execute_method(VMethodFrame& amethodFrame, const Method& method);
311: //{ for @conf[filespec] and @auto[filespec]
312: void execute_method(Value& aself,
313: const Method& method, VString *optional_param,
1.137 paf 314: const String **return_string);
315: void execute_nonvirtual_method(VStateless_class& aclass,
1.154 paf 316: const String& method_name, VString *optional_param,
1.137 paf 317: const String **return_string,
318: const Method **return_method=0);
1.139 paf 319: //}
320: /// for @main[]
321: const String *execute_virtual_method(Value& aself, const String& method_name);
1.9 paf 322:
1.156 paf 323: Value *get_element(const String *& remember_name, bool can_call_operator);
1.22 paf 324:
1.58 paf 325: private: // defaults
326:
1.111 paf 327: const uchar fdefault_lang;
1.68 paf 328: Value *default_content_type;
1.80 paf 329:
330: private: // mime types
331:
332: /// $MAIN:MIME-TYPES
333: Table *mime_types;
1.22 paf 334:
1.39 paf 335: private: // lang manipulation
1.22 paf 336:
1.111 paf 337: uchar set_lang(uchar alang) {
338: uchar result=flang;
1.39 paf 339: flang=alang;
340: return result;
341: }
1.111 paf 342: void restore_lang(uchar alang) {
1.39 paf 343: flang=alang;
344: }
345:
1.117 paf 346: private: // connection manipulation
347:
348: SQL_Connection *set_connection(SQL_Connection *aconnection) {
349: SQL_Connection *result=fconnection;
350: fconnection=aconnection;
351: return result;
352: }
353: void restore_connection(SQL_Connection *aconnection) {
354: fconnection=aconnection;
355: }
356:
357: private:
358:
1.159 paf 359: void output_result(const VFile& body_file, bool header_only, bool as_attachment);
1.148 paf 360: };
361:
362: /// Auto-object used to save request context across ^try body
363: class Request_context_saver {
364: Request& fr;
365:
366: /// exception stack trace
367: int exception_trace;
368: /// execution stack
369: int stack;
370: /// contexts
371: VMethodFrame *method_frame;
372: Value *rcontext;
373: WContext *wcontext;
374: /// current language
375: uchar flang;
376: /// current connection
1.117 paf 377: SQL_Connection *fconnection;
1.126 paf 378:
1.148 paf 379: public:
380: Request_context_saver(Request& ar) :
381: exception_trace(ar.exception_trace.top_index()),
382: stack(ar.stack.top_index()),
383: method_frame(ar.method_frame),
384: rcontext(ar.rcontext),
385: wcontext(ar.wcontext),
386: flang(ar.flang),
387: fconnection(ar.fconnection),
388: fr(ar) {}
1.153 paf 389: void restore() {
1.148 paf 390: fr.exception_trace.top_index(exception_trace);
391: fr.stack.top_index(stack);
1.157 paf 392: fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148 paf 393: fr.flang=flang;
394: fr.fconnection=fconnection;
395: }
1.39 paf 396: };
397:
1.61 paf 398: /// Auto-object used for temporary changing Request::flang.
1.39 paf 399: class Temp_lang {
400: Request& frequest;
1.111 paf 401: uchar saved_lang;
1.39 paf 402: public:
1.111 paf 403: Temp_lang(Request& arequest, uchar alang) :
1.39 paf 404: frequest(arequest),
405: saved_lang(arequest.set_lang(alang)) {
406: }
407: ~Temp_lang() {
408: frequest.restore_lang(saved_lang);
1.117 paf 409: }
410: };
411:
412: /// Auto-object used for temporary changing Request::fconnection.
413: class Temp_connection {
414: Request& frequest;
415: SQL_Connection *saved_connection;
416: public:
417: Temp_connection(Request& arequest, SQL_Connection *aconnection) :
418: frequest(arequest),
419: saved_connection(arequest.set_connection(aconnection)) {
420: }
421: ~Temp_connection() {
422: frequest.restore_connection(saved_connection);
1.39 paf 423: }
1.91 parser 424: };
425:
426: /**
427: @b method parameters passed in this array.
428: contains handy typecast ad junction/not junction ensurers
429:
430: */
431: class MethodParams : public Array {
432: public:
433: MethodParams(Pool& pool, const String& amethod_name) : Array(pool),
434: fmethod_name(amethod_name) {
435: }
436:
437: /// handy typecast. I long for templates
438: Value& get(int index) {
439: return *static_cast<Value *>(Array::get(index));
440: }
441: /// handy is-value-a-junction ensurer
442: Value& as_junction(int index, const char *msg) {
443: return get_as(index, true, msg);
444: }
445: /// handy value-is-not-a-junction ensurer
446: Value& as_no_junction(int index, const char *msg) {
447: return get_as(index, false, msg);
448: }
449: /// handy expression auto-processing to double
1.102 parser 450: double as_double(int index, const char *msg, Request& r) {
451: return get_processed(index, msg, r).as_double();
1.91 parser 452: }
453: /// handy expression auto-processing to int
1.102 parser 454: int as_int(int index, const char *msg, Request& r) {
455: return get_processed(index, msg, r).as_int();
1.103 parser 456: }
457: /// handy expression auto-processing to bool
458: bool as_bool(int index, const char *msg, Request& r) {
459: return get_processed(index, msg, r).as_bool();
1.91 parser 460: }
461: /// handy string ensurer
462: const String& as_string(int index, const char *msg) {
463: return as_no_junction(index, msg).as_string();
464: }
465:
466: private:
467:
468: /// handy value-is/not-a-junction ensurer
469: Value& get_as(int index, bool as_junction, const char *msg) {
470: Value& result=get(index);
471: if((result.get_junction()!=0) ^ as_junction)
1.125 paf 472: throw Exception("parser.runtime",
1.91 parser 473: &fmethod_name,
474: "%s (parameter #%d)", msg, 1+index);
1.104 parser 475:
1.91 parser 476: return result;
477: }
478:
1.102 parser 479: Value& get_processed(int index, const char *msg, Request& r) {
1.126 paf 480: return r.process_to_value(as_junction(index, msg), 0/*no name*/);
1.91 parser 481: }
482:
483: private:
484:
485: const String& fmethod_name;
486:
1.4 paf 487: };
1.1 paf 488:
489: #endif
E-mail: