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