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

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

E-mail: