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