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