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