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