Annotation of parser3/src/include/pa_request.h, revision 1.160.2.25
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.25! paf 11: static const char* IDENT_REQUEST_H="$Date: 2003/02/17 11:29:53 $";
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
1.160.2.21 paf 70: Pool& fpool;
1.160.2.6 paf 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:
1.160.2.24 paf 91: StringPtr string() { return value->get_string(0); }
1.160.2.12 paf 92: ValuePtr value;
93: ArrayOperationPtr ops;
1.160.2.24 paf 94: void *ptr;
1.160.2.12 paf 95:
96: /// needed to fill unused Array entries
97: StackItem() {}
98: StackItem(ValuePtr avalue): value(avalue) {}
99: StackItem(ArrayOperationPtr aops): ops(aops) {}
1.160.2.24 paf 100: StackItem(void *aptr): ptr(aptr) {}
1.160.2.12 paf 101: };
102:
1.160.2.6 paf 103: //@{ request processing status
104: /// exception stack trace
1.160.2.8 paf 105: Stack<StringPtr> exception_trace;
1.160.2.6 paf 106: /// execution stack
1.160.2.12 paf 107: Stack<StackItem> stack;
1.160.2.6 paf 108: /// contexts
109: VMethodFrame *method_frame;
110: Value *rcontext;
111: WContext *wcontext;
112: /// current language
113: uchar flang;
114: /// current connection
115: SQL_Connection *fconnection;
116: //@}
117: /// interrupted flag, raised on signals [SIGPIPE]
118: bool finterrupted;
119:
1.1 paf 120: public:
1.112 paf 121:
1.160.2.6 paf 122: /// all files and classes and other hard-to-place allocations go to this pool
1.160.2.1 paf 123: Pool& pool() { return fpool; }
124:
1.112 paf 125: #ifdef RESOURCES_DEBUG
126: /// measures
127: double sql_connect_time;
128: double sql_request_time;
129: #endif
1.61 paf 130:
1.160.2.22 paf 131: Request(Pool& apool, SAPI_Info& asapi_info, Request_info& arequest_info,
1.111 paf 132: uchar adefault_lang, ///< all tainted data default untainting lang
1.110 paf 133: bool status_allowed ///< status class allowed
1.50 paf 134: );
1.118 paf 135: ~Request();
1.1 paf 136:
1.61 paf 137: /// global classes
1.160.2.8 paf 138: HashStringValue& classes() { return fclasses; }
1.6 paf 139:
1.65 paf 140: /**
141: core request processing
142:
143: BEWARE: may throw exception to you: catch it!
144: */
145: void core(
1.160.2.9 paf 146: const char* config_filespec, ///< system config filespec
1.138 paf 147: bool config_fail_on_read_problem, ///< fail if system config file not found
1.65 paf 148: bool header_only);
1.17 paf 149:
1.61 paf 150: /// executes ops
1.160.2.12 paf 151: void execute(ArrayOperation& ops); // execute.C
1.127 paf 152: /// execute ops with anti-recoursion check
1.160.2.12 paf 153: void recoursion_checked_execute(StringPtr name, ArrayOperation& ops) {
1.128 paf 154: // anti_endless_execute_recoursion
155: if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
156: anti_endless_execute_recoursion=0; // give @exception a chance
157: throw Exception("parser.runtime",
158: name,
159: "call canceled - endless recursion detected");
160: }
1.147 paf 161: execute(ops); // execute it
1.128 paf 162: anti_endless_execute_recoursion--;
163: }
1.40 paf 164:
1.64 paf 165: /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.160.2.10 paf 166: VStateless_classPtr use_file(VStateless_class& aclass,
1.160.2.8 paf 167: StringPtr file_name,
1.149 paf 168: bool ignore_class_path=false,
1.160.2.8 paf 169: bool fail_on_read_problem=true, bool fail_on_file_absence=true); // pa_request.C
1.64 paf 170: /// compiles a @a source buffer
1.160.2.10 paf 171: VStateless_classPtr use_buf(VStateless_class& aclass,
1.160.2.9 paf 172: const char* source,
173: StringPtr filespec, const char* filespec_cstr); // pa_request.C
1.28 paf 174:
1.129 paf 175: /// processes any code-junction there may be inside of @a value
1.160.2.6 paf 176: StringOrValue process(ValuePtr input_value, bool intercept_string=true); // execute.C
1.129 paf 177: //@{ convinient helpers
1.160.2.8 paf 178: StringPtr process_to_string(ValuePtr input_value) {
1.160.2.6 paf 179: return process(input_value, true/*intercept_string*/).as_string(&pool());
1.129 paf 180: }
1.160.2.6 paf 181: ValuePtr process_to_value(ValuePtr input_value, bool intercept_string=true) {
1.129 paf 182: return process(input_value, intercept_string).as_value();
1.126 paf 183: }
184: //@}
1.131 paf 185:
1.126 paf 186:
1.131 paf 187: #define DEFINE_DUAL(modification) \
188: void write_##modification##_lang(StringOrValue dual) { \
1.160.2.8 paf 189: if(StringPtr string=dual.get_string()) \
1.131 paf 190: write_##modification##_lang(*string); \
191: else \
1.160.2.6 paf 192: write_##modification##_lang(dual.get_value()); \
1.131 paf 193: }
1.132 paf 194: #define DEFINE_DUAL_CHECKED(modification) \
1.160.2.8 paf 195: void write_##modification##_lang(StringOrValue dual, StringPtr origin) { \
196: if(StringPtr string=dual.get_string()) \
1.132 paf 197: write_##modification##_lang(*string); \
198: else \
1.160.2.6 paf 199: write_##modification##_lang(dual.get_value(), origin); \
1.132 paf 200: }
1.131 paf 201:
1.61 paf 202: /// appending, sure of clean string inside
1.65 paf 203: void write_no_lang(const String& astring) {
1.111 paf 204: wcontext->write(astring,
205: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.49 paf 206: }
1.131 paf 207: /// appending sure value, that would be converted to clean string
1.160.2.6 paf 208: void write_no_lang(ValuePtr avalue) {
1.131 paf 209: if(wcontext->get_in_expression())
1.160.2.10 paf 210: wcontext->write(pool(), avalue);
1.131 paf 211: else
1.160.2.6 paf 212: wcontext->write(pool(), avalue,
1.131 paf 213: String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
214: }
215:
1.61 paf 216: /// appending string, passing language built into string being written
1.65 paf 217: void write_pass_lang(const String& astring) {
218: wcontext->write(astring, String::UL_PASS_APPENDED);
1.59 paf 219: }
1.131 paf 220: /// appending possible string, passing language built into string being written
1.160.2.6 paf 221: void write_pass_lang(ValuePtr avalue) {
222: wcontext->write(pool(), avalue, String::UL_PASS_APPENDED);
1.131 paf 223: }
224: DEFINE_DUAL(pass)
225:
1.61 paf 226: /// appending possible string, assigning untaint language
1.160.2.6 paf 227: void write_assign_lang(ValuePtr avalue) {
228: wcontext->write(pool(), avalue, flang);
1.106 parser 229: }
1.132 paf 230: /// appending possible string, assigning untaint language
1.160.2.8 paf 231: void write_assign_lang(ValuePtr avalue, StringPtr origin) {
1.160.2.6 paf 232: wcontext->write(pool(), avalue, flang, origin);
1.132 paf 233: }
1.106 parser 234: /// appending string, assigning untaint language
235: void write_assign_lang(const String& astring) {
236: wcontext->write(astring, flang);
1.22 paf 237: }
1.131 paf 238: DEFINE_DUAL(assign)
1.132 paf 239: DEFINE_DUAL_CHECKED(assign)
1.22 paf 240:
1.64 paf 241: /// returns relative to @a path path to @a file
1.160.2.10 paf 242: StringPtr relative(const char* apath, StringPtr relative_name);
1.61 paf 243:
1.64 paf 244: /// returns an absolute @a path to relative @a name
1.160.2.10 paf 245: StringPtr absolute(StringPtr relative_name);
1.42 paf 246:
1.80 paf 247: /// returns the mime type of 'user_file_name_cstr'
1.160.2.10 paf 248: StringPtr mime_type_of(const char* user_file_name_cstr);
1.80 paf 249:
1.117 paf 250: /// returns current SQL connection if any
1.160.2.8 paf 251: SQL_Connection *connection(StringPtr source) {
1.117 paf 252: if(!fconnection && source)
1.125 paf 253: throw Exception("parser.runtime",
1.117 paf 254: source,
255: "outside of 'connect' operator");
256:
257: return fconnection;
1.133 paf 258: }
259:
1.154 paf 260: bool origins_mode();
1.117 paf 261:
1.158 paf 262: void interrupt() { finterrupted=true; }
263: bool interrupted() { return finterrupted; }
264:
1.17 paf 265: public:
1.22 paf 266:
1.61 paf 267: /// info from web server
1.160.2.8 paf 268: Request_info& request_info;
269:
270: /// info about ServerAPI
271: SAPI_Info& sapi_info;
1.160.2.2 paf 272:
273: /// source, client, mail charsets
1.160.2.6 paf 274: Request_charsets charsets;
1.53 paf 275:
1.154 paf 276: /// 'MAIN' class conglomerat & operators are methods of this class
1.160.2.8 paf 277: VStateless_classPtr main_class;
1.160.2.10 paf 278: /// $env:fields
279: //VEnv env;
1.108 paf 280: /// $status:fields
1.160.2.10 paf 281: //VStatus status;
1.86 paf 282: /// $form:elements
1.160.2.10 paf 283: VFormPtr form;
1.140 paf 284: /// $mail
1.160.2.10 paf 285: VMailPtr mail;
1.90 parser 286: /// $math:constants
1.160.2.10 paf 287: //VMath math;
1.86 paf 288: /// $request:elements
1.160.2.10 paf 289: //VRequest request;
1.86 paf 290: /// $response:elements
1.160.2.10 paf 291: VResponsePtr response;
1.86 paf 292: /// $cookie:elements
1.160.2.10 paf 293: VCookiePtr cookie;
1.70 paf 294:
1.148 paf 295: /// classes configured data
1.160.2.20 paf 296: HashStringObject classes_conf;
1.85 paf 297:
1.148 paf 298: public: // status read methods
1.76 paf 299:
1.148 paf 300: VMethodFrame *get_method_frame() { return method_frame; }
1.160.2.13 paf 301: ValuePtr get_self();
1.160.2.18 paf 302: #define GET_SELF(request, type) (*static_cast<type *>(request.get_self().get()))
303: /* for strange reason call to this:
304: r.get_self<VHash>()
305: refuses to compile
306:
307: template<typename T> T& get_self() {
308: return *static_cast<T*>(get_self().get());
309: }
310: */
311:
312:
1.87 paf 313:
1.136 paf 314: private:
315:
316: /// already executed some @conf method
317: bool configure_admin_done;
318:
1.160.2.10 paf 319: void configure_admin(VStateless_class& conf_class, StringPtr source);
1.99 parser 320:
1.7 paf 321: private: // compile.C
322:
1.46 paf 323: VStateless_class& real_compile(COMPILE_PARAMS);
1.7 paf 324:
325: private: // execute.C
326:
1.139 paf 327: /// for @postprocess[body]
1.160.2.10 paf 328: StringPtr execute_method(VMethodFrame& amethodFrame, const Method& method);
1.139 paf 329: //{ for @conf[filespec] and @auto[filespec]
1.160.2.13 paf 330: StringPtr execute_method(ValuePtr aself,
1.160.2.10 paf 331: const Method& method, VStringPtr optional_param,
332: bool do_return_string);
1.160.2.25! paf 333: struct Execute_nonvirtual_method_result {
! 334: StringPtr string;
! 335: MethodPtr method;
! 336: };
! 337: Execute_nonvirtual_method_result execute_nonvirtual_method(VStateless_class& aclass,
1.160.2.10 paf 338: StringPtr method_name, VStringPtr optional_param,
1.160.2.25! paf 339: bool do_return_string);
1.139 paf 340: //}
341: /// for @main[]
1.160.2.13 paf 342: StringPtr execute_virtual_method(ValuePtr aself, StringPtr method_name);
1.9 paf 343:
1.160.2.13 paf 344: ValuePtr get_element(StringPtr& remember_name, bool can_call_operator);
1.22 paf 345:
1.58 paf 346: private: // defaults
347:
1.111 paf 348: const uchar fdefault_lang;
1.80 paf 349:
350: private: // mime types
351:
352: /// $MAIN:MIME-TYPES
353: Table *mime_types;
1.22 paf 354:
1.39 paf 355: private: // lang manipulation
1.22 paf 356:
1.111 paf 357: uchar set_lang(uchar alang) {
358: uchar result=flang;
1.39 paf 359: flang=alang;
360: return result;
361: }
1.111 paf 362: void restore_lang(uchar alang) {
1.39 paf 363: flang=alang;
364: }
365:
1.117 paf 366: private: // connection manipulation
367:
368: SQL_Connection *set_connection(SQL_Connection *aconnection) {
369: SQL_Connection *result=fconnection;
370: fconnection=aconnection;
371: return result;
372: }
373: void restore_connection(SQL_Connection *aconnection) {
374: fconnection=aconnection;
375: }
376:
377: private:
378:
1.160.2.11 paf 379: void output_result(VFilePtr body_file, bool header_only, bool as_attachment);
1.148 paf 380: };
381:
382: /// Auto-object used to save request context across ^try body
383: class Request_context_saver {
384: Request& fr;
385:
386: /// exception stack trace
387: int exception_trace;
388: /// execution stack
389: int stack;
390: /// contexts
391: VMethodFrame *method_frame;
392: Value *rcontext;
393: WContext *wcontext;
394: /// current language
395: uchar flang;
396: /// current connection
1.117 paf 397: SQL_Connection *fconnection;
1.126 paf 398:
1.148 paf 399: public:
400: Request_context_saver(Request& ar) :
401: exception_trace(ar.exception_trace.top_index()),
402: stack(ar.stack.top_index()),
403: method_frame(ar.method_frame),
404: rcontext(ar.rcontext),
405: wcontext(ar.wcontext),
406: flang(ar.flang),
407: fconnection(ar.fconnection),
408: fr(ar) {}
1.153 paf 409: void restore() {
1.148 paf 410: fr.exception_trace.top_index(exception_trace);
411: fr.stack.top_index(stack);
1.157 paf 412: fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148 paf 413: fr.flang=flang;
414: fr.fconnection=fconnection;
415: }
1.39 paf 416: };
417:
1.61 paf 418: /// Auto-object used for temporary changing Request::flang.
1.39 paf 419: class Temp_lang {
420: Request& frequest;
1.160.2.15 paf 421: String_UL saved_lang;
1.39 paf 422: public:
1.160.2.15 paf 423: Temp_lang(Request& arequest, String_UL alang) :
1.39 paf 424: frequest(arequest),
425: saved_lang(arequest.set_lang(alang)) {
426: }
427: ~Temp_lang() {
428: frequest.restore_lang(saved_lang);
1.117 paf 429: }
430: };
431:
432: /// Auto-object used for temporary changing Request::fconnection.
433: class Temp_connection {
434: Request& frequest;
435: SQL_Connection *saved_connection;
436: public:
437: Temp_connection(Request& arequest, SQL_Connection *aconnection) :
438: frequest(arequest),
439: saved_connection(arequest.set_connection(aconnection)) {
440: }
441: ~Temp_connection() {
442: frequest.restore_connection(saved_connection);
1.39 paf 443: }
1.4 paf 444: };
1.160.2.7 paf 445:
1.160.2.15 paf 446:
447: // defines for externs
448:
449: #define CONTENT_DISPOSITION_NAME "content-disposition"
450: #define CONTENT_DISPOSITION_VALUE "attachment"
451: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
452:
1.160.2.7 paf 453: // externs
454:
455: extern StringPtr main_method_name;
1.160.2.20 paf 456: extern StringPtr auto_method_name;
1.160.2.16 paf 457: extern StringPtr body_name;
1.160.2.15 paf 458: extern StringPtr content_disposition_name;
459: extern StringPtr content_disposition_value;
460: extern StringPtr content_disposition_filename_name;
1.160.2.19 paf 461:
462: // defines for statics
463:
464: #define MAIN_CLASS_NAME "MAIN"
1.160.2.21 paf 465: #define AUTO_FILE_NAME "auto.p"
1.1 paf 466:
467: #endif
E-mail: