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