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