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