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