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