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

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

E-mail: