Annotation of parser3/src/include/pa_request.h, revision 1.245
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.245 ! moko 11: #define IDENT_PA_REQUEST_H "$Id: pa_request.h,v 1.244 2016/12/29 15:24:49 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.242 moko 261: const String* get_method_filespec(const Method* method); // execute.C
262: const String* get_used_filespec(uint file_no);
1.126 paf 263:
1.238 moko 264: /// appending string with it's languages
1.239 moko 265: inline void write(const String& astring) {
1.236 moko 266: wcontext->write(astring);
1.49 paf 267: }
1.238 moko 268:
269: /// in [] and {} appending string if get_string is not null, else appending value
1.240 moko 270: /// in () appending string if is_string, else appending value
1.239 moko 271: inline void write(Value& avalue) {
1.236 moko 272: wcontext->write_as_string(avalue);
1.131 paf 273: }
274:
1.238 moko 275: /// allways appending value
1.239 moko 276: inline void write_value(Value& avalue) {
1.226 moko 277: wcontext->write(avalue);
278: }
279:
1.64 paf 280: /// returns relative to @a path path to @a file
1.162 paf 281: const String& relative(const char* apath, const String& relative_name);
1.61 paf 282:
1.64 paf 283: /// returns an absolute @a path to relative @a name
1.69 paf 284: const String& absolute(const String& relative_name);
1.42 paf 285:
1.209 misha 286: /// returns the mime type of 'user_file_name'
287: const String& mime_type_of(const String* file_name);
288:
1.80 paf 289: /// returns the mime type of 'user_file_name_cstr'
1.162 paf 290: const String& mime_type_of(const char* user_file_name_cstr);
1.80 paf 291:
1.117 paf 292: /// returns current SQL connection if any
1.228 moko 293: SQL_Connection* connection(bool fail_on_error=true) {
1.162 paf 294: if(fail_on_error && !fconnection)
1.228 moko 295: throw Exception(PARSER_RUNTIME, 0, "outside of 'connect' operator");
296: return fconnection;
1.133 paf 297: }
298:
1.245 ! moko 299: Skip get_skip() { return fskip; }
1.181 paf 300: void set_skip(Skip askip) { fskip=askip; }
1.245 ! moko 301: void set_skip_return() { fskip=SKIP_RETURN; freturn_method_frame=method_frame; }
! 302: inline bool check_skip_break() { bool result=fskip >= SKIP_BREAK; if(fskip <= SKIP_BREAK) fskip=SKIP_NOTHING; return result; }
! 303: inline void check_skip_return() { if(fskip==SKIP_RETURN && method_frame==freturn_method_frame) fskip=SKIP_NOTHING; }
1.181 paf 304:
1.196 misha 305: void set_in_cycle(int adelta) { fin_cycle+=adelta; }
306: bool get_in_cycle() { return fin_cycle>0; }
307:
1.17 paf 308: public:
1.22 paf 309:
1.61 paf 310: /// info from web server
1.162 paf 311: Request_info& request_info;
312:
313: /// info about ServerAPI
314: SAPI_Info& sapi_info;
315:
316: /// source, client, mail charsets
317: Request_charsets charsets;
1.53 paf 318:
1.154 paf 319: /// 'MAIN' class conglomerat & operators are methods of this class
1.162 paf 320: VStateless_class& main_class;
1.86 paf 321: /// $form:elements
1.162 paf 322: VForm& form;
1.140 paf 323: /// $mail
1.162 paf 324: VMail& mail;
1.86 paf 325: /// $response:elements
1.162 paf 326: VResponse& response;
1.86 paf 327: /// $cookie:elements
1.162 paf 328: VCookie& cookie;
1.183 misha 329: /// $console
330: VConsole& console;
1.70 paf 331:
1.148 paf 332: /// classes configured data
1.193 misha 333: HashString<void*> classes_conf;
1.85 paf 334:
1.148 paf 335: public: // status read methods
1.76 paf 336:
1.148 paf 337: VMethodFrame *get_method_frame() { return method_frame; }
1.162 paf 338: Value& get_self();
1.227 moko 339:
1.162 paf 340: #define GET_SELF(request, type) (static_cast<type &>(request.get_self()))
1.6 paf 341:
1.205 moko 342: /// public for ^reflection:copy[]
343: void put_element(Value& ncontext, const String& name, Value* value);
344:
1.173 paf 345: /// for @main[]
346: const String* execute_virtual_method(Value& aself, const String& method_name);
347:
348: //{ for @conf[filespec] and @auto[filespec] and parser://method/call
1.228 moko 349: const String* execute_method(Value& aself, const Method& method, Value* optional_param, bool do_return_string);
350:
1.173 paf 351: struct Execute_nonvirtual_method_result {
352: const String* string;
353: Method* method;
354: Execute_nonvirtual_method_result(): string(0), method(0) {}
355: };
1.228 moko 356:
357: Execute_nonvirtual_method_result execute_nonvirtual_method(VStateless_class& aclass, const String& method_name, VString* optional_param, bool do_return_string);
1.173 paf 358: //}
359:
1.162 paf 360: #ifdef XML
361: public: // charset helpers
1.67 paf 362:
1.162 paf 363: /// @see Charset::transcode
1.180 paf 364: xmlChar* transcode(const String& s);
1.162 paf 365: /// @see Charset::transcode
1.180 paf 366: xmlChar* transcode(const String::Body s);
1.162 paf 367: /// @see Charset::transcode
1.180 paf 368: const String& transcode(const xmlChar* s);
1.89 parser 369:
1.162 paf 370: #endif
1.136 paf 371:
372: private:
373:
374: /// already executed some @conf method
375: bool configure_admin_done;
376:
1.162 paf 377: void configure_admin(VStateless_class& conf_class);
1.177 paf 378:
379: void configure();
1.99 parser 380:
1.7 paf 381: private: // compile.C
382:
1.228 moko 383: ArrayClass& compile(VStateless_class* aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset);
1.7 paf 384:
385: private: // execute.C
1.9 paf 386:
1.191 misha 387: Value& get_element(Value& ncontext, const String& name);
1.219 moko 388: #ifdef FEATURE_GET_ELEMENT4CALL
389: Value& get_element4call(Value& ncontext, const String& name);
390: #endif
1.22 paf 391:
1.58 paf 392: private: // defaults
393:
1.162 paf 394: const String::Language fdefault_lang;
1.80 paf 395:
396: private: // mime types
397:
398: /// $MAIN:MIME-TYPES
399: Table *mime_types;
1.22 paf 400:
1.39 paf 401: private: // lang manipulation
1.22 paf 402:
1.162 paf 403: String::Language set_lang(String::Language alang) {
404: String::Language result=flang;
1.39 paf 405: flang=alang;
406: return result;
407: }
1.162 paf 408: void restore_lang(String::Language alang) {
1.39 paf 409: flang=alang;
410: }
411:
1.117 paf 412: private: // connection manipulation
413:
1.162 paf 414: SQL_Connection* set_connection(SQL_Connection* aconnection) {
415: SQL_Connection* result=fconnection;
1.117 paf 416: fconnection=aconnection;
417: return result;
418: }
1.162 paf 419: void restore_connection(SQL_Connection* aconnection) {
1.117 paf 420: fconnection=aconnection;
421: }
422:
423: private:
424:
1.162 paf 425: void output_result(VFile* body_file, bool header_only, bool as_attachment);
1.148 paf 426: };
427:
428: /// Auto-object used to save request context across ^try body
429: class Request_context_saver {
430: Request& fr;
431:
432: /// exception stack trace
1.165 paf 433: size_t exception_trace_top;
434: size_t exception_trace_bottom;
1.148 paf 435: /// execution stack
1.162 paf 436: size_t stack;
1.166 paf 437: uint anti_endless_execute_recoursion;
1.148 paf 438: /// contexts
1.162 paf 439: VMethodFrame* method_frame;
440: Value* rcontext;
441: WContext* wcontext;
1.148 paf 442: /// current language
1.162 paf 443: String::Language flang;
1.148 paf 444: /// current connection
1.162 paf 445: SQL_Connection* fconnection;
1.126 paf 446:
1.148 paf 447: public:
1.228 moko 448: Request_context_saver(Request& ar) :
1.171 paf 449: fr(ar),
1.228 moko 450: exception_trace_top(ar.exception_trace.top_index()),
451: exception_trace_bottom(ar.exception_trace.bottom_index()),
1.165 paf 452: stack(ar.stack.top_index()),
1.166 paf 453: anti_endless_execute_recoursion(ar.anti_endless_execute_recoursion),
1.148 paf 454: method_frame(ar.method_frame),
455: rcontext(ar.rcontext),
456: wcontext(ar.wcontext),
457: flang(ar.flang),
1.171 paf 458: fconnection(ar.fconnection) {}
1.153 paf 459: void restore() {
1.165 paf 460: fr.exception_trace.set_top_index(exception_trace_top);
461: fr.exception_trace.set_bottom_index(exception_trace_bottom);
462: fr.stack.set_top_index(stack);
1.166 paf 463: fr.anti_endless_execute_recoursion=anti_endless_execute_recoursion;
1.157 paf 464: fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148 paf 465: fr.flang=flang;
466: fr.fconnection=fconnection;
467: }
1.39 paf 468: };
469:
1.117 paf 470: /// Auto-object used for temporary changing Request::fconnection.
471: class Temp_connection {
472: Request& frequest;
1.162 paf 473: SQL_Connection* saved_connection;
1.117 paf 474: public:
1.228 moko 475: Temp_connection(Request& arequest, SQL_Connection* aconnection) :
1.117 paf 476: frequest(arequest),
477: saved_connection(arequest.set_connection(aconnection)) {
478: }
1.228 moko 479: ~Temp_connection() {
1.117 paf 480: frequest.restore_connection(saved_connection);
1.39 paf 481: }
1.91 parser 482: };
483:
1.196 misha 484: /// Auto-object used for break out of cycle check
485: class InCycle {
486: Request& frequest;
487: public:
488: InCycle(Request& arequest) : frequest(arequest) {
489: frequest.set_in_cycle(1);
490: }
1.228 moko 491: ~InCycle() {
1.196 misha 492: frequest.set_in_cycle(-1);
493: }
494: };
1.91 parser 495:
1.245 ! moko 496: /// Auto-object used for break out of cycle check
! 497: class TempSkip4Delimiter {
! 498: Request& frequest;
! 499: Request::Skip fskip;
! 500: public:
! 501: TempSkip4Delimiter(Request& arequest) : frequest(arequest), fskip(arequest.get_skip()) {
! 502: frequest.set_skip(Request::SKIP_NOTHING);
! 503: }
! 504: // returns true if break required, should be called
! 505: bool check_break() {
! 506: if(frequest.get_skip())
! 507: fskip=frequest.get_skip();
! 508: frequest.set_skip(fskip <= Request::SKIP_BREAK ? Request::SKIP_NOTHING : fskip);
! 509: return fskip >= Request::SKIP_BREAK;
! 510: }
! 511: };
! 512:
1.213 moko 513: /// Auto-object used for temporary changing Request::allow_class_replace.
514: class Temp_class_replace {
515: Request& frequest;
516: public:
517: Temp_class_replace(Request& arequest, bool avalue) : frequest(arequest){
518: frequest.allow_class_replace=avalue;
519: }
520: ~Temp_class_replace() {
521: frequest.allow_class_replace=false;
522: }
523: };
524:
1.214 moko 525: /// Auto-object used for temporarily substituting/removing elements
526: class Temp_value_element {
527: Request& frequest;
528: Value& fwhere;
529: const String& fname;
1.216 moko 530: Value* saved;
1.214 moko 531: public:
1.216 moko 532: Temp_value_element(Request& arequest, Value& awhere, const String& aname, Value* awhat);
533: ~Temp_value_element();
1.214 moko 534: };
535:
1.162 paf 536: // defines for externs
1.91 parser 537:
1.170 paf 538: #define EXCEPTION_HANDLED_PART_NAME "handled"
539:
1.162 paf 540:
541: // externs
542:
543: extern const String main_method_name;
544: extern const String auto_method_name;
1.186 misha 545:
1.162 paf 546: extern const String exception_type_part_name;
547: extern const String exception_source_part_name;
548: extern const String exception_comment_part_name;
549: extern const String exception_handled_part_name;
1.91 parser 550:
1.162 paf 551: // defines for statics
1.91 parser 552:
1.162 paf 553: #define MAIN_CLASS_NAME "MAIN"
554: #define AUTO_FILE_NAME "auto.p"
1.1 paf 555:
556: #endif
E-mail: