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