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