Annotation of parser3/src/include/pa_request.h, revision 1.160.2.7
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.7! paf 11: static const char* IDENT_REQUEST_H="$Date: 2003/01/29 15:39:27 $";
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.44 paf 31:
1.112 paf 32: #ifdef RESOURCES_DEBUG
33: #include <sys/resource.h>
34: #endif
35:
1.128 paf 36: // consts
37:
38: const uint ANTI_ENDLESS_EXECUTE_RECOURSION=500;
39:
40: // defines
41:
1.7 paf 42: #ifndef NO_STRING_ORIGIN
1.40 paf 43: # define COMPILE_PARAMS \
1.154 paf 44: VStateless_class& aclass, const char *source \
45: , const char *file
46: # define COMPILE(aclass, source, file) \
47: real_compile(aclass, source, file)
1.7 paf 48: #else
1.40 paf 49: # define COMPILE_PARAMS \
1.154 paf 50: VStateless_class& aclass, const char *source
51: # define COMPILE(aclass, source, file) \
52: real_compile(aclass, source)
1.7 paf 53: #endif
1.4 paf 54:
1.39 paf 55: class Temp_lang;
1.84 paf 56: class Methoded;
1.116 paf 57: class VMethodFrame;
1.1 paf 58:
1.61 paf 59: /// Main workhorse.
1.160.2.6 paf 60: class Request: public PA_Object {
1.107 paf 61: friend class Temp_lang;
1.117 paf 62: friend class Temp_connection;
1.148 paf 63: friend class Request_context_saver;
1.155 paf 64: friend class Temp_request_self;
1.160.2.6 paf 65:
66: /// all files and classes and other hard-to-place allocations go to this pool
67: Pool fpool;
68:
1.160.2.7! paf 69: ///@{ core data
! 70:
! 71: typedef Hash<ConstStringPtr, VStateless_classPtr> classes_type;
! 72:
! 73: /// classes
! 74: classes_type fclasses;
! 75:
! 76: /// already used files to avoid cyclic uses
! 77: Hash<ConstStringPtr, bool> used_files;
! 78:
! 79: /** endless execute(execute(... preventing counter
! 80: @see ANTI_ENDLESS_EXECUTE_RECOURSION
! 81: */
! 82: uint anti_endless_execute_recoursion;
! 83:
! 84: ///@}
! 85:
! 86: public:
1.160.2.6 paf 87: //@{ request processing status
88: /// exception stack trace
89: Stack<ConstStringPtr> exception_trace;
90: /// execution stack
91: Stack<void *> stack;
92: /// contexts
93: VMethodFrame *method_frame;
94: Value *rcontext;
95: WContext *wcontext;
96: /// current language
97: uchar flang;
98: /// current connection
99: SQL_Connection *fconnection;
100: //@}
101: /// interrupted flag, raised on signals [SIGPIPE]
102: bool finterrupted;
103:
1.1 paf 104: public:
1.112 paf 105:
1.160.2.6 paf 106: /// all files and classes and other hard-to-place allocations go to this pool
1.160.2.1 paf 107: Pool& pool() { return fpool; }
108:
1.112 paf 109: #ifdef RESOURCES_DEBUG
110: /// measures
111: double sql_connect_time;
112: double sql_request_time;
113: #endif
1.61 paf 114:
1.160.2.1 paf 115: Request(
1.160.2.3 paf 116: Request_info& ainfo,
1.111 paf 117: uchar adefault_lang, ///< all tainted data default untainting lang
1.110 paf 118: bool status_allowed ///< status class allowed
1.50 paf 119: );
1.118 paf 120: ~Request();
1.1 paf 121:
1.61 paf 122: /// global classes
1.160.2.7! paf 123: classes_type& classes() { return fclasses; }
1.6 paf 124:
1.65 paf 125: /**
126: core request processing
127:
128: BEWARE: may throw exception to you: catch it!
129: */
130: void core(
1.138 paf 131: const char *config_filespec, ///< system config filespec
132: bool config_fail_on_read_problem, ///< fail if system config file not found
1.65 paf 133: bool header_only);
1.17 paf 134:
1.61 paf 135: /// executes ops
1.147 paf 136: void execute(const Array& ops); // execute.C
1.127 paf 137: /// execute ops with anti-recoursion check
1.160.2.6 paf 138: void recoursion_checked_execute(ConstStringPtr name, const Array& ops) {
1.128 paf 139: // anti_endless_execute_recoursion
140: if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
141: anti_endless_execute_recoursion=0; // give @exception a chance
142: throw Exception("parser.runtime",
143: name,
144: "call canceled - endless recursion detected");
145: }
1.147 paf 146: execute(ops); // execute it
1.128 paf 147: anti_endless_execute_recoursion--;
148: }
1.40 paf 149:
1.64 paf 150: /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.160.2.7! paf 151: VStateless_classPtr use_file(VStateless_class& aclass,
! 152: ConstStringPtr file_name,
1.149 paf 153: bool ignore_class_path=false,
1.154 paf 154: bool fail_on_read_problem=true, bool fail_on_file_absence=true); // core.C
1.64 paf 155: /// compiles a @a source buffer
1.160.2.7! paf 156: VStateless_classPtr use_buf(VStateless_class& aclass,
1.154 paf 157: const char *source,
158: const String& filespec, const char *filespec_cstr); // core.C
1.28 paf 159:
1.129 paf 160: /// processes any code-junction there may be inside of @a value
1.160.2.6 paf 161: StringOrValue process(ValuePtr input_value, bool intercept_string=true); // execute.C
1.129 paf 162: //@{ convinient helpers
1.160.2.6 paf 163: ConstStringPtr process_to_string(ValuePtr input_value) {
164: return process(input_value, true/*intercept_string*/).as_string(&pool());
1.129 paf 165: }
1.160.2.6 paf 166: ValuePtr process_to_value(ValuePtr input_value, bool intercept_string=true) {
1.129 paf 167: return process(input_value, intercept_string).as_value();
1.126 paf 168: }
169: //@}
1.131 paf 170:
1.126 paf 171:
1.131 paf 172: #define DEFINE_DUAL(modification) \
173: void write_##modification##_lang(StringOrValue dual) { \
1.160.2.6 paf 174: if(ConstStringPtr string=dual.get_string()) \
1.131 paf 175: write_##modification##_lang(*string); \
176: else \
1.160.2.6 paf 177: write_##modification##_lang(dual.get_value()); \
1.131 paf 178: }
1.132 paf 179: #define DEFINE_DUAL_CHECKED(modification) \
1.160.2.6 paf 180: void write_##modification##_lang(StringOrValue dual, ConstStringPtr origin) { \
181: if(ConstStringPtr string=dual.get_string()) \
1.132 paf 182: write_##modification##_lang(*string); \
183: else \
1.160.2.6 paf 184: write_##modification##_lang(dual.get_value(), origin); \
1.132 paf 185: }
1.131 paf 186:
1.61 paf 187: /// appending, sure of clean string inside
1.65 paf 188: void write_no_lang(const String& astring) {
1.111 paf 189: wcontext->write(astring,
190: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.49 paf 191: }
1.131 paf 192: /// appending sure value, that would be converted to clean string
1.160.2.6 paf 193: void write_no_lang(ValuePtr avalue) {
1.131 paf 194: if(wcontext->get_in_expression())
195: wcontext->write(avalue);
196: else
1.160.2.6 paf 197: wcontext->write(pool(), avalue,
1.131 paf 198: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
199: }
200:
1.61 paf 201: /// appending string, passing language built into string being written
1.65 paf 202: void write_pass_lang(const String& astring) {
203: wcontext->write(astring, String::UL_PASS_APPENDED);
1.59 paf 204: }
1.131 paf 205: /// appending possible string, passing language built into string being written
1.160.2.6 paf 206: void write_pass_lang(ValuePtr avalue) {
207: wcontext->write(pool(), avalue, String::UL_PASS_APPENDED);
1.131 paf 208: }
209: DEFINE_DUAL(pass)
210:
1.61 paf 211: /// appending possible string, assigning untaint language
1.160.2.6 paf 212: void write_assign_lang(ValuePtr avalue) {
213: wcontext->write(pool(), avalue, flang);
1.106 parser 214: }
1.132 paf 215: /// appending possible string, assigning untaint language
1.160.2.6 paf 216: void write_assign_lang(ValuePtr avalue, ConstStringPtr origin) {
217: wcontext->write(pool(), avalue, flang, origin);
1.132 paf 218: }
1.106 parser 219: /// appending string, assigning untaint language
220: void write_assign_lang(const String& astring) {
221: wcontext->write(astring, flang);
1.22 paf 222: }
1.131 paf 223: DEFINE_DUAL(assign)
1.132 paf 224: DEFINE_DUAL_CHECKED(assign)
1.22 paf 225:
1.64 paf 226: /// returns relative to @a path path to @a file
1.69 paf 227: const String& relative(const char *apath, const String& relative_name);
1.61 paf 228:
1.64 paf 229: /// returns an absolute @a path to relative @a name
1.69 paf 230: const String& absolute(const String& relative_name);
1.42 paf 231:
1.80 paf 232: /// returns the mime type of 'user_file_name_cstr'
233: const String& mime_type_of(const char *user_file_name_cstr);
234:
1.117 paf 235: /// returns current SQL connection if any
1.160.2.6 paf 236: SQL_Connection *connection(ConstStringPtr source) {
1.117 paf 237: if(!fconnection && source)
1.125 paf 238: throw Exception("parser.runtime",
1.117 paf 239: source,
240: "outside of 'connect' operator");
241:
242: return fconnection;
1.133 paf 243: }
244:
1.154 paf 245: bool origins_mode();
1.117 paf 246:
1.158 paf 247: void interrupt() { finterrupted=true; }
248: bool interrupted() { return finterrupted; }
249:
1.17 paf 250: public:
1.22 paf 251:
1.61 paf 252: /// info from web server
1.160.2.4 paf 253: Request_info& info;
1.160.2.2 paf 254:
255: /// source, client, mail charsets
1.160.2.6 paf 256: Request_charsets charsets;
1.53 paf 257:
1.154 paf 258: /// 'MAIN' class conglomerat & operators are methods of this class
259: VClass& main_class;
1.86 paf 260: /// $env:fields
1.57 paf 261: VEnv env;
1.108 paf 262: /// $status:fields
263: VStatus status;
1.86 paf 264: /// $form:elements
1.57 paf 265: VForm form;
1.140 paf 266: /// $mail
267: VMail mail;
1.90 parser 268: /// $math:constants
269: VMath math;
1.86 paf 270: /// $request:elements
1.57 paf 271: VRequest request;
1.86 paf 272: /// $response:elements
1.57 paf 273: VResponse response;
1.86 paf 274: /// $cookie:elements
1.60 paf 275: VCookie cookie;
1.70 paf 276:
1.148 paf 277: /// classes configured data
1.160.2.6 paf 278: HashStringValue classes_conf;
1.85 paf 279:
1.148 paf 280: public: // status read methods
1.76 paf 281:
1.148 paf 282: VMethodFrame *get_method_frame() { return method_frame; }
1.157 paf 283: Value *get_self();
1.87 paf 284:
1.136 paf 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.154 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.156 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: // connection manipulation
336:
337: SQL_Connection *set_connection(SQL_Connection *aconnection) {
338: SQL_Connection *result=fconnection;
339: fconnection=aconnection;
340: return result;
341: }
342: void restore_connection(SQL_Connection *aconnection) {
343: fconnection=aconnection;
344: }
345:
346: private:
347:
1.159 paf 348: void output_result(const VFile& body_file, bool header_only, bool as_attachment);
1.148 paf 349: };
350:
351: /// Auto-object used to save request context across ^try body
352: class Request_context_saver {
353: Request& fr;
354:
355: /// exception stack trace
356: int exception_trace;
357: /// execution stack
358: int stack;
359: /// contexts
360: VMethodFrame *method_frame;
361: Value *rcontext;
362: WContext *wcontext;
363: /// current language
364: uchar flang;
365: /// current connection
1.117 paf 366: SQL_Connection *fconnection;
1.126 paf 367:
1.148 paf 368: public:
369: Request_context_saver(Request& ar) :
370: exception_trace(ar.exception_trace.top_index()),
371: stack(ar.stack.top_index()),
372: method_frame(ar.method_frame),
373: rcontext(ar.rcontext),
374: wcontext(ar.wcontext),
375: flang(ar.flang),
376: fconnection(ar.fconnection),
377: fr(ar) {}
1.153 paf 378: void restore() {
1.148 paf 379: fr.exception_trace.top_index(exception_trace);
380: fr.stack.top_index(stack);
1.157 paf 381: fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148 paf 382: fr.flang=flang;
383: fr.fconnection=fconnection;
384: }
1.39 paf 385: };
386:
1.61 paf 387: /// Auto-object used for temporary changing Request::flang.
1.39 paf 388: class Temp_lang {
389: Request& frequest;
1.111 paf 390: uchar saved_lang;
1.39 paf 391: public:
1.111 paf 392: Temp_lang(Request& arequest, uchar alang) :
1.39 paf 393: frequest(arequest),
394: saved_lang(arequest.set_lang(alang)) {
395: }
396: ~Temp_lang() {
397: frequest.restore_lang(saved_lang);
1.117 paf 398: }
399: };
400:
401: /// Auto-object used for temporary changing Request::fconnection.
402: class Temp_connection {
403: Request& frequest;
404: SQL_Connection *saved_connection;
405: public:
406: Temp_connection(Request& arequest, SQL_Connection *aconnection) :
407: frequest(arequest),
408: saved_connection(arequest.set_connection(aconnection)) {
409: }
410: ~Temp_connection() {
411: frequest.restore_connection(saved_connection);
1.39 paf 412: }
1.4 paf 413: };
1.160.2.7! paf 414:
! 415: // externs
! 416:
! 417: extern StringPtr main_method_name;
1.1 paf 418:
419: #endif
E-mail: