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

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

E-mail: