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

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

E-mail: