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