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