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