Annotation of parser3/src/main/pa_request.C, revision 1.161

1.54      paf         1: /** @file
1.55      paf         2:        Parser: request class main part. @see compile.C and execute.C.
                      3: 
1.9       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.156     parser      5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.55      paf         6: 
1.161   ! parser      7:        $Id: pa_request.C,v 1.160 2001/09/30 12:27:59 parser Exp $
1.1       paf         8: */
                      9: 
1.70      paf        10: #include "pa_config_includes.h"
1.19      paf        11: 
1.117     paf        12: #include "pcre.h"
                     13: #include "internal.h"
1.132     parser     14: extern "C" unsigned char pcre_default_tables[]; // pcre/chartables.c
1.96      paf        15: 
1.69      paf        16: #include "pa_sapi.h"
1.53      paf        17: #include "pa_common.h"
1.1       paf        18: #include "pa_request.h"
1.3       paf        19: #include "pa_wwrapper.h"
                     20: #include "pa_vclass.h"
1.31      paf        21: #include "pa_globals.h"
1.30      paf        22: #include "pa_vint.h"
1.112     paf        23: #include "pa_vmethod_frame.h"
1.32      paf        24: #include "pa_types.h"
1.78      paf        25: #include "pa_vtable.h"
1.90      paf        26: #include "pa_vfile.h"
1.147     parser     27: #include "pa_dictionary.h"
1.3       paf        28: 
1.159     parser     29: #ifdef XML
                     30: #      include <util/XercesDefs.hpp>
                     31: #      include <util/TransENameMap.hpp>
                     32: #      include <util/XML256TableTranscoder.hpp>
                     33: #      include <util/PlatformUtils.hpp>
1.161   ! parser     34: #      include <PlatformSupport/XalanTranscodingServices.hpp>
1.159     parser     35: #endif
                     36: 
1.82      paf        37: /// content type of exception response, when no @MAIN:exception handler defined
                     38: const char *UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain";
                     39: 
                     40: /// content type of response when no $MAIN:defaults.content-type defined
                     41: const char *DEFAULT_CONTENT_TYPE="text/html";
1.144     parser     42: const char *ORIGINS_CONTENT_TYPE="text/plain";
1.82      paf        43: 
1.123     paf        44: Methoded *MOP_create(Pool&);
                     45: 
1.117     paf        46: 
1.158     parser     47: inline void prepare_case_tables(unsigned char *tables) {
                     48:        unsigned char *lcc_table=tables+lcc_offset;
                     49:        unsigned char *fcc_table=tables+fcc_offset;
                     50:        for(int i=0; i<0x100; i++)
                     51:                lcc_table[i]=fcc_table[i]=i;
1.117     paf        52: }
1.158     parser     53: inline void cstr2ctypes(unsigned char *tables, const unsigned char *cstr, 
1.117     paf        54:                                                unsigned char bit) {
                     55:        unsigned char *ctypes_table=tables+ctypes_offset;
                     56:        ctypes_table[0]=bit;
                     57:        for(; *cstr; cstr++) {
                     58:                unsigned char c=*cstr;
                     59:                ctypes_table[c]|=bit;
                     60:        }
                     61: }
1.158     parser     62: inline unsigned int to_wchar_code(const String *s) {
                     63:        if(!s || s->size()==0)
                     64:                return 0;
                     65:        if(s->size()==1)
                     66:                return (unsigned int)s->first_char();
                     67:        return (unsigned int)s->as_int();
                     68: }
                     69: inline bool to_bool(const String *s) {
                     70:        return s && s->size()!=0;
                     71: }
                     72: static void element2ctypes(unsigned char c, bool belongs,
                     73:                                                   unsigned char *tables,  unsigned char bit, int group_offset=-1) {
                     74:        if(!belongs)
                     75:                return;
                     76: 
                     77:        unsigned char *ctypes_table=tables+ctypes_offset;
                     78: 
                     79:        ctypes_table[c]|=bit;
                     80:        if(group_offset>=0)
                     81:                tables[cbits_offset+group_offset+c/8] |= 1 << (c%8);
                     82: }
                     83: static void element2case(unsigned char from, unsigned char to,
                     84:                                                 unsigned char *tables) {
                     85:        if(!to) 
                     86:                return;
                     87: 
1.117     paf        88:        unsigned char *lcc_table=tables+lcc_offset;
                     89:        unsigned char *fcc_table=tables+fcc_offset;
1.158     parser     90:        lcc_table[from]=to;
                     91:        fcc_table[from]=to; fcc_table[to]=from;
1.117     paf        92: }
                     93: 
1.159     parser     94: #ifndef DOXYGEN
1.160     parser     95: struct Charset_tables {
1.159     parser     96:        // pcre_tables
                     97:        unsigned char *pcre_tables;
                     98: #ifdef XML
                     99:        // transcoder
                    100:        XMLCh *fromTable;
                    101:        XMLTransService::TransRec *toTable;
                    102:        unsigned int toTableSz;
                    103: #endif
                    104: };
                    105: #endif
                    106: 
1.160     parser    107: static void ctype_table_row_to_charset_tables(Array::Item *value, void *info) {
1.158     parser    108:        Array& row=*static_cast<Array *>(value);
1.160     parser    109:        Charset_tables& tables=*static_cast<Charset_tables *>(info);
1.158     parser    110:        
                    111: // char        white-space     digit   hex-digit       letter  word    lowercase       unicode1        unicode2        
                    112:        unsigned int c=to_wchar_code(row.get_string(0));
                    113:        
1.159     parser    114:        // pcre_tables
                    115:        element2ctypes(c, to_bool(row.get_string(1)), tables.pcre_tables, ctype_space, cbit_space);
                    116:        element2ctypes(c, to_bool(row.get_string(2)), tables.pcre_tables, ctype_digit, cbit_digit);
                    117:        element2ctypes(c, to_bool(row.get_string(3)), tables.pcre_tables, ctype_xdigit);
                    118:        element2ctypes(c, to_bool(row.get_string(4)), tables.pcre_tables, ctype_letter);
                    119:        element2ctypes(c, to_bool(row.get_string(5)), tables.pcre_tables, ctype_word, cbit_word);
                    120:        element2case(c, to_wchar_code(row.get_string(6)), tables.pcre_tables);
                    121: 
                    122: #ifdef XML
                    123:        XMLCh unicode1=row.size()>7?(XMLCh)to_wchar_code(row.get_string(7)):0;
                    124:        XMLCh unicode2=row.size()>8?(XMLCh)to_wchar_code(row.get_string(8)):0;
                    125:        tables.toTable[tables.toTableSz].intCh=unicode1?unicode1:(XMLCh)c;
                    126:        tables.toTable[tables.toTableSz].extCh=(XMLByte)c;
                    127:        tables.toTableSz++;
                    128:        if(unicode2) {
                    129:                tables.toTable[tables.toTableSz].intCh=unicode2;
                    130:                tables.toTable[tables.toTableSz].extCh=(XMLByte)c;
                    131:                tables.toTableSz++;
                    132:        }
                    133: #endif
                    134: }
                    135: static int sort_cmp_Trans_rec_intCh(const void *a, const void *b) {
                    136:        const XMLCh ca=static_cast<const XMLTransService::TransRec *>(a)->intCh;
                    137:        const XMLCh cb=static_cast<const XMLTransService::TransRec *>(b)->intCh;
                    138:        // move zeros to end of table
                    139:        if(ca==0)
                    140:                return +1;
                    141:        if(cb==0)
                    142:                return -1;
                    143: 
                    144:        return ca-cb;
1.158     parser    145: }
                    146: 
1.159     parser    147: 
                    148: #ifdef XML
                    149: template <class TType> class ENameMapFor2 : public ENameMap
                    150: {
                    151: public :
                    152:     // -----------------------------------------------------------------------
                    153:     //  Constructors and Destructor
                    154:     // -----------------------------------------------------------------------
                    155:     ENameMapFor2(
                    156:                const XMLCh* const encodingName
                    157:         , const XMLCh* const                        fromTable
                    158:         , const XMLTransService::TransRec* const    toTable
                    159:         , const unsigned int                        toTableSize
                    160:                ) : ENameMap(encodingName),
                    161:                ffromTable(fromTable),
                    162:                ftoTable(toTable),
                    163:                ftoTableSize(toTableSize) {}
                    164:     ~ENameMapFor2() {}
                    165: 
                    166:     // -----------------------------------------------------------------------
                    167:     //  Implementation of virtual factory method
                    168:     // -----------------------------------------------------------------------
                    169:     virtual XMLTranscoder* makeNew(const unsigned int blockSize) const {
                    170:                return new TType(
                    171:                        getKey(), 
                    172:                        blockSize,
                    173:                        ffromTable,
                    174:                        ftoTable, ftoTableSize);
                    175:        }
                    176: private:
                    177:        const XMLCh* const                        ffromTable;
                    178:        const XMLTransService::TransRec* const    ftoTable;
                    179:        const unsigned int                        ftoTableSize;
                    180: 
                    181: private :
                    182:     // -----------------------------------------------------------------------
                    183:     //  Unimplemented constructors and operators
                    184:     // -----------------------------------------------------------------------
                    185:     ENameMapFor2();
                    186:     ENameMapFor2(const ENameMapFor2<TType>&);
                    187:     void operator=(const ENameMapFor2<TType>&);
                    188: };
                    189: 
                    190: class XML256TableTranscoder2 : public XML256TableTranscoder
                    191: {
                    192: public :
                    193:     XML256TableTranscoder2(
                    194:         const   XMLCh* const                        encodingName
                    195:         , const unsigned int                        blockSize
                    196:         , const XMLCh* const                        fromTable
                    197:         , const XMLTransService::TransRec* const    toTable
                    198:         , const unsigned int                        toTableSize
                    199:                ) : XML256TableTranscoder(encodingName, blockSize, fromTable, toTable, toTableSize) {}
                    200: 
                    201: private :
                    202:     XML256TableTranscoder2();
                    203:     XML256TableTranscoder2(const XML256TableTranscoder2&);
                    204:     void operator=(const XML256TableTranscoder2&);
                    205: };
                    206: #endif
                    207: 
1.161   ! parser    208: #ifdef XALAN_HACK_DIGITAL_ENTITIES
        !           209: static void hack_s_maximumCharacterValues(const XalanDOMString& encoding) {
        !           210: /*
        !           211:        open:
        !           212:                xml-xalan/c/src/PlatformSupport/XalanTranscodingServices.hpp 
        !           213:        find: 
        !           214:                static const MaximumCharacterValueMapType&      s_maximumCharacterValues;
        !           215:        paste to next line:
        !           216:                friend static void hack_s_maximumCharacterValues(const XalanDOMString& encoding); // hack by paf
        !           217: */
        !           218: 
        !           219:        /*
        !           220:        const_cast<XalanTranscodingServices::MaximumCharacterValueMapType &>(
        !           221:                XalanTranscodingServices::s_maximumCharacterValues).insert(
        !           222:                XalanTranscodingServices::MaximumCharacterValueMapType::value_type(encoding, 0xFFFF));
        !           223:                */
        !           224: }
        !           225: #endif
        !           226: 
1.160     parser    227: static void load_charset(const Hash::Key& akey, Hash::Val *avalue, 
1.158     parser    228:                                                                                  void *info) {
                    229:        Hash& CTYPE=*static_cast<Hash *>(info);
                    230:        Pool& pool=CTYPE.pool();
                    231: 
1.160     parser    232:        Charset_tables tables={
1.159     parser    233:                // pcre_tables
                    234:                // lowcase, flipcase, bits digit+word+whitespace, masks
                    235:                (unsigned char *)pool.calloc(tables_length) // pcre_tables
                    236: #ifdef XML
                    237:                // transcoder
                    238:                , (XMLCh *)pool.calloc(sizeof(XMLCh)*0x100) // fromTable
                    239:                , 0 // toTable
                    240:                , 0 // toTableSz
                    241: #endif
                    242:        };
                    243:        prepare_case_tables(tables.pcre_tables);
                    244:        cstr2ctypes(tables.pcre_tables, (const unsigned char *)"*+?{^.$|()[", ctype_meta);
1.158     parser    245: 
1.159     parser    246:        // fill tables
1.158     parser    247:        Value& value=*static_cast<Value *>(avalue);
                    248:        if(Table *table=value.get_table()) {
1.159     parser    249: #ifdef XML
                    250:                tables.toTable=(XMLTransService::TransRec *)pool.calloc(
                    251:                        sizeof(XMLTransService::TransRec)*table->size()*2);
                    252: #endif
1.160     parser    253:                table->for_each(ctype_table_row_to_charset_tables, &tables);
1.159     parser    254: #ifdef XML
                    255:                // sort by the Unicode code point
                    256:                _qsort(tables.toTable, tables.toTableSz, sizeof(*tables.toTable), 
                    257:                        sort_cmp_Trans_rec_intCh);
                    258: #endif
                    259:        } else
1.158     parser    260:                PTHROW(0, 0,
                    261:                        &value.name(),
                    262:                        "must be hash");
                    263: 
1.159     parser    264:        // charset->pcre_tables 
                    265:        CTYPE.put(akey, tables.pcre_tables);
                    266: 
                    267: #ifdef XML
                    268:        // charset->transcoder
1.161   ! parser    269:        XalanDOMString sencoding(akey.cstr());
        !           270: #ifdef XALAN_HACK_DIGITAL_ENTITIES
        !           271:        hack_s_maximumCharacterValues(sencoding);
        !           272: #endif
        !           273:        const XMLCh* const auto_encoding_cstr=sencoding.c_str();
        !           274:        int size=sizeof(XMLCh)*(sencoding.size()+1);
1.159     parser    275:        XMLCh* pool_encoding_cstr=(XMLCh*)malloc(size);
                    276:        memcpy(pool_encoding_cstr, auto_encoding_cstr, size);
                    277:     XMLString::upperCase(pool_encoding_cstr);
                    278: 
                    279:     XMLPlatformUtils::fgTransService->addEncoding(
                    280:                pool_encoding_cstr, 
                    281:                new ENameMapFor2<XML256TableTranscoder2>(
                    282:                        pool_encoding_cstr
                    283:                        , tables.fromTable
                    284:                        , tables.toTable
                    285:                        , tables.toTableSz
                    286:                ));
                    287: #endif
1.157     parser    288: }
                    289: 
                    290: //
                    291: Request::Request(Pool& apool,
                    292:                                 Info& ainfo,
                    293:                                 String::Untaint_lang adefault_lang) : Pooled(apool),
                    294:        stack(apool),
                    295:        OP(*MOP_create(apool)),
                    296:        env(apool),
                    297:        form(apool),
                    298:        math(apool),
                    299:        request(apool, *this),
                    300:        response(apool),
                    301:        cookie(apool),
                    302:        fclasses(apool),
1.158     parser    303:        CTYPE(apool),
1.157     parser    304:        fdefault_lang(adefault_lang), flang(adefault_lang),
                    305:        info(ainfo),
                    306:        post_data(0), post_size(0),
                    307:        used_files(apool),
                    308:        default_content_type(0),
                    309:        mime_types(0),
                    310:        main_class(0),
                    311:        connection(0),
                    312:        classes_conf(apool),
1.158     parser    313:        anti_endless_execute_recoursion(0)      
1.157     parser    314: {
                    315:        /// directly used
                    316:        // operators
                    317:        OP.register_directly_used(*this);
                    318:        // classes:
                    319:        // table, file, random, mail, image, ...
                    320:        methoded_array->register_directly_used(*this);
                    321: 
                    322:        /// methodless
                    323:        // env class
                    324:        classes().put(*NEW String(pool(), ENV_CLASS_NAME), &env);
                    325:        // request class
                    326:        classes().put(*NEW String(pool(), REQUEST_CLASS_NAME), &request);       
                    327:        // cookie class
                    328:        classes().put(*NEW String(pool(), COOKIE_CLASS_NAME), &cookie);
                    329: 
                    330:        /// methoded
                    331:        // response class
                    332:        classes().put(response.get_class()->name(), &response); 
                    333: 
                    334:        /// bases used
                    335:        // form class
                    336:        classes().put(form.get_class()->base()->name(), &form); 
                    337:        // math class
                    338:        classes().put(math.get_class()->base()->name(), &math); 
1.117     paf       339: }
1.150     parser    340: 
1.64      paf       341: /**
                    342:        load MAIN class, execute @main.
                    343:        MAIN class consists of all the auto.p files we'd manage to find
                    344:        plus
                    345:        the file user requested us to process
                    346:        all located classes become children of one another,
                    347:        composing class we name 'MAIN'
                    348: */
1.153     parser    349: void Request::core(
                    350:                                   const char *root_config_filespec, bool root_config_fail_on_read_problem,
                    351:                                   const char *site_config_filespec, bool site_config_fail_on_read_problem,
1.62      paf       352:                                   bool header_only) {
1.121     paf       353:        //_asm { int 3 }
1.41      paf       354:        bool need_rethrow=false;  Exception rethrow_me;
1.3       paf       355:        TRY {
1.29      paf       356:                char *auto_filespec=(char *)malloc(MAX_STRING);
                    357:                
1.153     parser    358:                // loading root config
                    359:                if(root_config_filespec) {
1.77      paf       360:                        String& filespec=*NEW String(pool());
1.153     parser    361:                        filespec.APPEND_CLEAN(root_config_filespec, 0, "root_config", 0);
1.29      paf       362:                        main_class=use_file(
1.148     parser    363:                                filespec, 
1.153     parser    364:                                true/*ignore class_path*/, root_config_fail_on_read_problem,
1.37      paf       365:                                main_class_name, main_class);
1.29      paf       366:                }
                    367: 
1.124     paf       368:                // configure root options
1.72      paf       369:                //      until someone with less privileges have overriden them
1.129     paf       370:                OP.configure_admin(*this);
1.124     paf       371:                methoded_array->configure_admin(*this);
1.72      paf       372: 
1.153     parser    373:                // loading site config
                    374:                if(site_config_filespec) {
1.77      paf       375:                        String& filespec=*NEW String(pool());
1.153     parser    376:                        filespec.APPEND_CLEAN(site_config_filespec, 0, "site_config", 0);
1.37      paf       377:                        main_class=use_file(
1.148     parser    378:                                filespec, 
1.153     parser    379:                                true/*ignore class_path*/, site_config_fail_on_read_problem,
1.37      paf       380:                                main_class_name, main_class);
1.29      paf       381:                }
1.8       paf       382: 
1.72      paf       383:                // loading auto.p files from document_root/.. 
                    384:                // to the one beside requested file.
                    385:                // all assigned bases from upper dir
                    386:                {
1.115     paf       387:                        const char *after=info.path_translated;
                    388:                        size_t drlen=strlen(info.document_root);
                    389:                        if(memcmp(after, info.document_root, drlen)==0) {
                    390:                                after+=drlen;
                    391:                                if(after[-1]=='/') 
                    392:                                        --after;
                    393:                        }
                    394:                        
1.152     parser    395:                        int step=0;
1.115     paf       396:                        while(const char *before=strchr(after, '/')) {
1.152     parser    397:                                String& sfile_spec=*NEW String(pool());
1.115     paf       398:                                if(after!=info.path_translated) {
1.152     parser    399:                                        sfile_spec.APPEND_CLEAN(
                    400:                                                info.path_translated, before-info.path_translated,
                    401:                                                "path-translated-scanned", step++);
                    402:                                        sfile_spec << "/" AUTO_FILE_NAME;
                    403: 
                    404:                                        main_class=use_file(sfile_spec, 
                    405:                                                true/*ignore class_path*/, false/*ignore read problem*/,
                    406:                                                main_class_name, main_class);
1.115     paf       407:                                }
                    408:                                after=before+1;
1.72      paf       409:                        }
                    410:                }
1.34      paf       411: 
1.125     paf       412:                // compile requested file
1.77      paf       413:                String& spath_translated=*NEW String(pool());
1.136     parser    414:                spath_translated.APPEND_TAINTED(info.path_translated, 0, "user-request", 0);
1.148     parser    415:                main_class=use_file(spath_translated, 
                    416:                        true/*ignore class_path*/, true/*don't ignore read problem*/,
1.72      paf       417:                        main_class_name, main_class);
1.7       paf       418: 
1.124     paf       419:                // configure not-root=user options
1.129     paf       420:                OP.configure_user(*this);
1.124     paf       421:                methoded_array->configure_user(*this);
                    422: 
1.85      paf       423:                // $MAIN:DEFAULTS
1.78      paf       424:                Value *defaults=main_class->get_element(*defaults_name);
1.75      paf       425:                // value must be allocated on request's pool for that pool used on
                    426:                // meaning constructing @see attributed_meaning_to_string
1.80      paf       427:                default_content_type=defaults?defaults->get_element(*content_type_name):0;
1.155     parser    428: #ifdef XML
                    429:                // record default charset
1.154     parser    430:                if(default_content_type)
                    431:                        if(Hash *hash=default_content_type->get_hash())
                    432:                                if(Value *vcharset=(Value *)hash->get(*charset_name))
                    433:                                        pool().set_charset(vcharset->as_string());              
1.155     parser    434: #endif
1.154     parser    435: 
1.117     paf       436:                if(Value *element=main_class->get_element(*user_html_name))
1.87      paf       437:                        if(Table *table=element->get_table())
1.147     parser    438:                                pool().set_tag(NEW Dictionary(*table));
1.85      paf       439: 
                    440:                // $MAIN:MIME-TYPES
                    441:                if(Value *element=main_class->get_element(*mime_types_name))
                    442:                        if(Table *table=element->get_table())
                    443:                                mime_types=table;                       
1.96      paf       444: 
1.160     parser    445:                if(Value *vcharsets=main_class->get_element(*charsets_name)) {
                    446:                        if(Hash *charsets=vcharsets->get_hash())
                    447:                                charsets->for_each(load_charset, &CTYPE);
1.158     parser    448:                        else
                    449:                                THROW(0, 0,
1.160     parser    450:                                        &vcharsets->name(),
1.158     parser    451:                                        "must be hash");
                    452:                }
1.102     paf       453: 
1.124     paf       454:                // filling form fields
1.153     parser    455:                form.fill_fields_and_tables(*this);
1.102     paf       456: 
1.124     paf       457:                // filling cookies
                    458:                cookie.fill_fields(*this);
1.25      paf       459: 
                    460:                // execute @main[]
1.143     parser    461:                const String *body_string=execute_virtual_method(
                    462:                        *main_class, *main_method_name);
1.41      paf       463:                if(!body_string)
1.25      paf       464:                        THROW(0,0,
1.145     parser    465:                                0, 
                    466:                                "'"MAIN_METHOD_NAME"' method not found");
1.79      paf       467: 
1.91      paf       468:                VString body_vstring_before_post_process(*body_string);
                    469:                VString *body_vstring_after_post_process=&body_vstring_before_post_process;
                    470:                
1.119     paf       471:                // @postprocess
1.91      paf       472:                if(Value *value=main_class->get_element(*post_process_method_name))
                    473:                        if(Junction *junction=value->get_junction())
                    474:                                if(const Method *method=junction->method) {
                    475:                                        // preparing to pass parameters to 
1.119     paf       476:                                        //      @postprocess[data]
1.146     parser    477:                                        VMethodFrame frame(pool(), value->name(), *junction);
1.91      paf       478:                                        frame.set_self(*main_class);
                    479: 
                    480:                                        frame.store_param(method->name, 
                    481:                                                &body_vstring_before_post_process);
                    482:                                        body_vstring_after_post_process=
                    483:                                                NEW VString(*execute_method(frame, *method));
                    484:                                }
1.90      paf       485: 
1.144     parser    486:                bool origins_mode=main_class->get_element(*origins_mode_name)!=0;
                    487: 
                    488:                const VFile *body_file=body_vstring_after_post_process->as_vfile(
                    489:                        String::UL_UNSPECIFIED, origins_mode);
1.41      paf       490: 
1.45      paf       491:                // extract response body
                    492:                Value *body_value=static_cast<Value *>(
                    493:                        response.fields().get(*body_name));
1.119     paf       494:                if(body_value) // there is some $response.body
1.90      paf       495:                        body_file=body_value->as_vfile();
1.144     parser    496:                else if(origins_mode)
                    497:                        response.fields().put(*content_type_name, 
                    498:                                NEW VString(*NEW String(pool(), ORIGINS_CONTENT_TYPE)));
1.45      paf       499: 
                    500:                // OK. write out the result
1.90      paf       501:                output_result(*body_file, header_only);
1.3       paf       502:        } 
1.76      paf       503:        CATCH(e) { // request handling problem
                    504:                // we're returning not result, but error explanation
1.30      paf       505:                TRY {
1.76      paf       506:                        // log the beast
                    507:                        const String *problem_source=e.problem_source();
1.114     paf       508:                        if(problem_source && problem_source->size())
1.76      paf       509:                                SAPI::log(pool(),
                    510: #ifndef NO_STRING_ORIGIN
                    511:                                        "%s(%d): "
                    512: #endif
                    513:                                        "'%s' %s [%s %s]",
                    514: #ifndef NO_STRING_ORIGIN
                    515:                                        problem_source->origin().file?problem_source->origin().file:"global",
                    516:                                        problem_source->origin().line,
                    517: #endif
1.99      paf       518:                                        problem_source->cstr(String::UL_AS_IS),
1.76      paf       519:                                        e.comment(),
1.99      paf       520:                                        e.type()?e.type()->cstr(String::UL_AS_IS):"-",
                    521:                                        e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76      paf       522:                                );
                    523:                        else
                    524:                                SAPI::log(pool(),
                    525:                                        "%s [%s %s]",
                    526:                                        e.comment(),
1.99      paf       527:                                        e.type()?e.type()->cstr(String::UL_AS_IS):"-",
                    528:                                        e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76      paf       529:                                        );
1.43      paf       530: 
                    531:                        // reset language to default
                    532:                        flang=fdefault_lang;
1.135     parser    533:                        if(flang==String::UL_USER_HTML)
1.137     parser    534:                                flang=String::UL_HTML; // no _ & Co conversions in @exception[params]
1.43      paf       535:                        
                    536:                        // reset response
                    537:                        response.fields().clear();
                    538: 
                    539:                        // this is what we'd return in $response:body
1.41      paf       540:                        const String *body_string=0;
1.43      paf       541: 
1.30      paf       542:                        if(main_class) { // we've managed to end up with some main_class
                    543:                                // maybe we'd be lucky enough as to report an error
                    544:                                // in a gracefull way...
                    545:                                if(Value *value=main_class->get_element(*exception_method_name))
                    546:                                        if(Junction *junction=value->get_junction())
                    547:                                                if(const Method *method=junction->method) {
                    548:                                                        // preparing to pass parameters to 
                    549:                                                        //      @exception[origin;source;comment;type;code]
1.146     parser    550:                                                        VMethodFrame frame(pool(), value->name(), *junction);
1.38      paf       551:                                                        frame.set_self(*main_class);
1.30      paf       552: 
                    553:                                                        const String *problem_source=e.problem_source();
                    554:                                                        // origin
1.38      paf       555:                                                        Value *origin_value=0;
1.30      paf       556: #ifndef NO_STRING_ORIGIN
1.114     paf       557:                                                        if(problem_source && problem_source->size()) {
1.38      paf       558:                                                                const Origin& origin=problem_source->origin();
                    559:                                                                if(origin.file) {
                    560:                                                                        char *buf=(char *)malloc(MAX_STRING);
1.106     paf       561:                                                                        size_t buf_size=snprintf(buf, MAX_STRING, "%s(%d):", 
1.38      paf       562:                                                                                origin.file, 1+origin.line);
1.44      paf       563:                                                                        String *origin_file_line=NEW String(pool(),
1.106     paf       564:                                                                                buf, buf_size, true);
1.38      paf       565:                                                                        origin_value=NEW VString(*origin_file_line);
                    566:                                                                }
1.30      paf       567:                                                        }
                    568: #endif
1.91      paf       569:                                                        frame.store_param(method->name, 
1.138     parser    570:                                                                origin_value?origin_value:NEW VVoid(pool()));
1.28      paf       571: 
1.30      paf       572:                                                        // source
1.38      paf       573:                                                        Value *source_value=0;
1.114     paf       574:                                                        if(problem_source && problem_source->size()) {
1.47      paf       575:                                                                String& problem_source_copy=*NEW String(pool());
                    576:                                                                problem_source_copy.append(*problem_source, 
                    577:                                                                        flang, true);
1.43      paf       578:                                                                source_value=NEW VString(problem_source_copy);
                    579:                                                        }
1.91      paf       580:                                                        frame.store_param(method->name, 
1.138     parser    581:                                                                source_value?source_value:NEW VVoid(pool()));
1.30      paf       582: 
                    583:                                                        // comment
1.44      paf       584:                                                        String *comment_value=NEW String(pool(),
1.106     paf       585:                                                                e.comment(), 0, true);
1.91      paf       586:                                                        frame.store_param(method->name, 
1.30      paf       587:                                                                NEW VString(*comment_value));
                    588: 
                    589:                                                        // type
                    590:                                                        Value *type_value;
1.43      paf       591:                                                        if(e.type()) {
1.47      paf       592:                                                                String& type_copy=*NEW String(pool());
                    593:                                                                type_value=NEW VString(type_copy.append(*e.type(), 
                    594:                                                                        flang, true));
1.43      paf       595:                                                        } else
1.138     parser    596:                                                                type_value=NEW VVoid(pool());
1.91      paf       597:                                                        frame.store_param(method->name, type_value);
1.30      paf       598: 
                    599:                                                        // code
                    600:                                                        Value *code_value;
1.43      paf       601:                                                        if(e.code()) {
1.47      paf       602:                                                                String& code_copy=*NEW String(pool());
                    603:                                                                code_value=NEW VString(code_copy.append(*e.code(), 
                    604:                                                                        flang, true));
1.43      paf       605:                                                        } else
1.138     parser    606:                                                                code_value=NEW VVoid(pool());
1.91      paf       607:                                                        frame.store_param(method->name, code_value);
1.30      paf       608: 
1.43      paf       609:                                                        // future $response:body=
                    610:                                                        //   execute ^exception[origin;source;comment;type;code]
                    611:                                                        body_string=execute_method(frame, *method);
1.30      paf       612:                                                }
                    613:                        }
                    614:                        
1.41      paf       615:                        if(!body_string) {  // couldn't report an error beautifully?
                    616:                                // doing that ugly
                    617: 
                    618:                                // make up result: $origin $source $comment $type $code
                    619:                                char *buf=(char *)malloc(MAX_STRING);
1.30      paf       620:                                size_t printed=0;
                    621:                                const String *problem_source=e.problem_source();
                    622:                                if(problem_source) {
1.16      paf       623: #ifndef NO_STRING_ORIGIN
1.30      paf       624:                                        const Origin& origin=problem_source->origin();
                    625:                                        if(origin.file)
1.41      paf       626:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ", 
1.30      paf       627:                                                origin.file, 1+origin.line);
1.28      paf       628: #endif
1.41      paf       629:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ", 
1.99      paf       630:                                                problem_source->cstr(String::UL_AS_IS));
1.30      paf       631:                                }
1.41      paf       632:                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", 
1.30      paf       633:                                        e.comment());
                    634:                                const String *type=e.type();
                    635:                                if(type) {
1.41      paf       636:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "  type: %s", 
1.99      paf       637:                                                type->cstr(String::UL_AS_IS));
1.30      paf       638:                                        const String *code=e.code();
                    639:                                        if(code)
1.41      paf       640:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s", 
1.99      paf       641:                                                code->cstr(String::UL_AS_IS));
1.30      paf       642:                                }
1.43      paf       643: 
                    644:                                // future $response:content-type
1.49      paf       645:                                response.fields().put(*content_type_name, 
1.82      paf       646:                                        NEW VString(*NEW String(pool(), UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.43      paf       647:                                // future $response:body
1.44      paf       648:                                body_string=NEW String(pool(), buf);
1.30      paf       649:                        }
1.41      paf       650: 
1.144     parser    651:                        VString body_vstring(*body_string);
1.90      paf       652:                        const VFile *body_file=body_vstring.as_vfile();
                    653: 
1.45      paf       654:                        // ERROR. write it out
1.90      paf       655:                        output_result(*body_file, header_only);
1.3       paf       656:                }
1.30      paf       657:                CATCH(e) {
1.41      paf       658:                        // exception in request exception handler
                    659:                        // remember to rethrow it
                    660:                        rethrow_me=e;  need_rethrow=true; 
1.1       paf       661:                }
1.30      paf       662:                END_CATCH
1.1       paf       663:        }
1.41      paf       664:        END_CATCH // do not use pool() after this point - no exception handler set
                    665:                  // any throw() would try to use zero exception() pointer 
                    666: 
1.91      paf       667:        if(need_rethrow) // were there an exception for us to rethrow?
1.62      paf       668:                THROW(rethrow_me.type(), rethrow_me.code(),
1.41      paf       669:                        rethrow_me.problem_source(),
                    670:                        rethrow_me.comment());
1.1       paf       671: }
                    672: 
1.148     parser    673: VStateless_class *Request::use_file(const String& file_name, 
                    674:                                                                        bool ignore_class_path, bool fail_on_read_problem,
1.26      paf       675:                                                                        const String *name, 
                    676:                                                                        VStateless_class *base_class) {
1.73      paf       677:        // cyclic dependence check
1.148     parser    678:        if(used_files.get(file_name))
1.73      paf       679:                return base_class;
1.148     parser    680:        used_files.put(file_name, (Hash::Val *)true);
1.73      paf       681: 
1.148     parser    682:        const String *file_spec;
1.153     parser    683:        if(ignore_class_path) // ignore_class_path?
1.148     parser    684:                file_spec=&file_name;
1.153     parser    685:        else if(file_name.first_char()=='/') //absolute path, no need to scan MAIN:CLASS_PATH?
                    686:                file_spec=&absolute(file_name);
                    687:        else {
                    688:                file_spec=0;
                    689:                if(main_class)
                    690:                        if(Value *element=main_class->get_element(*class_path_name)) {
                    691:                                if(element->is_string()) {
                    692:                                        file_spec=file_readable(element->as_string(), file_name); // found at class_path?
                    693:                                } else if(Table *table=element->get_table()) {
                    694:                                        int size=table->size();
                    695:                                        for(int i=size; i--; ) {
                    696:                                                const String& path=*static_cast<Array *>(table->get(i))->get_string(0);
                    697:                                                if(file_spec=file_readable(path, file_name))
                    698:                                                        break; // found along class_path
1.148     parser    699:                                        }
1.153     parser    700:                                } else
                    701:                                        THROW(0, 0,
                    702:                                                &element->name(),
                    703:                                                "must be string or table");
                    704:                                if(!file_spec)
                    705:                                        THROW(0, 0,
                    706:                                                &file_name,
                    707:                                                "not found along " MAIN_CLASS_NAME ":" CLASS_PATH_NAME);
                    708:                        }
                    709:                if(!file_spec)
                    710:                        THROW(0, 0,
                    711:                                &file_name,
                    712:                                "usage failed - no " MAIN_CLASS_NAME  ":" CLASS_PATH_NAME " were specified");
1.148     parser    713:        }
1.73      paf       714: 
1.148     parser    715:        char *source=file_read_text(pool(), *file_spec, fail_on_read_problem);
1.3       paf       716:        if(!source)
1.12      paf       717:                return base_class;
1.3       paf       718: 
1.148     parser    719:        return use_buf(source, file_spec->cstr(), 0/*new class*/, name, base_class);
1.16      paf       720: }
                    721: 
1.67      paf       722: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26      paf       723:                                                                   VStateless_class *aclass, const String *name, 
                    724:                                                                   VStateless_class *base_class) {
1.5       paf       725:        // compile loaded class
1.26      paf       726:        VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1       paf       727: 
1.4       paf       728:        // locate and execute possible @auto[] static method
1.143     parser    729:        execute_nonvirtual_method(cclass, *auto_method_name, false /*no result needed*/);
1.16      paf       730:        return &cclass;
1.19      paf       731: }
                    732: 
1.77      paf       733: const String& Request::relative(const char *apath, const String& relative_name) {
                    734:        int lpath_buf_size=strlen(apath)+1;
                    735:     char *lpath=(char *)malloc(lpath_buf_size);
                    736:        memcpy(lpath, apath, lpath_buf_size);
1.120     paf       737:     if(!rsplit(lpath, '/'))
                    738:                strcpy(lpath, ".");
1.77      paf       739:        String& result=*NEW String(pool(), lpath);
1.104     paf       740:     result << "/" << relative_name;
1.19      paf       741:     return result;
                    742: }
                    743: 
1.77      paf       744: const String& Request::absolute(const String& relative_name) {
                    745:        char *relative_name_cstr=relative_name.cstr();
                    746:        if(relative_name_cstr[0]=='/') {
                    747:                String& result=*NEW String(pool(), info.document_root);
1.104     paf       748:                result << relative_name;
1.19      paf       749:                return result;
                    750:        } else 
1.77      paf       751:                return relative(info.path_translated, relative_name);
1.1       paf       752: }
1.45      paf       753: 
1.101     paf       754: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    755:                                                                 void *info) {
                    756:        String *attribute_to_exclude=static_cast<String *>(info);
                    757:        if(aattribute==*attribute_to_exclude)
                    758:                return;
                    759: 
                    760:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    761:        Pool& pool=lmeaning.pool();
                    762: 
                    763:        SAPI::add_header_attribute(pool,
1.116     paf       764:                aattribute.cstr(String::UL_AS_IS), 
1.101     paf       765:                attributed_meaning_to_string(lmeaning, String::UL_HTTP_HEADER).cstr());
                    766: }
1.90      paf       767: void Request::output_result(const VFile& body_file, bool header_only) {
1.51      paf       768:        // header: cookies
                    769:        cookie.output_result();
                    770:        
1.90      paf       771:        // set content-type
                    772:        if(String *body_file_content_type=static_cast<String *>(
                    773:                body_file.fields().get(*vfile_mime_type_name))) {
                    774:                // body file content type
                    775:                response.fields().put(*content_type_name, body_file_content_type);
                    776:        } else {
                    777:                // default content type
                    778:                response.fields().put_dont_replace(*content_type_name, 
                    779:                        default_content_type?default_content_type
                    780:                        :NEW VString(*NEW String(pool(), DEFAULT_CONTENT_TYPE)));
1.92      paf       781:        }
                    782: 
                    783:        // content-disposition
1.111     paf       784:        if(VString *vfile_name=static_cast<VString *>(body_file.fields().get(*name_name)))
                    785:                if(vfile_name->string()!=NONAME_DAT) {
                    786:                        VHash& vhash=*NEW VHash(pool());
                    787:                        vhash.hash().put(*content_disposition_filename_name, vfile_name);
                    788:                        response.fields().put(*content_disposition_name, &vhash);
                    789:                }
1.49      paf       790: 
1.62      paf       791:        // prepare header: $response:fields without :body
1.73      paf       792:        response.fields().for_each(add_header_attribute, /*excluding*/ body_name);
1.50      paf       793: 
1.62      paf       794:        // prepare...
1.90      paf       795:        const void *body=body_file.value_ptr();
                    796:        size_t content_length=body_file.value_size();
1.50      paf       797: 
1.62      paf       798:        // prepare header: content-length
1.65      paf       799:        if(content_length) { // useful for redirecting [header "location: http://..."]
                    800:                char content_length_cstr[MAX_NUMBER];
1.108     paf       801:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.69      paf       802:                SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.65      paf       803:        }
1.62      paf       804: 
                    805:        // send header
1.69      paf       806:        SAPI::send_header(pool());
1.71      paf       807:        
1.62      paf       808:        // send body
                    809:        if(!header_only)
1.69      paf       810:                SAPI::send_body(pool(), body, content_length);
1.50      paf       811: }
1.104     paf       812: 
                    813: const String& Request::mime_type_of(const char *user_file_name_cstr) {
                    814:        if(mime_types)
                    815:                if(const char *cext=strrchr(user_file_name_cstr, '.')) {
                    816:                        String sext(pool(), ++cext);
                    817:                        if(mime_types->locate(0, sext))
                    818:                                if(const String *result=mime_types->item(1))
                    819:                                        return *result;
                    820:                                else
                    821:                                        THROW(0, 0,
                    822:                                                mime_types->origin_string(),
                    823:                                                "MIME-TYPE table column elements must not be empty");
                    824:                }
                    825:        return *NEW String(pool(), "application/octet-stream");
1.158     parser    826: }
                    827: 
                    828: unsigned char *Request::pcre_tables() {
1.159     parser    829:        if(unsigned char *result=(unsigned char *)CTYPE.get(pool().get_charset()))
                    830:                return result;
1.158     parser    831: 
                    832:        return pcre_default_tables;
1.133     parser    833: }

E-mail: