Annotation of parser3/src/include/pa_request.h, revision 1.277
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.277 ! moko 11: #define IDENT_PA_REQUEST_H "$Id: pa_request.h,v 1.276 2026/09/26 17:41:10 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.276 moko 150: int execute_recursion_limit;
151: void recursion_limit_reached();
1.162 paf 152:
153: ///@}
154:
155: /// execution stack
156: Stack<StackItem> stack;
157:
1.274 moko 158: public:
1.162 paf 159: /// exception stack trace
160: Exception_trace exception_trace;
161:
1.213 moko 162: bool allow_class_replace;
163:
1.162 paf 164: //@{ request processing status
165: /// contexts
166: VMethodFrame* method_frame;
167: Value* rcontext;
168: WContext* wcontext;
169: /// current language
1.228 moko 170: String::Language flang;
1.162 paf 171: /// current connection
172: SQL_Connection* fconnection;
173: //@}
1.245 moko 174:
175: private:
176:
177: int fin_cycle;
1.181 paf 178: Skip fskip;
1.245 moko 179: VMethodFrame* freturn_method_frame;
1.162 paf 180:
181: public:
1.175 paf 182: uint register_file(String::Body file_spec);
1.162 paf 183:
1.273 moko 184: const char* get_exception_cstr(const Exception& e, VException& details);
1.162 paf 185:
186: /// @see Stack::wipe_unused
187: void wipe_unused_execution_stack() {
188: stack.wipe_unused();
189: }
1.112 paf 190:
191: #ifdef RESOURCES_DEBUG
192: /// measures
193: double sql_connect_time;
194: double sql_request_time;
195: #endif
1.61 paf 196:
1.162 paf 197: Request(SAPI_Info& asapi_info, Request_info& arequest_info,
1.208 moko 198: String::Language adefault_lang ///< all tainted data default untainting lang
1.50 paf 199: );
1.118 paf 200: ~Request();
1.1 paf 201:
1.61 paf 202: /// global classes
1.270 moko 203: OrderedHashString<VStateless_class*>& classes() { return fclasses; }
1.224 moko 204: VStateless_class* get_class(const String& name);
1.260 moko 205: VStateless_class& get_class_ref(const String& name);
1.224 moko 206: void put_class(VStateless_class *aclass){ classes().put(aclass->type(), aclass); }
1.265 moko 207: bool add_class(const char* atype, VStateless_class *aclass);
1.6 paf 208:
1.65 paf 209: /**
210: core request processing
211: BEWARE: may throw exception to you: catch it!
212: */
1.261 moko 213: 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 214:
1.61 paf 215: /// executes ops
1.162 paf 216: void execute(ArrayOperation& ops); // execute.C
1.239 moko 217:
218: template<typename Frame> void call(Frame& frame){
219: VMethodFrame *saved_method_frame=method_frame;
220: Value* saved_rcontext=rcontext;
221: WContext *saved_wcontext=wcontext;
222:
223: rcontext=wcontext=method_frame=&frame;
224:
225: frame.call(*this);
226:
227: wcontext=saved_wcontext;
228: rcontext=saved_rcontext;
229: method_frame=saved_method_frame;
230: }
231:
232: template<typename Frame> void call_write(Frame& frame){
233: VMethodFrame *saved_method_frame=method_frame;
234: Value* saved_rcontext=rcontext;
235:
236: rcontext=method_frame=&frame;
237:
238: frame.call(*this);
239:
240: rcontext=saved_rcontext;
241: method_frame=saved_method_frame;
242: }
243:
1.225 moko 244: Value& construct(VStateless_class &class_value, const Method &method);
1.202 moko 245:
1.267 moko 246: /// execute ops with anti-recursion check
247: void recursion_checked_execute(ArrayOperation& ops) {
1.276 moko 248: if(++anti_endless_execute_recursion>=execute_recursion_limit)
249: recursion_limit_reached();
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.277 ! moko 254: /// take the recovery reserve back if recursion limit is far enough
! 255: void recursion_limit_try_restore() {
! 256: if(execute_recursion_limit>pa_execute_recursion_limit && anti_endless_execute_recursion<pa_execute_recursion_limit/2)
! 257: execute_recursion_limit=pa_execute_recursion_limit;
! 258: }
! 259:
1.199 misha 260: ///
1.255 moko 261: void use_file_directly(const String& file_spec, bool fail_on_file_absence=true, bool with_auto_p=false);
1.228 moko 262:
1.255 moko 263: /// compiles the file in main class context by default
264: void use_file(const String& file_name, const String* use_filespec, bool with_auto_p=false);
1.241 moko 265:
266: /// for @USE only, calls ^use (which may be user-defined)
267: void use_file(const String& file_name, const String* use_filespec, Operation::Origin origin);
1.199 misha 268:
1.64 paf 269: /// compiles a @a source buffer
1.228 moko 270: void use_buf(VStateless_class& aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset=0);
1.28 paf 271:
1.129 paf 272: /// processes any code-junction there may be inside of @a value
1.227 moko 273: Value& process_getter(Junction& junction); // execute.C
1.231 moko 274: Value& process(Value& input_value); // execute.C
1.192 misha 275: void process_write(Value& input_value); // execute.C
1.228 moko 276:
1.129 paf 277: //@{ convinient helpers
278: const String& process_to_string(Value& input_value) {
1.231 moko 279: return process(input_value).as_string();
1.129 paf 280: }
1.126 paf 281: //@}
1.247 moko 282: const Operation::Origin get_method_origin(const Method* method); // execute.C
1.242 moko 283: const String* get_method_filespec(const Method* method); // execute.C
284: const String* get_used_filespec(uint file_no);
1.126 paf 285:
1.238 moko 286: /// appending string with it's languages
1.239 moko 287: inline void write(const String& astring) {
1.236 moko 288: wcontext->write(astring);
1.49 paf 289: }
1.238 moko 290:
291: /// in [] and {} appending string if get_string is not null, else appending value
1.240 moko 292: /// in () appending string if is_string, else appending value
1.239 moko 293: inline void write(Value& avalue) {
1.236 moko 294: wcontext->write_as_string(avalue);
1.131 paf 295: }
296:
1.238 moko 297: /// allways appending value
1.239 moko 298: inline void write_value(Value& avalue) {
1.226 moko 299: wcontext->write(avalue);
300: }
301:
1.64 paf 302: /// returns relative to @a path path to @a file
1.162 paf 303: const String& relative(const char* apath, const String& relative_name);
1.61 paf 304:
1.256 moko 305: const String& full_disk_path(const String& relative_name);
1.42 paf 306:
1.209 misha 307: /// returns the mime type of 'user_file_name'
308: const String& mime_type_of(const String* file_name);
309:
1.80 paf 310: /// returns the mime type of 'user_file_name_cstr'
1.162 paf 311: const String& mime_type_of(const char* user_file_name_cstr);
1.80 paf 312:
1.117 paf 313: /// returns current SQL connection if any
1.228 moko 314: SQL_Connection* connection(bool fail_on_error=true) {
1.162 paf 315: if(fail_on_error && !fconnection)
1.228 moko 316: throw Exception(PARSER_RUNTIME, 0, "outside of 'connect' operator");
317: return fconnection;
1.133 paf 318: }
319:
1.245 moko 320: Skip get_skip() { return fskip; }
1.181 paf 321: void set_skip(Skip askip) { fskip=askip; }
1.246 moko 322: void set_skip_return(VMethodFrame& amethod_frame) { fskip=SKIP_RETURN; freturn_method_frame=&amethod_frame; }
1.245 moko 323: inline bool check_skip_break() { bool result=fskip >= SKIP_BREAK; if(fskip <= SKIP_BREAK) fskip=SKIP_NOTHING; return result; }
324: inline void check_skip_return() { if(fskip==SKIP_RETURN && method_frame==freturn_method_frame) fskip=SKIP_NOTHING; }
1.181 paf 325:
1.196 misha 326: void set_in_cycle(int adelta) { fin_cycle+=adelta; }
327: bool get_in_cycle() { return fin_cycle>0; }
328:
1.17 paf 329: public:
1.22 paf 330:
1.61 paf 331: /// info from web server
1.162 paf 332: Request_info& request_info;
333:
334: /// info about ServerAPI
335: SAPI_Info& sapi_info;
336:
337: /// source, client, mail charsets
338: Request_charsets charsets;
1.53 paf 339:
1.154 paf 340: /// 'MAIN' class conglomerat & operators are methods of this class
1.162 paf 341: VStateless_class& main_class;
1.86 paf 342: /// $form:elements
1.162 paf 343: VForm& form;
1.140 paf 344: /// $mail
1.162 paf 345: VMail& mail;
1.86 paf 346: /// $response:elements
1.162 paf 347: VResponse& response;
1.86 paf 348: /// $cookie:elements
1.162 paf 349: VCookie& cookie;
1.183 misha 350: /// $console
351: VConsole& console;
1.70 paf 352:
1.148 paf 353: /// classes configured data
1.193 misha 354: HashString<void*> classes_conf;
1.85 paf 355:
1.148 paf 356: public: // status read methods
1.76 paf 357:
1.148 paf 358: VMethodFrame *get_method_frame() { return method_frame; }
1.162 paf 359: Value& get_self();
1.227 moko 360:
1.162 paf 361: #define GET_SELF(request, type) (static_cast<type &>(request.get_self()))
1.6 paf 362:
1.205 moko 363: /// public for ^reflection:copy[]
364: void put_element(Value& ncontext, const String& name, Value* value);
365:
1.251 moko 366: /// for @main[] and parser://method/call
367: const String* execute_method(VStateless_class& aclass, const String& method_name, Value* optional_param = 0);
1.173 paf 368:
1.251 moko 369: //{ for @conf[filespec] and @auto[filespec]
370: bool execute_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param);
1.263 moko 371: bool execute_auto_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param);
1.228 moko 372:
1.173 paf 373: //}
374:
1.162 paf 375: #ifdef XML
376: public: // charset helpers
1.67 paf 377:
1.162 paf 378: /// @see Charset::transcode
1.180 paf 379: xmlChar* transcode(const String& s);
1.162 paf 380: /// @see Charset::transcode
1.180 paf 381: xmlChar* transcode(const String::Body s);
1.162 paf 382: /// @see Charset::transcode
1.180 paf 383: const String& transcode(const xmlChar* s);
1.89 parser 384:
1.162 paf 385: #endif
1.136 paf 386:
387: private:
388:
389: /// already executed some @conf method
390: bool configure_admin_done;
391:
1.162 paf 392: void configure_admin(VStateless_class& conf_class);
1.177 paf 393:
394: void configure();
1.99 parser 395:
1.7 paf 396: private: // compile.C
397:
1.228 moko 398: ArrayClass& compile(VStateless_class* aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset);
1.7 paf 399:
400: private: // execute.C
1.9 paf 401:
1.191 misha 402: Value& get_element(Value& ncontext, const String& name);
1.219 moko 403: #ifdef FEATURE_GET_ELEMENT4CALL
404: Value& get_element4call(Value& ncontext, const String& name);
405: #endif
1.22 paf 406:
1.58 paf 407: private: // defaults
408:
1.162 paf 409: const String::Language fdefault_lang;
1.80 paf 410:
411: private: // mime types
412:
413: /// $MAIN:MIME-TYPES
414: Table *mime_types;
1.22 paf 415:
1.117 paf 416: private: // connection manipulation
417:
1.162 paf 418: SQL_Connection* set_connection(SQL_Connection* aconnection) {
419: SQL_Connection* result=fconnection;
1.117 paf 420: fconnection=aconnection;
421: return result;
422: }
1.162 paf 423: void restore_connection(SQL_Connection* aconnection) {
1.117 paf 424: fconnection=aconnection;
425: }
426:
427: private:
428:
1.162 paf 429: void output_result(VFile* body_file, bool header_only, bool as_attachment);
1.148 paf 430: };
431:
1.273 moko 432: /// Hash-compatible exception; hash is built on demand.
433: class VException: public VHashReference {
434: Exception fexception;
435: String::Body ffile;
436: Value* fhandled;
1.275 moko 437: size_t ftrace_bottom, ftrace_top;
1.273 moko 438: public:
439: Operation::Origin origin;
440: const String* problem_source;
441:
442: VException(Request& r, const Exception& e);
443:
444: override HashStringValue& hash();
445: override Value* get_element(const String& name);
446: override const VJunction* put_element(const String& name, Value* value);
447:
448: Value* handled();
1.275 moko 449: Table& stack(Request& r);
450: void expire_trace() { ftrace_top=size_t(-1); }
1.273 moko 451: };
452:
1.148 paf 453: /// Auto-object used to save request context across ^try body
454: class Request_context_saver {
455: Request& fr;
456:
457: /// execution stack
1.162 paf 458: size_t stack;
1.277 ! moko 459: int anti_endless_execute_recursion;
1.148 paf 460: /// contexts
1.162 paf 461: VMethodFrame* method_frame;
462: Value* rcontext;
463: WContext* wcontext;
1.148 paf 464: /// current language
1.162 paf 465: String::Language flang;
1.148 paf 466: /// current connection
1.162 paf 467: SQL_Connection* fconnection;
1.126 paf 468:
1.148 paf 469: public:
1.228 moko 470: Request_context_saver(Request& ar) :
1.171 paf 471: fr(ar),
1.165 paf 472: stack(ar.stack.top_index()),
1.267 moko 473: anti_endless_execute_recursion(ar.anti_endless_execute_recursion),
1.148 paf 474: method_frame(ar.method_frame),
475: rcontext(ar.rcontext),
476: wcontext(ar.wcontext),
477: flang(ar.flang),
1.171 paf 478: fconnection(ar.fconnection) {}
1.153 paf 479: void restore() {
1.165 paf 480: fr.stack.set_top_index(stack);
1.267 moko 481: fr.anti_endless_execute_recursion=anti_endless_execute_recursion;
1.157 paf 482: fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148 paf 483: fr.flang=flang;
484: fr.fconnection=fconnection;
485: }
1.39 paf 486: };
487:
1.117 paf 488: /// Auto-object used for temporary changing Request::fconnection.
489: class Temp_connection {
490: Request& frequest;
1.162 paf 491: SQL_Connection* saved_connection;
1.117 paf 492: public:
1.228 moko 493: Temp_connection(Request& arequest, SQL_Connection* aconnection) :
1.117 paf 494: frequest(arequest),
495: saved_connection(arequest.set_connection(aconnection)) {
496: }
1.228 moko 497: ~Temp_connection() {
1.117 paf 498: frequest.restore_connection(saved_connection);
1.39 paf 499: }
1.91 parser 500: };
501:
1.196 misha 502: /// Auto-object used for break out of cycle check
503: class InCycle {
504: Request& frequest;
505: public:
506: InCycle(Request& arequest) : frequest(arequest) {
507: frequest.set_in_cycle(1);
508: }
1.228 moko 509: ~InCycle() {
1.196 misha 510: frequest.set_in_cycle(-1);
511: }
512: };
1.91 parser 513:
1.245 moko 514: /// Auto-object used for break out of cycle check
515: class TempSkip4Delimiter {
516: Request& frequest;
517: Request::Skip fskip;
518: public:
519: TempSkip4Delimiter(Request& arequest) : frequest(arequest), fskip(arequest.get_skip()) {
520: frequest.set_skip(Request::SKIP_NOTHING);
521: }
522: // returns true if break required, should be called
523: bool check_break() {
524: if(frequest.get_skip())
525: fskip=frequest.get_skip();
526: frequest.set_skip(fskip <= Request::SKIP_BREAK ? Request::SKIP_NOTHING : fskip);
527: return fskip >= Request::SKIP_BREAK;
528: }
529: };
530:
1.213 moko 531: /// Auto-object used for temporary changing Request::allow_class_replace.
532: class Temp_class_replace {
533: Request& frequest;
534: public:
535: Temp_class_replace(Request& arequest, bool avalue) : frequest(arequest){
536: frequest.allow_class_replace=avalue;
537: }
538: ~Temp_class_replace() {
539: frequest.allow_class_replace=false;
540: }
541: };
542:
1.214 moko 543: /// Auto-object used for temporarily substituting/removing elements
544: class Temp_value_element {
545: Request& frequest;
546: Value& fwhere;
547: const String& fname;
1.216 moko 548: Value* saved;
1.214 moko 549: public:
1.216 moko 550: Temp_value_element(Request& arequest, Value& awhere, const String& aname, Value* awhat);
551: ~Temp_value_element();
1.214 moko 552: };
553:
1.267 moko 554: /// Auto-object that temporarily increases recursion
555: class Temp_recursion {
556: Request& frequest;
557: public:
558: Temp_recursion(Request& arequest) : frequest(arequest){
559: frequest.anti_endless_execute_recursion++;
560: }
561: ~Temp_recursion(){
562: frequest.anti_endless_execute_recursion--;
563: }
564: };
565:
1.1 paf 566: #endif
E-mail: