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

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

E-mail: