Annotation of parser3/src/include/pa_request.h, revision 1.275
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.275 ! moko 11: #define IDENT_PA_REQUEST_H "$Id: pa_request.h,v 1.274 2026/09/25 12:55:42 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()); }
1.274 moko 121: void set_range(size_t bottom, size_t top) { fbottom=bottom; fsize=top; }
1.162 paf 122:
123: void clear() {
1.264 moko 124: fsize=fbottom=0;
1.162 paf 125: }
126:
127: bool is_empty() {
1.264 moko 128: return fsize==fbottom;
1.162 paf 129: }
1.259 moko 130:
1.275 ! moko 131: Table &table(Request &r, const String* problem_source) {
! 132: return table(r, problem_source, bottom_index(), top_index());
! 133: }
! 134: Table &table(Request &r, const String* problem_source, size_t bottom, size_t top);
1.162 paf 135: };
136:
137: ///@{ core data
138:
139: /// classes
1.270 moko 140: OrderedHashString<VStateless_class*> fclasses;
1.162 paf 141:
142: /// already used files to avoid cyclic uses
1.193 misha 143: HashString<bool> used_files;
1.200 misha 144: HashString<bool> searched_along_class_path;
1.162 paf 145: /// list of all used files, Operation::file_no = index to it
1.163 paf 146: Array<String::Body> file_list;
1.162 paf 147:
1.229 moko 148: /// endless execute(execute(... preventing counter
1.267 moko 149: int anti_endless_execute_recursion;
1.162 paf 150:
151: ///@}
152:
153: /// execution stack
154: Stack<StackItem> stack;
155:
1.274 moko 156: public:
1.162 paf 157: /// exception stack trace
158: Exception_trace exception_trace;
159:
1.213 moko 160: bool allow_class_replace;
161:
1.162 paf 162: //@{ request processing status
163: /// contexts
164: VMethodFrame* method_frame;
165: Value* rcontext;
166: WContext* wcontext;
167: /// current language
1.228 moko 168: String::Language flang;
1.162 paf 169: /// current connection
170: SQL_Connection* fconnection;
171: //@}
1.245 moko 172:
173: private:
174:
175: int fin_cycle;
1.181 paf 176: Skip fskip;
1.245 moko 177: VMethodFrame* freturn_method_frame;
1.162 paf 178:
179: public:
1.175 paf 180: uint register_file(String::Body file_spec);
1.162 paf 181:
1.273 moko 182: const char* get_exception_cstr(const Exception& e, VException& details);
1.162 paf 183:
184: /// @see Stack::wipe_unused
185: void wipe_unused_execution_stack() {
186: stack.wipe_unused();
187: }
1.112 paf 188:
189: #ifdef RESOURCES_DEBUG
190: /// measures
191: double sql_connect_time;
192: double sql_request_time;
193: #endif
1.61 paf 194:
1.162 paf 195: Request(SAPI_Info& asapi_info, Request_info& arequest_info,
1.208 moko 196: String::Language adefault_lang ///< all tainted data default untainting lang
1.50 paf 197: );
1.118 paf 198: ~Request();
1.1 paf 199:
1.61 paf 200: /// global classes
1.270 moko 201: OrderedHashString<VStateless_class*>& classes() { return fclasses; }
1.224 moko 202: VStateless_class* get_class(const String& name);
1.260 moko 203: VStateless_class& get_class_ref(const String& name);
1.224 moko 204: void put_class(VStateless_class *aclass){ classes().put(aclass->type(), aclass); }
1.265 moko 205: bool add_class(const char* atype, VStateless_class *aclass);
1.6 paf 206:
1.65 paf 207: /**
208: core request processing
209: BEWARE: may throw exception to you: catch it!
210: */
1.261 moko 211: 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 212:
1.61 paf 213: /// executes ops
1.162 paf 214: void execute(ArrayOperation& ops); // execute.C
1.239 moko 215:
216: template<typename Frame> void call(Frame& frame){
217: VMethodFrame *saved_method_frame=method_frame;
218: Value* saved_rcontext=rcontext;
219: WContext *saved_wcontext=wcontext;
220:
221: rcontext=wcontext=method_frame=&frame;
222:
223: frame.call(*this);
224:
225: wcontext=saved_wcontext;
226: rcontext=saved_rcontext;
227: method_frame=saved_method_frame;
228: }
229:
230: template<typename Frame> void call_write(Frame& frame){
231: VMethodFrame *saved_method_frame=method_frame;
232: Value* saved_rcontext=rcontext;
233:
234: rcontext=method_frame=&frame;
235:
236: frame.call(*this);
237:
238: rcontext=saved_rcontext;
239: method_frame=saved_method_frame;
240: }
241:
1.225 moko 242: Value& construct(VStateless_class &class_value, const Method &method);
1.202 moko 243:
1.267 moko 244: /// execute ops with anti-recursion check
245: void recursion_checked_execute(ArrayOperation& ops) {
1.271 moko 246: if(++anti_endless_execute_recursion>=pa_execute_recursion_limit) {
1.267 moko 247: anti_endless_execute_recursion=0; // give @exception a chance
1.228 moko 248: throw Exception(PARSER_RUNTIME, 0, "call canceled - endless recursion detected");
1.128 paf 249: }
1.147 paf 250: execute(ops); // execute it
1.267 moko 251: anti_endless_execute_recursion--;
1.128 paf 252: }
1.40 paf 253:
1.199 misha 254: ///
1.255 moko 255: void use_file_directly(const String& file_spec, bool fail_on_file_absence=true, bool with_auto_p=false);
1.228 moko 256:
1.255 moko 257: /// compiles the file in main class context by default
258: void use_file(const String& file_name, const String* use_filespec, bool with_auto_p=false);
1.241 moko 259:
260: /// for @USE only, calls ^use (which may be user-defined)
261: void use_file(const String& file_name, const String* use_filespec, Operation::Origin origin);
1.199 misha 262:
1.64 paf 263: /// compiles a @a source buffer
1.228 moko 264: void use_buf(VStateless_class& aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset=0);
1.28 paf 265:
1.129 paf 266: /// processes any code-junction there may be inside of @a value
1.227 moko 267: Value& process_getter(Junction& junction); // execute.C
1.231 moko 268: Value& process(Value& input_value); // execute.C
1.192 misha 269: void process_write(Value& input_value); // execute.C
1.228 moko 270:
1.129 paf 271: //@{ convinient helpers
272: const String& process_to_string(Value& input_value) {
1.231 moko 273: return process(input_value).as_string();
1.129 paf 274: }
1.126 paf 275: //@}
1.247 moko 276: const Operation::Origin get_method_origin(const Method* method); // execute.C
1.242 moko 277: const String* get_method_filespec(const Method* method); // execute.C
278: const String* get_used_filespec(uint file_no);
1.126 paf 279:
1.238 moko 280: /// appending string with it's languages
1.239 moko 281: inline void write(const String& astring) {
1.236 moko 282: wcontext->write(astring);
1.49 paf 283: }
1.238 moko 284:
285: /// in [] and {} appending string if get_string is not null, else appending value
1.240 moko 286: /// in () appending string if is_string, else appending value
1.239 moko 287: inline void write(Value& avalue) {
1.236 moko 288: wcontext->write_as_string(avalue);
1.131 paf 289: }
290:
1.238 moko 291: /// allways appending value
1.239 moko 292: inline void write_value(Value& avalue) {
1.226 moko 293: wcontext->write(avalue);
294: }
295:
1.64 paf 296: /// returns relative to @a path path to @a file
1.162 paf 297: const String& relative(const char* apath, const String& relative_name);
1.61 paf 298:
1.256 moko 299: const String& full_disk_path(const String& relative_name);
1.42 paf 300:
1.209 misha 301: /// returns the mime type of 'user_file_name'
302: const String& mime_type_of(const String* file_name);
303:
1.80 paf 304: /// returns the mime type of 'user_file_name_cstr'
1.162 paf 305: const String& mime_type_of(const char* user_file_name_cstr);
1.80 paf 306:
1.117 paf 307: /// returns current SQL connection if any
1.228 moko 308: SQL_Connection* connection(bool fail_on_error=true) {
1.162 paf 309: if(fail_on_error && !fconnection)
1.228 moko 310: throw Exception(PARSER_RUNTIME, 0, "outside of 'connect' operator");
311: return fconnection;
1.133 paf 312: }
313:
1.245 moko 314: Skip get_skip() { return fskip; }
1.181 paf 315: void set_skip(Skip askip) { fskip=askip; }
1.246 moko 316: void set_skip_return(VMethodFrame& amethod_frame) { fskip=SKIP_RETURN; freturn_method_frame=&amethod_frame; }
1.245 moko 317: inline bool check_skip_break() { bool result=fskip >= SKIP_BREAK; if(fskip <= SKIP_BREAK) fskip=SKIP_NOTHING; return result; }
318: inline void check_skip_return() { if(fskip==SKIP_RETURN && method_frame==freturn_method_frame) fskip=SKIP_NOTHING; }
1.181 paf 319:
1.196 misha 320: void set_in_cycle(int adelta) { fin_cycle+=adelta; }
321: bool get_in_cycle() { return fin_cycle>0; }
322:
1.17 paf 323: public:
1.22 paf 324:
1.61 paf 325: /// info from web server
1.162 paf 326: Request_info& request_info;
327:
328: /// info about ServerAPI
329: SAPI_Info& sapi_info;
330:
331: /// source, client, mail charsets
332: Request_charsets charsets;
1.53 paf 333:
1.154 paf 334: /// 'MAIN' class conglomerat & operators are methods of this class
1.162 paf 335: VStateless_class& main_class;
1.86 paf 336: /// $form:elements
1.162 paf 337: VForm& form;
1.140 paf 338: /// $mail
1.162 paf 339: VMail& mail;
1.86 paf 340: /// $response:elements
1.162 paf 341: VResponse& response;
1.86 paf 342: /// $cookie:elements
1.162 paf 343: VCookie& cookie;
1.183 misha 344: /// $console
345: VConsole& console;
1.70 paf 346:
1.148 paf 347: /// classes configured data
1.193 misha 348: HashString<void*> classes_conf;
1.85 paf 349:
1.148 paf 350: public: // status read methods
1.76 paf 351:
1.148 paf 352: VMethodFrame *get_method_frame() { return method_frame; }
1.162 paf 353: Value& get_self();
1.227 moko 354:
1.162 paf 355: #define GET_SELF(request, type) (static_cast<type &>(request.get_self()))
1.6 paf 356:
1.205 moko 357: /// public for ^reflection:copy[]
358: void put_element(Value& ncontext, const String& name, Value* value);
359:
1.251 moko 360: /// for @main[] and parser://method/call
361: const String* execute_method(VStateless_class& aclass, const String& method_name, Value* optional_param = 0);
1.173 paf 362:
1.251 moko 363: //{ for @conf[filespec] and @auto[filespec]
364: bool execute_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param);
1.263 moko 365: bool execute_auto_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param);
1.228 moko 366:
1.173 paf 367: //}
368:
1.162 paf 369: #ifdef XML
370: public: // charset helpers
1.67 paf 371:
1.162 paf 372: /// @see Charset::transcode
1.180 paf 373: xmlChar* transcode(const String& s);
1.162 paf 374: /// @see Charset::transcode
1.180 paf 375: xmlChar* transcode(const String::Body s);
1.162 paf 376: /// @see Charset::transcode
1.180 paf 377: const String& transcode(const xmlChar* s);
1.89 parser 378:
1.162 paf 379: #endif
1.136 paf 380:
381: private:
382:
383: /// already executed some @conf method
384: bool configure_admin_done;
385:
1.162 paf 386: void configure_admin(VStateless_class& conf_class);
1.177 paf 387:
388: void configure();
1.99 parser 389:
1.7 paf 390: private: // compile.C
391:
1.228 moko 392: ArrayClass& compile(VStateless_class* aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset);
1.7 paf 393:
394: private: // execute.C
1.9 paf 395:
1.191 misha 396: Value& get_element(Value& ncontext, const String& name);
1.219 moko 397: #ifdef FEATURE_GET_ELEMENT4CALL
398: Value& get_element4call(Value& ncontext, const String& name);
399: #endif
1.22 paf 400:
1.58 paf 401: private: // defaults
402:
1.162 paf 403: const String::Language fdefault_lang;
1.80 paf 404:
405: private: // mime types
406:
407: /// $MAIN:MIME-TYPES
408: Table *mime_types;
1.22 paf 409:
1.117 paf 410: private: // connection manipulation
411:
1.162 paf 412: SQL_Connection* set_connection(SQL_Connection* aconnection) {
413: SQL_Connection* result=fconnection;
1.117 paf 414: fconnection=aconnection;
415: return result;
416: }
1.162 paf 417: void restore_connection(SQL_Connection* aconnection) {
1.117 paf 418: fconnection=aconnection;
419: }
420:
421: private:
422:
1.162 paf 423: void output_result(VFile* body_file, bool header_only, bool as_attachment);
1.148 paf 424: };
425:
1.273 moko 426: /// Hash-compatible exception; hash is built on demand.
427: class VException: public VHashReference {
428: Exception fexception;
429: String::Body ffile;
430: Value* fhandled;
1.275 ! moko 431: size_t ftrace_bottom, ftrace_top;
1.273 moko 432: public:
433: Operation::Origin origin;
434: const String* problem_source;
435:
436: VException(Request& r, const Exception& e);
437:
438: override HashStringValue& hash();
439: override Value* get_element(const String& name);
440: override const VJunction* put_element(const String& name, Value* value);
441:
442: Value* handled();
1.275 ! moko 443: Table& stack(Request& r);
! 444: void expire_trace() { ftrace_top=size_t(-1); }
1.273 moko 445: };
446:
1.148 paf 447: /// Auto-object used to save request context across ^try body
448: class Request_context_saver {
449: Request& fr;
450:
451: /// execution stack
1.162 paf 452: size_t stack;
1.267 moko 453: uint anti_endless_execute_recursion;
1.148 paf 454: /// contexts
1.162 paf 455: VMethodFrame* method_frame;
456: Value* rcontext;
457: WContext* wcontext;
1.148 paf 458: /// current language
1.162 paf 459: String::Language flang;
1.148 paf 460: /// current connection
1.162 paf 461: SQL_Connection* fconnection;
1.126 paf 462:
1.148 paf 463: public:
1.228 moko 464: Request_context_saver(Request& ar) :
1.171 paf 465: fr(ar),
1.165 paf 466: stack(ar.stack.top_index()),
1.267 moko 467: anti_endless_execute_recursion(ar.anti_endless_execute_recursion),
1.148 paf 468: method_frame(ar.method_frame),
469: rcontext(ar.rcontext),
470: wcontext(ar.wcontext),
471: flang(ar.flang),
1.171 paf 472: fconnection(ar.fconnection) {}
1.153 paf 473: void restore() {
1.165 paf 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: