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

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

E-mail: