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