Annotation of parser3/src/include/pa_request.h, revision 1.147
1.61 paf 1: /** @file
1.62 paf 2: Parser: request class decl.
3:
1.120 paf 4: Copyright (c) 2001, 2002 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.147 ! paf 11: static const char* IDENT_REQUEST_H="$Date: 2002/08/29 12:22:46 $";
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 \
43: const char *source, \
1.46 paf 44: VStateless_class *aclass, const String *name, \
45: VStateless_class *base_class, \
1.40 paf 46: const char *file
47: # define COMPILE(source, aclass, name, base_class, file) \
48: real_compile(source, aclass, name, base_class, file)
1.7 paf 49: #else
1.40 paf 50: # define COMPILE_PARAMS \
51: const char *source, \
1.46 paf 52: VStateless_class *aclass, const String *name, \
53: VStateless_class *base_class
1.40 paf 54: # define COMPILE(source, aclass, name, base_class, file) \
55: real_compile(source, aclass, name, base_class)
1.7 paf 56: #endif
1.4 paf 57:
1.39 paf 58: class Temp_lang;
1.84 paf 59: class Methoded;
1.116 paf 60: class VMethodFrame;
1.1 paf 61:
1.61 paf 62: /// Main workhorse.
1.7 paf 63: class Request : public Pooled {
1.107 paf 64: friend class Temp_lang;
1.117 paf 65: friend class Temp_connection;
1.1 paf 66: public:
1.112 paf 67:
68: #ifdef RESOURCES_DEBUG
69: /// measures
70: double sql_connect_time;
71: double sql_request_time;
72: #endif
1.61 paf 73:
74: /// some information from web server
1.65 paf 75: class Info {
76: public:
1.53 paf 77: const char *document_root;
78: const char *path_translated;
1.56 paf 79: const char *method;
1.53 paf 80: const char *query_string;
1.56 paf 81: const char *uri;
1.53 paf 82: const char *content_type;
83: size_t content_length;
1.60 paf 84: const char *cookie;
1.140 paf 85: bool mail_received;
1.53 paf 86: };
87:
1.50 paf 88: Request(Pool& apool,
1.53 paf 89: 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.46 paf 124: VStateless_class *use_file(
1.94 parser 125: const String& file_name,
126: bool ignore_class_path=false, bool fail_on_read_problem=true,
1.45 paf 127: const String *name=0,
1.46 paf 128: VStateless_class *base_class=0); // core.C
1.64 paf 129: /// compiles a @a source buffer
1.46 paf 130: VStateless_class *use_buf(
1.139 paf 131: const char *source, const String& filespec, const char *filespec_cstr,
1.46 paf 132: VStateless_class *aclass=0, const String *name=0,
133: VStateless_class *base_class=0); // core.C
1.28 paf 134:
1.129 paf 135: /// processes any code-junction there may be inside of @a value
1.131 paf 136: StringOrValue process(Value& input_value, bool intercept_string=true); // execute.C
1.129 paf 137: //@{ convinient helpers
138: const String& process_to_string(Value& input_value) {
139: return process(input_value, true/*intercept_string*/).as_string();
140: }
141: Value& process_to_value(Value& input_value, bool intercept_string=true) {
142: return process(input_value, intercept_string).as_value();
1.126 paf 143: }
144: //@}
1.131 paf 145:
1.126 paf 146:
1.131 paf 147: #define DEFINE_DUAL(modification) \
148: void write_##modification##_lang(StringOrValue dual) { \
149: if(const String *string=dual.get_string()) \
150: write_##modification##_lang(*string); \
151: else \
152: write_##modification##_lang(*dual.get_value()); \
153: }
1.132 paf 154: #define DEFINE_DUAL_CHECKED(modification) \
155: void write_##modification##_lang(StringOrValue dual, const String *origin) { \
156: if(const String *string=dual.get_string()) \
157: write_##modification##_lang(*string); \
158: else \
159: write_##modification##_lang(*dual.get_value(), origin); \
160: }
1.131 paf 161:
1.61 paf 162: /// appending, sure of clean string inside
1.65 paf 163: void write_no_lang(const String& astring) {
1.111 paf 164: wcontext->write(astring,
165: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.49 paf 166: }
1.131 paf 167: /// appending sure value, that would be converted to clean string
168: void write_no_lang(Value& avalue) {
169: if(wcontext->get_in_expression())
170: wcontext->write(avalue);
171: else
172: wcontext->write(avalue,
173: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
174: }
175: //DEFINE_DUAL(no)
176:
1.61 paf 177: /// appending string, passing language built into string being written
1.65 paf 178: void write_pass_lang(const String& astring) {
179: wcontext->write(astring, String::UL_PASS_APPENDED);
1.59 paf 180: }
1.131 paf 181: /// appending possible string, passing language built into string being written
182: void write_pass_lang(Value& avalue) {
183: wcontext->write(avalue, String::UL_PASS_APPENDED);
184: }
185: DEFINE_DUAL(pass)
186:
1.61 paf 187: /// appending possible string, assigning untaint language
1.40 paf 188: void write_assign_lang(Value& avalue) {
1.39 paf 189: wcontext->write(avalue, flang);
1.106 parser 190: }
1.132 paf 191: /// appending possible string, assigning untaint language
192: void write_assign_lang(Value& avalue, const String *origin) {
193: wcontext->write(avalue, flang, origin);
194: }
1.106 parser 195: /// appending string, assigning untaint language
196: void write_assign_lang(const String& astring) {
197: wcontext->write(astring, flang);
1.22 paf 198: }
1.131 paf 199: DEFINE_DUAL(assign)
1.132 paf 200: DEFINE_DUAL_CHECKED(assign)
1.22 paf 201:
1.64 paf 202: /// returns relative to @a path path to @a file
1.69 paf 203: const String& relative(const char *apath, const String& relative_name);
1.61 paf 204:
1.64 paf 205: /// returns an absolute @a path to relative @a name
1.69 paf 206: const String& absolute(const String& relative_name);
1.42 paf 207:
1.80 paf 208: /// returns the mime type of 'user_file_name_cstr'
209: const String& mime_type_of(const char *user_file_name_cstr);
210:
1.117 paf 211: /// returns current SQL connection if any
212: SQL_Connection *connection(const String *source) {
213: if(!fconnection && source)
1.125 paf 214: throw Exception("parser.runtime",
1.117 paf 215: source,
216: "outside of 'connect' operator");
217:
218: return fconnection;
1.133 paf 219: }
220:
221: bool origins_mode() {
1.144 paf 222: return main_class->get_element(*origins_mode_name, main_class, false)!=0; // $ORIGINS mode
1.117 paf 223: }
224:
1.17 paf 225: public:
1.22 paf 226:
1.61 paf 227: /// info from web server
1.53 paf 228: Info& info;
1.81 paf 229: /// user's post data
1.145 paf 230: const char *post_data; size_t post_size;
1.53 paf 231:
1.134 paf 232: /// name of 'main' method
233: const String main_method_name;
234:
1.78 paf 235: /// operators are methods of this class
1.84 paf 236: Methoded& OP;
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.123 paf 254: //@{ request processing status
255: /// execution stack
256: Stack stack;
1.61 paf 257: /// contexts
1.146 paf 258: Value *self;
259: VMethodFrame *method_frame;
260: Value *rcontext;
1.22 paf 261: WContext *wcontext;
1.124 paf 262: /// exception stack trace
263: Stack exception_trace;
1.123 paf 264: //@}
1.85 paf 265:
1.86 paf 266: /// 'MAIN' class conglomerat
1.85 paf 267: VStateless_class *main_class;
1.76 paf 268:
1.87 paf 269: /// classes configured data
270: Hash classes_conf;
271:
1.7 paf 272: private: // core data
1.6 paf 273:
1.89 parser 274: /// classes
1.6 paf 275: Hash fclasses;
1.67 paf 276:
1.89 parser 277: /// already used files to avoid cyclic uses
1.67 paf 278: Hash used_files;
1.89 parser 279:
280: /** endless execute(execute(... preventing counter
281: @see ANTI_ENDLESS_EXECUTE_RECOURSION
282: */
283: uint anti_endless_execute_recoursion;
1.136 paf 284:
285: private:
286:
287: /// already executed some @conf method
288: bool configure_admin_done;
289:
290: void configure_admin(VStateless_class& conf_class, const String *source);
1.99 parser 291:
1.7 paf 292: private: // compile.C
293:
1.46 paf 294: VStateless_class& real_compile(COMPILE_PARAMS);
1.7 paf 295:
296: private: // execute.C
297:
1.139 paf 298: /// for @postprocess[body]
299: const String& execute_method(VMethodFrame& amethodFrame, const Method& method);
300: //{ for @conf[filespec] and @auto[filespec]
301: void execute_method(Value& aself,
302: const Method& method, VString *optional_param,
1.137 paf 303: const String **return_string);
304: void execute_nonvirtual_method(VStateless_class& aclass,
1.139 paf 305: const String& method_name, VString *optional_param,
1.137 paf 306: const String **return_string,
307: const Method **return_method=0);
1.139 paf 308: //}
309: /// for @main[]
310: const String *execute_virtual_method(Value& aself, const String& method_name);
1.9 paf 311:
1.132 paf 312: Value *get_element(const String *& remember_name, bool can_call_operator);
1.22 paf 313:
1.58 paf 314: private: // defaults
315:
1.111 paf 316: const uchar fdefault_lang;
1.68 paf 317: Value *default_content_type;
1.80 paf 318:
319: private: // mime types
320:
321: /// $MAIN:MIME-TYPES
322: Table *mime_types;
1.22 paf 323:
1.39 paf 324: private: // lang manipulation
1.22 paf 325:
1.111 paf 326: uchar set_lang(uchar alang) {
327: uchar result=flang;
1.39 paf 328: flang=alang;
329: return result;
330: }
1.111 paf 331: void restore_lang(uchar alang) {
1.39 paf 332: flang=alang;
333: }
334:
1.117 paf 335: private: // lang&raw
336:
337: uchar flang;
338:
339:
340: private: // connection manipulation
341:
342: SQL_Connection *set_connection(SQL_Connection *aconnection) {
343: SQL_Connection *result=fconnection;
344: fconnection=aconnection;
345: return result;
346: }
347: void restore_connection(SQL_Connection *aconnection) {
348: fconnection=aconnection;
349: }
350:
351: private:
352:
353: /// connection
354: SQL_Connection *fconnection;
1.126 paf 355:
1.75 paf 356: void output_result(const VFile& body_file, bool header_only);
1.39 paf 357: };
358:
1.61 paf 359: /// Auto-object used for temporary changing Request::flang.
1.39 paf 360: class Temp_lang {
361: Request& frequest;
1.111 paf 362: uchar saved_lang;
1.39 paf 363: public:
1.111 paf 364: Temp_lang(Request& arequest, uchar alang) :
1.39 paf 365: frequest(arequest),
366: saved_lang(arequest.set_lang(alang)) {
367: }
368: ~Temp_lang() {
369: frequest.restore_lang(saved_lang);
1.117 paf 370: }
371: };
372:
373: /// Auto-object used for temporary changing Request::fconnection.
374: class Temp_connection {
375: Request& frequest;
376: SQL_Connection *saved_connection;
377: public:
378: Temp_connection(Request& arequest, SQL_Connection *aconnection) :
379: frequest(arequest),
380: saved_connection(arequest.set_connection(aconnection)) {
381: }
382: ~Temp_connection() {
383: frequest.restore_connection(saved_connection);
1.39 paf 384: }
1.91 parser 385: };
386:
387: /**
388: @b method parameters passed in this array.
389: contains handy typecast ad junction/not junction ensurers
390:
391: */
392: class MethodParams : public Array {
393: public:
394: MethodParams(Pool& pool, const String& amethod_name) : Array(pool),
395: fmethod_name(amethod_name) {
396: }
397:
398: /// handy typecast. I long for templates
399: Value& get(int index) {
400: return *static_cast<Value *>(Array::get(index));
401: }
402: /// handy is-value-a-junction ensurer
403: Value& as_junction(int index, const char *msg) {
404: return get_as(index, true, msg);
405: }
406: /// handy value-is-not-a-junction ensurer
407: Value& as_no_junction(int index, const char *msg) {
408: return get_as(index, false, msg);
409: }
410: /// handy expression auto-processing to double
1.102 parser 411: double as_double(int index, const char *msg, Request& r) {
412: return get_processed(index, msg, r).as_double();
1.91 parser 413: }
414: /// handy expression auto-processing to int
1.102 parser 415: int as_int(int index, const char *msg, Request& r) {
416: return get_processed(index, msg, r).as_int();
1.103 parser 417: }
418: /// handy expression auto-processing to bool
419: bool as_bool(int index, const char *msg, Request& r) {
420: return get_processed(index, msg, r).as_bool();
1.91 parser 421: }
422: /// handy string ensurer
423: const String& as_string(int index, const char *msg) {
424: return as_no_junction(index, msg).as_string();
425: }
426:
427: private:
428:
429: /// handy value-is/not-a-junction ensurer
430: Value& get_as(int index, bool as_junction, const char *msg) {
431: Value& result=get(index);
432: if((result.get_junction()!=0) ^ as_junction)
1.125 paf 433: throw Exception("parser.runtime",
1.91 parser 434: &fmethod_name,
435: "%s (parameter #%d)", msg, 1+index);
1.104 parser 436:
1.91 parser 437: return result;
438: }
439:
1.102 parser 440: Value& get_processed(int index, const char *msg, Request& r) {
1.126 paf 441: return r.process_to_value(as_junction(index, msg), 0/*no name*/);
1.91 parser 442: }
443:
444: private:
445:
446: const String& fmethod_name;
447:
1.4 paf 448: };
1.1 paf 449:
450: #endif
E-mail: