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