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

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.12! paf        11: static const char* IDENT_REQUEST_H="$Date: 2003/01/31 16:05:57 $";
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;
        !            94: 
        !            95:                /// needed to fill unused Array entries
        !            96:                StackItem() {}
        !            97:                StackItem(StringPtr astring): string(astring) {}
        !            98:                StackItem(ValuePtr avalue): value(avalue) {}            
        !            99:                StackItem(ArrayOperationPtr aops): ops(aops) {}
        !           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
                    112:        uchar flang; 
                    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.8  paf       130:        Request(SAPI_Info& asapi_info, Request_info& arequest_info,
1.111     paf       131:                uchar 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.160.2.10  paf       277:        /// $env:fields
                    278:        //VEnv env;
1.108     paf       279:        /// $status:fields
1.160.2.10  paf       280:        //VStatus status;
1.86      paf       281:        /// $form:elements
1.160.2.10  paf       282:        VFormPtr form;
1.140     paf       283:        /// $mail
1.160.2.10  paf       284:        VMailPtr mail;
1.90      parser    285:        /// $math:constants
1.160.2.10  paf       286:        //VMath math;
1.86      paf       287:        /// $request:elements
1.160.2.10  paf       288:        //VRequest request;
1.86      paf       289:        /// $response:elements
1.160.2.10  paf       290:        VResponsePtr response;
1.86      paf       291:        /// $cookie:elements
1.160.2.10  paf       292:        VCookiePtr cookie;
1.70      paf       293: 
1.148     paf       294:        /// classes configured data
1.160.2.6  paf       295:        HashStringValue classes_conf;
1.85      paf       296: 
1.148     paf       297: public: // status read methods
1.76      paf       298: 
1.148     paf       299:        VMethodFrame *get_method_frame() { return method_frame; }
1.157     paf       300:        Value *get_self();
1.87      paf       301: 
1.136     paf       302: private:
                    303: 
                    304:        /// already executed some @conf method
                    305:        bool configure_admin_done;
                    306: 
1.160.2.10  paf       307:        void configure_admin(VStateless_class& conf_class, StringPtr source);
1.99      parser    308: 
1.7       paf       309: private: // compile.C
                    310: 
1.46      paf       311:        VStateless_class& real_compile(COMPILE_PARAMS);
1.7       paf       312: 
                    313: private: // execute.C
                    314: 
1.139     paf       315:        /// for @postprocess[body]
1.160.2.10  paf       316:        StringPtr execute_method(VMethodFrame& amethodFrame, const Method& method);
1.139     paf       317:        //{ for @conf[filespec] and @auto[filespec]
1.160.2.10  paf       318:        StringPtr execute_method(Value& aself, 
                    319:                const Method& method, VStringPtr optional_param,
                    320:                bool do_return_string);
                    321:        StringPtr execute_nonvirtual_method(VStateless_class& aclass, 
                    322:                StringPtr method_name, VStringPtr optional_param,
                    323:                bool do_return_string,
1.137     paf       324:                const Method **return_method=0);
1.139     paf       325:        //}
                    326:        /// for @main[]
1.160.2.10  paf       327:        StringPtr execute_virtual_method(Value& aself, StringPtr method_name);
1.9       paf       328: 
1.160.2.10  paf       329:        Value *get_element(StringPtr & remember_name, bool can_call_operator);
1.22      paf       330: 
1.58      paf       331: private: // defaults
                    332: 
1.111     paf       333:        const uchar fdefault_lang;
1.68      paf       334:        Value *default_content_type;
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.111     paf       407:        uchar saved_lang;
1.39      paf       408: public:
1.111     paf       409:        Temp_lang(Request& arequest, uchar 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: 
                    432: // externs
                    433: 
                    434: extern StringPtr main_method_name;
1.1       paf       435: 
                    436: #endif

E-mail: