Annotation of parser3/src/include/pa_request.h, revision 1.160.2.37.2.20

1.61      paf         1: /** @file
1.62      paf         2:        Parser: request class decl.
                      3: 
1.160.2.9  paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.121     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_REQUEST_H
                      9: #define PA_REQUEST_H
1.141     paf        10: 
1.160.2.37.2.  0(paf      11:3): static const char* IDENT_REQUEST_H="$Date: 2003/04/03 06:54:27 $";
1.1       paf        12: 
1.5       paf        13: #include "pa_hash.h"
1.7       paf        14: #include "pa_wcontext.h"
1.6       paf        15: #include "pa_value.h"
1.7       paf        16: #include "pa_stack.h"
1.160.2.6  paf        17: #include "pa_request_info.h"
                     18: #include "pa_request_charsets.h"
1.160.2.8  paf        19: #include "pa_sapi.h"
1.44      paf        20: 
1.112     paf        21: #ifdef RESOURCES_DEBUG
                     22: #include <sys/resource.h>
                     23: #endif
                     24: 
1.128     paf        25: // consts
                     26: 
1.160.2.35  paf        27: const uint ANTI_ENDLESS_EXECUTE_RECOURSION=1000;
1.128     paf        28: 
1.160.2.8  paf        29: // forwards
                     30: 
1.39      paf        31: class Temp_lang;
1.84      paf        32: class Methoded;
1.116     paf        33: class VMethodFrame;
1.160.2.30  paf        34: class GdomeDOMString_auto_ptr;
1.160.2.37.2.  3(paf      35:3): class VMail;
                     36:3): class VForm;
                     37:3): class VResponse;
                     38:3): class VCookie;
                     39:3): class VStateless_class;
1.1       paf        40: 
1.61      paf        41: /// Main workhorse.
1.160.2.6  paf        42: class Request: public PA_Object {
1.107     paf        43:        friend class Temp_lang;
1.117     paf        44:        friend class Temp_connection;
1.148     paf        45:        friend class Request_context_saver;
1.155     paf        46:        friend class Temp_request_self;
1.160.2.37.2.  0(paf      47:3):     friend class Exception_trace;
1.160.2.6  paf        48: 
1.160.2.37.2.  0(paf      49:3): public:
                     50:3):     class Trace {
                     51:3):             const String* fname;
                     52:3):             Operation::Info forigin;
                     53:3):     public:
                     54:3):             Trace(): fname(0) {}
                     55:3):             void clear() { fname=0; }
                     56:3):             operator bool() const { return fname!=0; }
                     57:3): 
                     58:3):             Trace(const String* aname, const Operation::Info aorigin):
                     59:3):                     fname(aname), forigin(aorigin) {}
                     60:3): 
                     61:3):             const String* name() const { return fname; }
                     62:3):             const Operation::Info origin() const { return forigin; }
                     63:3):     };
                     64:3): 
                     65:3): 
                     66:3): private:
          (paf       67:):      union StackItem {
          5(paf      68:3):             Value* fvalue;
                     69:3):             ArrayOperation* fops;
                     70:3):             VMethodFrame* fmethod_frame;
                     71:3):     public:
                     72:3):             Value& value() const { return *fvalue; }
                     73:3):             const String& string() const { 
                     74:3):                     const String* result=fvalue->get_string();
                     75:3):                     assert(result);
                     76:3):                     return *result; 
                     77:3):             }
                     78:3):             ArrayOperation& ops() const { return *fops; }
                     79:3):             VMethodFrame& method_frame() const { return *fmethod_frame; }
1.160.2.12  paf        80: 
                     81:                /// needed to fill unused Array entries
                     82:                StackItem() {}
1.160.2.37.2.  5(paf      83:3):             StackItem(Value& avalue): fvalue(&avalue) {}            
                     84:3):             StackItem(ArrayOperation& aops): fops(&aops) {}
                     85:3):             StackItem(VMethodFrame& amethod_frame): fmethod_frame(&amethod_frame) {}
1.160.2.12  paf        86:        };
                     87: 
1.160.2.37.2.  0(paf      88:3):     class Exception_trace: public Stack<Trace> {
                     89:3):             size_t fbottom;
          7(paf      90:3):     public:
          0(paf      91:3):             Exception_trace(): fbottom(0) {}
                     92:3): 
                     93:3):             size_t bottom_index() { return fbottom; }
                     94:3):             element_type bottom_value() { return get(bottom_index()); }
                     95:3): 
                     96:3):             void clear() {
                     97:3):                     ftop=fbottom=0;
                     98:3):             }
                     99:3): 
                    100:3):             bool is_empty() {
                    101:3):                     return ftop==fbottom;
                    102:3):             }
          7(paf     103:3): 
          0(paf     104:3):             const element_type extract_origin(const String*& problem_source);
          7(paf     105:3):     };
                    106:3): 
                    107:3):     ///@{ core data
                    108:3): 
                    109:3):     /// classes
                    110:3):     HashStringValue fclasses;
                    111:3): 
                    112:3):     /// already used files to avoid cyclic uses
                    113:3):     Hash<const StringBody, bool> used_files;
                    114:3):     /// list of all used files, Operation::file_no = index to it
                    115:3):     Array<StringBody> file_list;
                    116:3): 
                    117:3):     /**     endless execute(execute(... preventing counter 
                    118:3):             @see ANTI_ENDLESS_EXECUTE_RECOURSION
                    119:3):     */
                    120:3):     uint anti_endless_execute_recoursion;
                    121:3): 
                    122:3):     ///@}
                    123:3): 
          4(paf     124:3):     /// execution stack
                    125:3):     Stack<StackItem> stack;
                    126:3): 
          7(paf     127:3):     /// exception stack trace
          0(paf     128:3):     Exception_trace exception_trace;
          4(paf     129:3): public:
                    130:3): 
1.160.2.6  paf       131:        //@{ request processing status
                    132:        /// contexts
1.160.2.37.2.  (paf      133:):      VMethodFrame* method_frame;
                    134:):      Value* rcontext;
                    135:):      WContext* wcontext;
1.160.2.6  paf       136:        /// current language
1.160.2.37.2.  (paf      137:):      String::Language flang; 
1.160.2.6  paf       138:        /// current connection
1.160.2.37.2.  (paf      139:):      SQL_Connection* fconnection;
1.160.2.6  paf       140:        //@}
                    141:        /// interrupted flag, raised on signals [SIGPIPE]
                    142:        bool finterrupted;
                    143: 
1.1       paf       144: public:
1.160.2.37.2.  0(paf     145:3): 
                    146:3):     struct Exception_details {
                    147:3):             const Trace trace;
                    148:3):             const String* problem_source;
                    149:3):             VHash& vhash;
                    150:3): 
                    151:3):             Exception_details(
                    152:3):                     const Trace atrace,
                    153:3):                     const String* aproblem_source,
                    154:3):                     VHash& avhash):
                    155:3):                     trace(atrace), problem_source(aproblem_source), vhash(avhash) {}
                    156:3): 
                    157:3):     };
                    158:3):     Exception_details get_details(const Exception& e);
                    159:3): 
          4(paf     160:3):     /// @see Stack::wipe_unused
                    161:3):     void wipe_unused_execution_stack() {
                    162:3):             stack.wipe_unused();
                    163:3):     }
1.112     paf       164: 
                    165: #ifdef RESOURCES_DEBUG
                    166:        /// measures
                    167:        double sql_connect_time;
                    168:        double sql_request_time;
                    169: #endif 
1.61      paf       170: 
1.160.2.37.2.  (paf      171:):      Request(SAPI_Info& asapi_info, Request_info& arequest_info,
                    172:):              String::Language adefault_lang, ///< all tainted data default untainting lang
1.110     paf       173:                bool status_allowed ///<  status class allowed
1.50      paf       174:        );
1.118     paf       175:        ~Request();
1.1       paf       176: 
1.61      paf       177:        /// global classes
1.160.2.8  paf       178:        HashStringValue& classes() { return fclasses; }
1.6       paf       179: 
1.65      paf       180:        /**
                    181:                core request processing
                    182: 
                    183:                BEWARE: may throw exception to you: catch it!
                    184:        */
                    185:        void core(
1.160.2.9  paf       186:                const char* config_filespec, ///< system config filespec
1.138     paf       187:                bool config_fail_on_read_problem, ///< fail if system config file not found
1.65      paf       188:                bool header_only);
1.17      paf       189: 
1.61      paf       190:        /// executes ops
1.160.2.12  paf       191:        void execute(ArrayOperation& ops); // execute.C
1.127     paf       192:        /// execute ops with anti-recoursion check
1.160.2.37.2.  (paf      193:):      void recoursion_checked_execute(/*const String& name, */ArrayOperation& ops) {
1.128     paf       194:                // anti_endless_execute_recoursion
                    195:                if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
                    196:                        anti_endless_execute_recoursion=0; // give @exception a chance
                    197:                        throw Exception("parser.runtime",
1.160.2.37.2.  (paf      198:):                              0, //&name,
1.128     paf       199:                                "call canceled - endless recursion detected");
                    200:                }
1.147     paf       201:                execute(ops); // execute it
1.128     paf       202:                anti_endless_execute_recoursion--;
                    203:        }
1.40      paf       204: 
1.64      paf       205:        /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.160.2.37  paf       206:        void use_file(VStateless_class& aclass,
1.160.2.37.2.  (paf      207:):              const String& file_name, 
1.149     paf       208:                bool ignore_class_path=false, 
1.160.2.8  paf       209:                bool fail_on_read_problem=true, bool fail_on_file_absence=true); // pa_request.C
1.64      paf       210:        /// compiles a @a source buffer
1.160.2.37  paf       211:        void use_buf(VStateless_class& aclass,
1.160.2.9  paf       212:                const char* source, 
1.160.2.37.2.  6(paf     213:3):             uint file_no); // pa_request.C
1.28      paf       214: 
1.129     paf       215:        /// processes any code-junction there may be inside of @a value
1.160.2.37.2.  (paf      216:):      StringOrValue process(Value& input_value, bool intercept_string=true); // execute.C
1.129     paf       217:        //@{ convinient helpers
1.160.2.37.2.  (paf      218:):      const String& process_to_string(Value& input_value) {
                    219:):              return process(input_value, true/*intercept_string*/).as_string();
1.129     paf       220:        }
1.160.2.37.2.  (paf      221:):      Value& process_to_value(Value& input_value, bool intercept_string=true) {
1.129     paf       222:                return process(input_value, intercept_string).as_value();
1.126     paf       223:        }
                    224:        //@}
1.131     paf       225: 
1.126     paf       226:        
1.131     paf       227: #define DEFINE_DUAL(modification) \
                    228:        void write_##modification##_lang(StringOrValue dual) { \
1.160.2.37.2.  (paf      229:):              if(const String* string=dual.get_string()) \
1.132     paf       230:                        write_##modification##_lang(*string); \
                    231:                else \
1.160.2.37.2.  (paf      232:):                      write_##modification##_lang(*dual.get_value()); \
1.132     paf       233:        }
1.131     paf       234: 
1.61      paf       235:        /// appending, sure of clean string inside
1.65      paf       236:        void write_no_lang(const String& astring) {
1.111     paf       237:                wcontext->write(astring, 
1.160.2.37.2.  (paf      238:):                      (String::Language)(String::L_CLEAN | flang&String::L_OPTIMIZE_BIT));
1.49      paf       239:        }
1.131     paf       240:        /// appending sure value, that would be converted to clean string
1.160.2.37.2.  (paf      241:):      void write_no_lang(Value& avalue) {
1.131     paf       242:                if(wcontext->get_in_expression())
1.160.2.37.2.  (paf      243:):                      wcontext->write(avalue);
1.131     paf       244:                else
1.160.2.37.2.  (paf      245:):                      wcontext->write(avalue, 
                    246:):                              (String::Language)(String::L_CLEAN | flang&String::L_OPTIMIZE_BIT));
1.131     paf       247:        }
                    248: 
1.61      paf       249:        /// appending string, passing language built into string being written
1.65      paf       250:        void write_pass_lang(const String& astring) {
1.160.2.37.2.  (paf      251:):              wcontext->write(astring, String::L_PASS_APPENDED); 
1.59      paf       252:        }
1.131     paf       253:        /// appending possible string, passing language built into string being written
1.160.2.37.2.  (paf      254:):      void write_pass_lang(Value& avalue) {
                    255:):              wcontext->write(avalue, String::L_PASS_APPENDED); 
1.131     paf       256:        }
                    257:        DEFINE_DUAL(pass)
                    258: 
1.61      paf       259:        /// appending possible string, assigning untaint language
1.160.2.37.2.  (paf      260:):      void write_assign_lang(Value& avalue) {
                    261:):              wcontext->write(avalue, flang); 
1.132     paf       262:        }
1.106     parser    263:        /// appending string, assigning untaint language
                    264:        void write_assign_lang(const String& astring) {
                    265:                wcontext->write(astring, flang); 
1.22      paf       266:        }
1.131     paf       267:        DEFINE_DUAL(assign)
1.22      paf       268: 
1.64      paf       269:        /// returns relative to @a path  path to @a file 
1.160.2.37.2.  (paf      270:):      const String& relative(const char* apath, const String& relative_name);
1.61      paf       271: 
1.64      paf       272:        /// returns an absolute @a path to relative @a name
1.160.2.37.2.  (paf      273:):      const String& absolute(const String& relative_name);
1.42      paf       274: 
1.80      paf       275:        /// returns the mime type of 'user_file_name_cstr'
1.160.2.37.2.  (paf      276:):      const String& mime_type_of(const char* user_file_name_cstr);
1.80      paf       277: 
1.117     paf       278:        /// returns current SQL connection if any
1.160.2.37.2.  (paf      279:):      SQL_Connection* connection(bool fail_on_error=true) { 
                    280:):              if(fail_on_error && !fconnection)
1.125     paf       281:                        throw Exception("parser.runtime",
1.160.2.37.2.  (paf      282:):                              0,
1.117     paf       283:                                "outside of 'connect' operator");
                    284: 
                    285:                return fconnection; 
1.133     paf       286:        }
                    287: 
1.160.2.36  paf       288:        void set_interrupted(bool ainterrupted) { finterrupted=ainterrupted; }
                    289:        bool get_interrupted() { return finterrupted; }
1.158     paf       290: 
1.17      paf       291: public:
1.22      paf       292:        
1.61      paf       293:        /// info from web server
1.160.2.8  paf       294:        Request_info& request_info;
                    295: 
                    296:        /// info about ServerAPI
                    297:        SAPI_Info& sapi_info;
1.160.2.2  paf       298: 
                    299:        /// source, client, mail charsets
1.160.2.6  paf       300:        Request_charsets charsets;
1.53      paf       301: 
1.154     paf       302:        /// 'MAIN' class conglomerat & operators are methods of this class
1.160.2.37.2.  (paf      303:):      VStateless_class& main_class;
1.86      paf       304:        /// $form:elements
1.160.2.37.2.  (paf      305:):      VForm& form;
1.140     paf       306:        /// $mail
1.160.2.37.2.  (paf      307:):      VMail& mail;
1.86      paf       308:        /// $response:elements
1.160.2.37.2.  (paf      309:):      VResponse& response;
1.86      paf       310:        /// $cookie:elements
1.160.2.37.2.  (paf      311:):      VCookie& cookie;
1.70      paf       312: 
1.148     paf       313:        /// classes configured data
1.160.2.20  paf       314:        HashStringObject classes_conf;
1.85      paf       315: 
1.148     paf       316: public: // status read methods
1.76      paf       317: 
1.148     paf       318:        VMethodFrame *get_method_frame() { return method_frame; }
1.160.2.37.2.  (paf      319:):      Value& get_self();
                    320:): #define GET_SELF(request, type) (static_cast<type &>(request.get_self()))
1.160.2.18  paf       321:        /* for strange reason call to this: 
                    322:                r.get_self<VHash>() 
                    323:                refuses to compile
                    324: 
                    325:        template<typename T> T& get_self() {
                    326:                return *static_cast<T*>(get_self().get());
                    327:        }
                    328:        */
1.160.2.30  paf       329: 
                    330: #ifdef XML
                    331: public: // charset helpers
                    332: 
                    333:        /// @see Charset::transcode
1.160.2.37.2.  (paf      334:):      GdomeDOMString_auto_ptr transcode(const String& s);
          2(paf     335:3):     /// @see Charset::transcode
                    336:3):     GdomeDOMString_auto_ptr transcode(const StringBody s);
1.160.2.30  paf       337:        /// @see Charset::transcode
1.160.2.37.2.  1(paf     338:3):     const String& transcode(GdomeDOMString* s);
1.160.2.31  paf       339:        /// @see Charset::transcode
1.160.2.37.2.  1(paf     340:3):     const String& transcode(xmlChar* s);
1.160.2.31  paf       341: 
1.160.2.30  paf       342: #endif
1.87      paf       343: 
1.136     paf       344: private:
                    345: 
                    346:        /// already executed some @conf method
                    347:        bool configure_admin_done;
                    348: 
1.160.2.37.2.  (paf      349:):      void configure_admin(VStateless_class& conf_class);
1.99      parser    350: 
1.7       paf       351: private: // compile.C
                    352: 
1.160.2.37.2.  6(paf     353:3):     VStateless_class& compile(VStateless_class* aclass, const char* source, uint file_no);
1.7       paf       354: 
                    355: private: // execute.C
                    356: 
1.139     paf       357:        /// for @postprocess[body]
1.160.2.37.2.  (paf      358:):      const String& execute_method(VMethodFrame& amethodFrame, const Method& method);
1.139     paf       359:        //{ for @conf[filespec] and @auto[filespec]
1.160.2.37.2.  (paf      360:):      const String* execute_method(Value& aself, 
                    361:):              const Method& method, VString* optional_param,
1.160.2.10  paf       362:                bool do_return_string);
1.160.2.25  paf       363:        struct Execute_nonvirtual_method_result {
1.160.2.37.2.  (paf      364:):              const String* string;
                    365:):              Method* method;
                    366:):              Execute_nonvirtual_method_result(): string(0), method(0) {}
1.160.2.25  paf       367:        };
                    368:        Execute_nonvirtual_method_result execute_nonvirtual_method(VStateless_class& aclass, 
1.160.2.37.2.  (paf      369:):              const String& method_name, VString* optional_param,
1.160.2.25  paf       370:                bool do_return_string);
1.139     paf       371:        //}
                    372:        /// for @main[]
1.160.2.37.2.  (paf      373:):      const String* execute_virtual_method(Value& aself, const String& method_name);
1.9       paf       374: 
1.160.2.37.2.  7(paf     375:3):     Value& get_element(Value& ncontext, const String& name, bool can_call_operator);
1.22      paf       376: 
1.58      paf       377: private: // defaults
                    378: 
1.160.2.37.2.  (paf      379:):      const String::Language fdefault_lang;
1.80      paf       380: 
                    381: private: // mime types
                    382: 
                    383:        /// $MAIN:MIME-TYPES
                    384:        Table *mime_types;
1.22      paf       385: 
1.39      paf       386: private: // lang manipulation
1.22      paf       387: 
1.160.2.37.2.  (paf      388:):      String::Language set_lang(String::Language alang) {
                    389:):              String::Language result=flang;
1.39      paf       390:                flang=alang;
                    391:                return result;
                    392:        }
1.160.2.37.2.  (paf      393:):      void restore_lang(String::Language alang) {
1.39      paf       394:                flang=alang;
                    395:        }
                    396: 
1.117     paf       397: private: // connection manipulation
                    398: 
1.160.2.37.2.  (paf      399:):      SQL_Connection* set_connection(SQL_Connection* aconnection) {
                    400:):              SQL_Connection* result=fconnection;
1.117     paf       401:                fconnection=aconnection;
                    402:                return result;
                    403:        }
1.160.2.37.2.  (paf      404:):      void restore_connection(SQL_Connection* aconnection) {
1.117     paf       405:                fconnection=aconnection;
                    406:        }
                    407: 
                    408: private:
                    409: 
1.160.2.37.2.  (paf      410:):      void output_result(VFile* body_file, bool header_only, bool as_attachment);
1.148     paf       411: };
                    412: 
                    413: /// Auto-object used to save request context across ^try body
                    414: class Request_context_saver {
                    415:        Request& fr;
                    416: 
                    417:        /// exception stack trace
1.160.2.37.2.  9(paf     418:3):     size_t exception_trace;
1.148     paf       419:        /// execution stack
1.160.2.37.2.  8(paf     420:3):     size_t stack;
1.148     paf       421:        /// contexts
1.160.2.37.2.  8(paf     422:3):     VMethodFrame* method_frame;
                    423:3):     Value* rcontext;
                    424:3):     WContext* wcontext;
1.148     paf       425:        /// current language
1.160.2.37.2.  (paf      426:):      String::Language flang; 
1.148     paf       427:        /// current connection
1.160.2.37.2.  (paf      428:):      SQL_Connection* fconnection;
1.126     paf       429: 
1.148     paf       430: public:
                    431:        Request_context_saver(Request& ar) : 
1.160.2.37.2.  9(paf     432:3):             exception_trace(ar.exception_trace.top_index()),        
1.148     paf       433:                stack(ar.stack.top_index()),
                    434:                method_frame(ar.method_frame),
                    435:                rcontext(ar.rcontext),
                    436:                wcontext(ar.wcontext),
                    437:                flang(ar.flang),
                    438:                fconnection(ar.fconnection),
                    439:                fr(ar) {}
1.153     paf       440:        void restore() {
1.160.2.37.2.  9(paf     441:3):             fr.exception_trace.top_index(exception_trace);
1.148     paf       442:                fr.stack.top_index(stack);
1.157     paf       443:                fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148     paf       444:                fr.flang=flang;
                    445:                fr.fconnection=fconnection;
                    446:        }
1.39      paf       447: };
                    448: 
1.61      paf       449: ///    Auto-object used for temporary changing Request::flang.
1.39      paf       450: class Temp_lang {
                    451:        Request& frequest;
1.160.2.37.2.  (paf      452:):      String::Language saved_lang;
1.39      paf       453: public:
1.160.2.37.2.  (paf      454:):      Temp_lang(Request& arequest, String::Language alang) : 
1.39      paf       455:                frequest(arequest),
                    456:                saved_lang(arequest.set_lang(alang)) {
                    457:        }
                    458:        ~Temp_lang() { 
                    459:                frequest.restore_lang(saved_lang); 
1.117     paf       460:        }
                    461: };
                    462: 
                    463: ///    Auto-object used for temporary changing Request::fconnection.
                    464: class Temp_connection {
                    465:        Request& frequest;
1.160.2.37.2.  (paf      466:):      SQL_Connection* saved_connection;
1.117     paf       467: public:
1.160.2.37.2.  (paf      468:):      Temp_connection(Request& arequest, SQL_Connection* aconnection) : 
1.117     paf       469:                frequest(arequest),
                    470:                saved_connection(arequest.set_connection(aconnection)) {
                    471:        }
                    472:        ~Temp_connection() { 
                    473:                frequest.restore_connection(saved_connection); 
1.39      paf       474:        }
1.4       paf       475: };
1.160.2.7  paf       476: 
1.160.2.15  paf       477: 
                    478: // defines for externs
                    479: 
                    480: #define CONTENT_DISPOSITION_NAME "content-disposition"
                    481: #define CONTENT_DISPOSITION_VALUE "attachment"
                    482: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
                    483: 
1.160.2.7  paf       484: // externs
                    485: 
1.160.2.37.2.  (paf      486:): extern const String main_method_name;
                    487:): extern const String auto_method_name;
                    488:): extern const String body_name;
                    489:): extern const String content_disposition_name;
                    490:): extern const String content_disposition_value;
                    491:): extern const String content_disposition_filename_name;
          0(paf     492:3): 
                    493:3): extern const String exception_type_part_name;
                    494:3): extern const String exception_source_part_name;
                    495:3): extern const String exception_comment_part_name;
                    496:3): extern const String exception_handled_part_name;
1.160.2.19  paf       497: 
                    498: // defines for statics
                    499: 
                    500: #define MAIN_CLASS_NAME "MAIN"
1.160.2.21  paf       501: #define AUTO_FILE_NAME "auto.p"
1.1       paf       502: 
                    503: #endif

E-mail: