Annotation of parser3/src/include/pa_request.h, revision 1.127
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.32 paf 6:
1.127 ! paf 7: $Id: pa_request.h,v 1.126 2002/04/10 09:53:14 paf Exp $
1.1 paf 8: */
9:
10: #ifndef PA_REQUEST_H
11: #define PA_REQUEST_H
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.90 parser 23: #include "pa_vmath.h"
1.56 paf 24: #include "pa_vrequest.h"
1.57 paf 25: #include "pa_vresponse.h"
1.60 paf 26: #include "pa_vcookie.h"
1.77 paf 27: #include "pa_sql_driver_manager.h"
1.44 paf 28:
1.112 paf 29: #ifdef RESOURCES_DEBUG
30: #include <sys/resource.h>
31: #endif
32:
1.7 paf 33: #ifndef NO_STRING_ORIGIN
1.40 paf 34: # define COMPILE_PARAMS \
35: const char *source, \
1.46 paf 36: VStateless_class *aclass, const String *name, \
37: VStateless_class *base_class, \
1.40 paf 38: const char *file
39: # define COMPILE(source, aclass, name, base_class, file) \
40: real_compile(source, aclass, name, base_class, file)
1.7 paf 41: #else
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: # define COMPILE(source, aclass, name, base_class, file) \
47: real_compile(source, aclass, name, base_class)
1.7 paf 48: #endif
1.4 paf 49:
1.39 paf 50: class Temp_lang;
1.84 paf 51: class Methoded;
1.116 paf 52: class VMethodFrame;
1.1 paf 53:
1.61 paf 54: /// Main workhorse.
1.7 paf 55: class Request : public Pooled {
1.107 paf 56: friend class Temp_lang;
1.117 paf 57: friend class Temp_connection;
1.1 paf 58: public:
1.112 paf 59:
60: #ifdef RESOURCES_DEBUG
61: /// measures
62: double sql_connect_time;
63: double sql_request_time;
64: #endif
1.61 paf 65:
66: /// some information from web server
1.65 paf 67: class Info {
68: public:
1.53 paf 69: const char *document_root;
70: const char *path_translated;
1.56 paf 71: const char *method;
1.53 paf 72: const char *query_string;
1.56 paf 73: const char *uri;
1.53 paf 74: const char *content_type;
75: size_t content_length;
1.60 paf 76: const char *cookie;
1.71 paf 77: const char *user_agent;
1.53 paf 78: };
79:
1.50 paf 80: Request(Pool& apool,
1.53 paf 81: Info& ainfo,
1.111 paf 82: uchar adefault_lang, ///< all tainted data default untainting lang
1.110 paf 83: bool status_allowed ///< status class allowed
1.50 paf 84: );
1.118 paf 85: ~Request();
1.1 paf 86:
1.61 paf 87: /// global classes
1.6 paf 88: Hash& classes() { return fclasses; }
89:
1.65 paf 90: /**
91: core request processing
92:
93: BEWARE: may throw exception to you: catch it!
94: */
95: void core(
1.95 parser 96: const char *root_config_filespec, ///< system config filespec
97: bool root_config_fail_on_read_problem, ///< fail if system config file not found
98: const char *site_config_filespec, ///< site config filespec
99: bool site_config_fail_on_read_problem, ///< fail if site config file not found
1.65 paf 100: bool header_only);
1.17 paf 101:
1.61 paf 102: /// executes ops
1.89 parser 103: void execute(const Array& ops); // execute.C
1.127 ! paf 104: /// execute ops with anti-recoursion check
! 105: void recoursion_checked_execute(const String *name, const Array& ops);
1.40 paf 106:
1.64 paf 107: /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.46 paf 108: VStateless_class *use_file(
1.94 parser 109: const String& file_name,
110: bool ignore_class_path=false, bool fail_on_read_problem=true,
1.45 paf 111: const String *name=0,
1.46 paf 112: VStateless_class *base_class=0); // core.C
1.64 paf 113: /// compiles a @a source buffer
1.46 paf 114: VStateless_class *use_buf(
1.40 paf 115: const char *source, const char *file,
1.46 paf 116: VStateless_class *aclass=0, const String *name=0,
117: VStateless_class *base_class=0); // core.C
1.28 paf 118:
1.126 paf 119: //@{ convinient inline helpers @see Request::process
120: const String& process_to_string(Value& input_value,
121: const String *result_name=0) {
122: const String *result;
123: process_internal(input_value, result_name, true/*intercept_string*/, &result, 0);
124: return *result;
125: }
126: Value& process_to_value(Value& input_value,
127: const String *result_name=0,
128: bool intercept_string=true) {
129: Value *result;
130: process_internal(input_value, result_name, intercept_string, 0, &result);
131: return *result;
132: }
133: //@}
134:
1.61 paf 135: /// appending, sure of clean string inside
1.65 paf 136: void write_no_lang(const String& astring) {
1.111 paf 137: wcontext->write(astring,
138: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.49 paf 139: }
1.61 paf 140: /// appending string, passing language built into string being written
1.65 paf 141: void write_pass_lang(const String& astring) {
142: wcontext->write(astring, String::UL_PASS_APPENDED);
1.59 paf 143: }
1.61 paf 144: /// appending possible string, assigning untaint language
1.40 paf 145: void write_assign_lang(Value& avalue) {
1.39 paf 146: wcontext->write(avalue, flang);
1.106 parser 147: }
148: /// appending string, assigning untaint language
149: void write_assign_lang(const String& astring) {
150: wcontext->write(astring, flang);
1.22 paf 151: }
1.61 paf 152: /// appending possible string, passing language built into string being written
1.40 paf 153: void write_pass_lang(Value& avalue) {
1.65 paf 154: wcontext->write(avalue, String::UL_PASS_APPENDED);
1.49 paf 155: }
1.61 paf 156: /// appending sure value, that would be converted to clean string
1.49 paf 157: void write_no_lang(Value& avalue) {
1.122 paf 158: if(wcontext->get_in_expression())
159: wcontext->write(avalue);
160: else
161: wcontext->write(avalue,
162: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.55 paf 163: }
1.61 paf 164: /// appending sure value, not VString
1.55 paf 165: void write_expr_result(Value& avalue) {
166: wcontext->write(avalue);
1.40 paf 167: }
1.22 paf 168:
1.64 paf 169: /// returns relative to @a path path to @a file
1.69 paf 170: const String& relative(const char *apath, const String& relative_name);
1.61 paf 171:
1.64 paf 172: /// returns an absolute @a path to relative @a name
1.69 paf 173: const String& absolute(const String& relative_name);
1.42 paf 174:
1.80 paf 175: /// returns the mime type of 'user_file_name_cstr'
176: const String& mime_type_of(const char *user_file_name_cstr);
177:
1.117 paf 178: /// returns current SQL connection if any
179: SQL_Connection *connection(const String *source) {
180: if(!fconnection && source)
1.125 paf 181: throw Exception("parser.runtime",
1.117 paf 182: source,
183: "outside of 'connect' operator");
184:
185: return fconnection;
186: }
187:
1.17 paf 188: public:
1.22 paf 189:
1.61 paf 190: /// info from web server
1.53 paf 191: Info& info;
1.81 paf 192: /// user's post data
193: char *post_data; size_t post_size;
1.53 paf 194:
1.78 paf 195: /// operators are methods of this class
1.84 paf 196: Methoded& OP;
1.86 paf 197: /// $env:fields
1.57 paf 198: VEnv env;
1.108 paf 199: /// $status:fields
200: VStatus status;
1.86 paf 201: /// $form:elements
1.57 paf 202: VForm form;
1.90 parser 203: /// $math:constants
204: VMath math;
1.86 paf 205: /// $request:elements
1.57 paf 206: VRequest request;
1.86 paf 207: /// $response:elements
1.57 paf 208: VResponse response;
1.86 paf 209: /// $cookie:elements
1.60 paf 210: VCookie cookie;
1.70 paf 211:
1.123 paf 212: //@{ request processing status
213: /// execution stack
214: Stack stack;
1.61 paf 215: /// contexts
1.22 paf 216: Value *self, *root, *rcontext;
1.61 paf 217: /// contexts
1.22 paf 218: WContext *wcontext;
1.124 paf 219: /// exception stack trace
220: Stack exception_trace;
1.123 paf 221: //@}
1.85 paf 222:
1.86 paf 223: /// 'MAIN' class conglomerat
1.85 paf 224: VStateless_class *main_class;
1.76 paf 225:
1.87 paf 226: /// classes configured data
227: Hash classes_conf;
228:
1.7 paf 229: private: // core data
1.6 paf 230:
1.89 parser 231: /// classes
1.6 paf 232: Hash fclasses;
1.67 paf 233:
1.89 parser 234: /// already used files to avoid cyclic uses
1.67 paf 235: Hash used_files;
1.89 parser 236:
237: /** endless execute(execute(... preventing counter
238: @see ANTI_ENDLESS_EXECUTE_RECOURSION
239: */
240: uint anti_endless_execute_recoursion;
1.99 parser 241:
1.7 paf 242: private: // compile.C
243:
1.46 paf 244: VStateless_class& real_compile(COMPILE_PARAMS);
1.7 paf 245:
246: private: // execute.C
247:
1.116 paf 248: const String *execute_method(Value& aself, const Method& method,
249: bool return_cstr);
250: const String& execute_method(VMethodFrame& amethodFrame, const Method& method);
251: const String *execute_virtual_method(Value& aself, const String& method_name);
1.93 parser 252: const String *execute_nonvirtual_method(VStateless_class& aclass,
1.116 paf 253: const String& method_name,
254: bool return_cstr);
1.9 paf 255:
1.119 paf 256: Value *get_element(bool can_call_operator);
1.22 paf 257:
1.58 paf 258: private: // defaults
259:
1.111 paf 260: const uchar fdefault_lang;
1.68 paf 261: Value *default_content_type;
1.80 paf 262:
263: private: // mime types
264:
265: /// $MAIN:MIME-TYPES
266: Table *mime_types;
1.22 paf 267:
1.39 paf 268: private: // lang manipulation
1.22 paf 269:
1.111 paf 270: uchar set_lang(uchar alang) {
271: uchar result=flang;
1.39 paf 272: flang=alang;
273: return result;
274: }
1.111 paf 275: void restore_lang(uchar alang) {
1.39 paf 276: flang=alang;
277: }
278:
1.117 paf 279: private: // lang&raw
280:
281: uchar flang;
282:
283:
284: private: // connection manipulation
285:
286: SQL_Connection *set_connection(SQL_Connection *aconnection) {
287: SQL_Connection *result=fconnection;
288: fconnection=aconnection;
289: return result;
290: }
291: void restore_connection(SQL_Connection *aconnection) {
292: fconnection=aconnection;
293: }
294:
295: private:
296:
297: /// connection
298: SQL_Connection *fconnection;
299:
300:
1.59 paf 301: private:
302:
1.126 paf 303: /// processes any code-junction there may be inside of @a value
304: void process_internal(
305: Value& input_value, const String *result_name,
306: bool intercept_string,
307: const String **string_result, Value **value_result); // execute.C
308:
1.75 paf 309: void output_result(const VFile& body_file, bool header_only);
1.39 paf 310: };
311:
1.61 paf 312: /// Auto-object used for temporary changing Request::flang.
1.39 paf 313: class Temp_lang {
314: Request& frequest;
1.111 paf 315: uchar saved_lang;
1.39 paf 316: public:
1.111 paf 317: Temp_lang(Request& arequest, uchar alang) :
1.39 paf 318: frequest(arequest),
319: saved_lang(arequest.set_lang(alang)) {
320: }
321: ~Temp_lang() {
322: frequest.restore_lang(saved_lang);
1.117 paf 323: }
324: };
325:
326: /// Auto-object used for temporary changing Request::fconnection.
327: class Temp_connection {
328: Request& frequest;
329: SQL_Connection *saved_connection;
330: public:
331: Temp_connection(Request& arequest, SQL_Connection *aconnection) :
332: frequest(arequest),
333: saved_connection(arequest.set_connection(aconnection)) {
334: }
335: ~Temp_connection() {
336: frequest.restore_connection(saved_connection);
1.39 paf 337: }
1.91 parser 338: };
339:
340: /**
341: @b method parameters passed in this array.
342: contains handy typecast ad junction/not junction ensurers
343:
344: */
345: class MethodParams : public Array {
346: public:
347: MethodParams(Pool& pool, const String& amethod_name) : Array(pool),
348: fmethod_name(amethod_name) {
349: }
350:
351: /// handy typecast. I long for templates
352: Value& get(int index) {
353: return *static_cast<Value *>(Array::get(index));
354: }
355: /// handy is-value-a-junction ensurer
356: Value& as_junction(int index, const char *msg) {
357: return get_as(index, true, msg);
358: }
359: /// handy value-is-not-a-junction ensurer
360: Value& as_no_junction(int index, const char *msg) {
361: return get_as(index, false, msg);
362: }
363: /// handy expression auto-processing to double
1.102 parser 364: double as_double(int index, const char *msg, Request& r) {
365: return get_processed(index, msg, r).as_double();
1.91 parser 366: }
367: /// handy expression auto-processing to int
1.102 parser 368: int as_int(int index, const char *msg, Request& r) {
369: return get_processed(index, msg, r).as_int();
1.103 parser 370: }
371: /// handy expression auto-processing to bool
372: bool as_bool(int index, const char *msg, Request& r) {
373: return get_processed(index, msg, r).as_bool();
1.91 parser 374: }
375: /// handy string ensurer
376: const String& as_string(int index, const char *msg) {
377: return as_no_junction(index, msg).as_string();
378: }
379:
380: private:
381:
382: /// handy value-is/not-a-junction ensurer
383: Value& get_as(int index, bool as_junction, const char *msg) {
384: Value& result=get(index);
385: if((result.get_junction()!=0) ^ as_junction)
1.125 paf 386: throw Exception("parser.runtime",
1.91 parser 387: &fmethod_name,
388: "%s (parameter #%d)", msg, 1+index);
1.104 parser 389:
1.91 parser 390: return result;
391: }
392:
1.102 parser 393: Value& get_processed(int index, const char *msg, Request& r) {
1.126 paf 394: return r.process_to_value(as_junction(index, msg), 0/*no name*/);
1.91 parser 395: }
396:
397: private:
398:
399: const String& fmethod_name;
400:
1.4 paf 401: };
1.1 paf 402:
403: #endif
E-mail: