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

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.55      paf         5: 
1.14      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.9       paf         7: 
1.123   ! paf         8:        $Id: pa_request.C,v 1.122.2.4 2001/04/28 08:30:30 paf Exp $
1.1       paf         9: */
                     10: 
1.70      paf        11: #include "pa_config_includes.h"
1.19      paf        12: 
1.117     paf        13: #include "pcre.h"
                     14: #include "internal.h"
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.3       paf        27: 
1.109     paf        28: /// $LIMITS.post_max_size default 10M
1.72      paf        29: const size_t MAX_POST_SIZE_DEFAULT=10*0x400*400;
                     30: 
1.82      paf        31: /// content type of exception response, when no @MAIN:exception handler defined
                     32: const char *UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain";
                     33: 
                     34: /// content type of response when no $MAIN:defaults.content-type defined
                     35: const char *DEFAULT_CONTENT_TYPE="text/html";
                     36: 
1.123   ! paf        37: Methoded *MOP_create(Pool&);
        !            38: 
1.72      paf        39: //
1.28      paf        40: Request::Request(Pool& apool,
1.33      paf        41:                                 Info& ainfo,
1.43      paf        42:                                 String::Untaint_lang adefault_lang) : Pooled(apool),
1.3       paf        43:        stack(apool),
1.123   ! paf        44:        OP(*MOP_create(apool)),
1.41      paf        45:        env(apool),
                     46:        form(apool),
                     47:        request(apool, *this),
                     48:        response(apool),
1.51      paf        49:        cookie(apool),
1.3       paf        50:        fclasses(apool),
1.43      paf        51:        fdefault_lang(adefault_lang), flang(adefault_lang),
                     52:        info(ainfo),
1.105     paf        53:        post_data(0), post_size(0),
1.85      paf        54:        used_files(apool),
1.74      paf        55:        default_content_type(0),
1.94      paf        56:        mime_types(0),
1.103     paf        57:        connection(0), protocol2library(0),
1.117     paf        58:        mail(0), 
                     59:        pcre_tables(0)
1.3       paf        60: {
1.123   ! paf        61:        /// directly used
        !            62:        // operators
        !            63:        OP.register_directly_used(*this);
        !            64:        // classes:
        !            65:        // table, file, random, mail, image
        !            66:        methoded_array->register_directly_used(*this);
        !            67: 
        !            68:        /// methodless
1.3       paf        69:        // env class
1.123   ! paf        70:        classes().put(*NEW String(pool(), ENV_CLASS_NAME), &env);
1.40      paf        71:        // request class
1.123   ! paf        72:        classes().put(*NEW String(pool(), REQUEST_CLASS_NAME), &request);       
        !            73:        // cookie class
        !            74:        classes().put(*NEW String(pool(), COOKIE_CLASS_NAME), &cookie);
        !            75: 
        !            76:        /// methoded
1.41      paf        77:        // response class
1.123   ! paf        78:        classes().put(response.get_class()->name(), &response); 
        !            79: 
        !            80:        /// bases used
        !            81:        // form class
        !            82:        classes().put(form.get_class()->base()->name(), &form); 
1.45      paf        83: }
                     84: 
1.117     paf        85: static void element2ctypes(unsigned char *tables, 
                     86:        Value& ctype, const String& name, 
                     87:        unsigned char bit,
                     88:        int group_offset=-1,
                     89:        bool skip_ws=true) {
                     90:        Value *value=ctype.get_element(name);
                     91:        if(!value)
                     92:                return;
                     93: 
                     94:        unsigned char *ctypes_table=tables+ctypes_offset;
                     95:        const unsigned char *cstr=
                     96:                (const unsigned char *)value->as_string().cstr(String::UL_AS_IS);
                     97:        for(; *cstr; cstr++) {
                     98:                unsigned char c=*cstr;
                     99:                if(skip_ws && (c=='\n' || c=='\t' || c==' '))
                    100:                        continue;
                    101:                ctypes_table[c]|=bit;
                    102: 
                    103:                if(group_offset>=0)
1.118     paf       104:                        tables[cbits_offset+group_offset+c/8] |= 1 << (c%8);
1.117     paf       105:        }                               
                    106: }
                    107: static void cstr2ctypes(unsigned char *tables, const unsigned char *cstr, 
                    108:                                                unsigned char bit) {
                    109:        unsigned char *ctypes_table=tables+ctypes_offset;
                    110:        ctypes_table[0]=bit;
                    111:        for(; *cstr; cstr++) {
                    112:                unsigned char c=*cstr;
                    113:                ctypes_table[c]|=bit;
                    114:        }
                    115: }
                    116: static void prepare_case_tables(unsigned char *tables) {
                    117:        unsigned char *lcc_table=tables+lcc_offset;
                    118:        unsigned char *fcc_table=tables+fcc_offset;
                    119:        for(int i=0; i<0x100; i++)
                    120:                lcc_table[i]=fcc_table[i]=i;
                    121: }
                    122: static void element2case(unsigned char *tables, Value& ctype, const String& name) {
                    123:        Value *value=ctype.get_element(name);
                    124:        if(!value)
                    125:                return;
                    126: 
                    127:        unsigned char *lcc_table=tables+lcc_offset;
                    128:        unsigned char *fcc_table=tables+fcc_offset;
                    129:        const unsigned char *cstr=
                    130:                (const unsigned char *)value->as_string().cstr(String::UL_AS_IS);
                    131:        unsigned char from=0;
                    132:        for(; *cstr; cstr++) {
                    133:                unsigned char c=*cstr;
                    134:                if(c=='\n' || c=='\t' || c==' ')
                    135:                        continue;
                    136:                if(from) {
                    137:                        lcc_table[from]=c;
                    138:                        fcc_table[from]=c; fcc_table[c]=from;
                    139:                        from=0;
                    140:                } else
                    141:                        from=c;
                    142:        }
                    143: }
1.64      paf       144: /**
                    145:        load MAIN class, execute @main.
                    146:        MAIN class consists of all the auto.p files we'd manage to find
                    147:        plus
                    148:        the file user requested us to process
                    149:        all located classes become children of one another,
                    150:        composing class we name 'MAIN'
1.123   ! paf       151: 
        !           152:        @test move configure to M-class
1.64      paf       153: */
1.63      paf       154: void Request::core(const char *root_auto_path, bool root_auto_fail,
                    155:                                   const char *site_auto_path, bool site_auto_fail,
1.62      paf       156:                                   bool header_only) {
1.121     paf       157:        //_asm { int 3 }
1.29      paf       158:        VStateless_class *main_class=0;
1.41      paf       159:        bool need_rethrow=false;  Exception rethrow_me;
1.3       paf       160:        TRY {
1.29      paf       161:                char *auto_filespec=(char *)malloc(MAX_STRING);
                    162:                
1.63      paf       163:                // loading root auto.p 
                    164:                if(root_auto_path) {
1.77      paf       165:                        String& filespec=*NEW String(pool());
1.88      paf       166:                        filespec.APPEND_CLEAN(root_auto_path, 0, "root_auto", 0);
1.77      paf       167:                        filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.29      paf       168:                        main_class=use_file(
1.77      paf       169:                                filespec, root_auto_fail,
1.37      paf       170:                                main_class_name, main_class);
1.29      paf       171:                }
                    172: 
1.85      paf       173:                // $MAIN:LIMITS hash used here,
1.72      paf       174:                //      until someone with less privileges have overriden them
1.123   ! paf       175:                methoded_array->configure(*this);
1.78      paf       176:                {
                    177:                        Value *limits=main_class?main_class->get_element(*limits_name):0;
1.106     paf       178:                        if(info.method && StrEqNc(info.method, "post", true)) {
1.105     paf       179:                                // $limits.post_max_size default 10M
                    180:                                Value *element=limits?limits->get_element(*post_max_size_name):0;
                    181:                                size_t value=element?(size_t)element->as_double():0;
                    182:                                size_t post_max_size=value?value:MAX_POST_SIZE_DEFAULT;
                    183:                                
1.110     paf       184:                                if(size_t limit=max(0, min(info.content_length, post_max_size))) {
1.106     paf       185:                                        // read POST data
1.110     paf       186:                                        post_data=(char *)malloc(limit);
                    187:                                        post_size=SAPI::read_post(pool(), post_data, limit);
                    188:                                        if(post_size!=limit)
1.105     paf       189:                                                THROW(0, 0, 
                    190:                                                        0, 
1.110     paf       191:                                                        "post_size(%d)!=limit(%d)", 
                    192:                                                                post_size, limit);
1.105     paf       193:                                }
                    194:                        }
                    195: 
                    196:                        form.fill_fields(*this);
1.78      paf       197:                }
1.72      paf       198: 
1.78      paf       199:                // filling cookies
1.72      paf       200:                cookie.fill_fields(*this);
                    201: 
1.64      paf       202:                // loading site auto.p
1.63      paf       203:                if(site_auto_path) {
1.77      paf       204:                        String& filespec=*NEW String(pool());
1.88      paf       205:                        filespec.APPEND_CLEAN(site_auto_path, 0, "site_auto", 0);
1.77      paf       206:                        filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.37      paf       207:                        main_class=use_file(
1.77      paf       208:                                filespec, site_auto_fail,
1.37      paf       209:                                main_class_name, main_class);
1.29      paf       210:                }
1.8       paf       211: 
1.72      paf       212:                // loading auto.p files from document_root/.. 
                    213:                // to the one beside requested file.
                    214:                // all assigned bases from upper dir
                    215:                {
1.115     paf       216:                        Array ladder(pool());
                    217:                        const char *after=info.path_translated;
                    218:                        size_t drlen=strlen(info.document_root);
                    219:                        if(memcmp(after, info.document_root, drlen)==0) {
                    220:                                after+=drlen;
                    221:                                if(after[-1]=='/') 
                    222:                                        --after;
                    223:                        }
                    224:                        
                    225:                        while(const char *before=strchr(after, '/')) {
                    226:                                String& step=*NEW String(pool());
                    227:                                if(after!=info.path_translated) {
                    228:                                        step.APPEND_CLEAN(
                    229:                                                info.path_translated, before+1/* / */-info.path_translated,
                    230:                                                "path-translated-scanned", ladder.size());
                    231:                                        step << AUTO_FILE_NAME;
                    232:                                        ladder+=&step;
                    233:                                }
                    234:                                after=before+1;
                    235:                        }
                    236:                        for(int i=ladder.size(); --i>=0; ) {
                    237:                                const String& sfile_spec=*ladder.get_string(i);
                    238:                                main_class=use_file(sfile_spec, false/*ignore read problem*/,
                    239:                                        main_class_name, main_class);
1.72      paf       240:                        }
                    241:                }
1.34      paf       242: 
1.72      paf       243:                // compiling requested file
1.77      paf       244:                String& spath_translated=*NEW String(pool());
1.88      paf       245:                spath_translated.APPEND_CLEAN(info.path_translated, 0, "user-request", 0);
1.77      paf       246:                main_class=use_file(spath_translated, true/*don't ignore read problem*/,
1.72      paf       247:                        main_class_name, main_class);
1.7       paf       248: 
1.85      paf       249:                // $MAIN:DEFAULTS
1.78      paf       250:                Value *defaults=main_class->get_element(*defaults_name);
1.75      paf       251:                // value must be allocated on request's pool for that pool used on
                    252:                // meaning constructing @see attributed_meaning_to_string
1.80      paf       253:                default_content_type=defaults?defaults->get_element(*content_type_name):0;
1.117     paf       254:                if(Value *element=main_class->get_element(*user_html_name))
1.87      paf       255:                        if(Table *table=element->get_table())
                    256:                                pool().set_tag(table);
1.95      paf       257: 
                    258:                // $MAIN:SQL.drivers
                    259:                if(Value *sql=main_class->get_element(*main_sql_name))
                    260:                        if(Value *element=sql->get_element(*main_sql_drivers_name))
                    261:                                protocol2library=element->get_table();          
1.85      paf       262: 
                    263:                // $MAIN:MIME-TYPES
                    264:                if(Value *element=main_class->get_element(*mime_types_name))
                    265:                        if(Table *table=element->get_table())
                    266:                                mime_types=table;                       
1.96      paf       267: 
1.117     paf       268:                /*
                    269:                        $MAIN:CTYPE[
                    270:                                $white-space[
                    271:                                        ^#09^#0A^#0B^#0C^#0D^#20]
                    272:                                $digit[
                    273:                                        0123456789]
                    274:                                $hex-digit[
                    275:                                        0123456789ABCDEFabcdef]
                    276:                                $letter[
                    277:                                        ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]
                    278:                                $word[
                    279:                                        0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz]
                    280:                                $meta[
                    281:                                        ^#00$()*+.?[^{|]
                    282: 
                    283:                                $lowercase[
                    284:                                        AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz]
                    285:                        ]
                    286:                */
                    287:                if(Value *ctype=main_class->get_element(*ctype_name)) {
                    288:                        // lowcase, flipcase, bits digit+word+whitespace, masks
                    289:                        pcre_tables=(unsigned char *)calloc(tables_length);
                    290:                        prepare_case_tables(pcre_tables);
                    291: 
                    292:                        element2ctypes(pcre_tables, *ctype, *ctype_white_space_name, ctype_space, cbit_space, false);
                    293:                        element2ctypes(pcre_tables, *ctype, *ctype_digit_name, ctype_digit, cbit_digit);
                    294:                        element2ctypes(pcre_tables, *ctype, *ctype_hex_digit_name, ctype_xdigit);
                    295:                        element2ctypes(pcre_tables, *ctype, *ctype_letter_name, ctype_letter);
                    296:                        element2ctypes(pcre_tables, *ctype, *ctype_word_name, ctype_word, cbit_word);
                    297:                        cstr2ctypes(pcre_tables, (const unsigned char *)"*+?{^.$|()[", ctype_meta);
                    298: 
                    299:                        element2case(pcre_tables, *ctype, *ctype_lowercase_name);
                    300:                }
1.102     paf       301: 
                    302:                // $MAIN:MAIL[$SMTP[mail.design.ru]]
                    303:                if(Value *mail_element=main_class->get_element(*mail_name))
                    304:                        if(!(mail=mail_element->get_hash()))
                    305:                                THROW(0, 0,
                    306:                                        0,
                    307:                                        "$MAIN:MAIL is not hash");
                    308: 
1.25      paf       309: 
                    310:                // execute @main[]
1.41      paf       311:                const String *body_string=execute_method(*main_class, *main_method_name);
                    312:                if(!body_string)
1.25      paf       313:                        THROW(0,0,
                    314:                        0, 
                    315:                        "'"MAIN_METHOD_NAME"' method not found");
1.79      paf       316: 
1.91      paf       317:                VString body_vstring_before_post_process(*body_string);
                    318:                VString *body_vstring_after_post_process=&body_vstring_before_post_process;
                    319:                
1.119     paf       320:                // @postprocess
1.91      paf       321:                if(Value *value=main_class->get_element(*post_process_method_name))
                    322:                        if(Junction *junction=value->get_junction())
                    323:                                if(const Method *method=junction->method) {
                    324:                                        // preparing to pass parameters to 
1.119     paf       325:                                        //      @postprocess[data]
1.98      paf       326:                                        VMethodFrame frame(pool(), *junction, false);
1.91      paf       327:                                        frame.set_self(*main_class);
                    328: 
                    329:                                        frame.store_param(method->name, 
                    330:                                                &body_vstring_before_post_process);
                    331:                                        body_vstring_after_post_process=
                    332:                                                NEW VString(*execute_method(frame, *method));
                    333:                                }
1.90      paf       334: 
1.91      paf       335:                const VFile *body_file=body_vstring_after_post_process->as_vfile();
1.41      paf       336: 
1.45      paf       337:                // extract response body
                    338:                Value *body_value=static_cast<Value *>(
                    339:                        response.fields().get(*body_name));
1.119     paf       340:                if(body_value) // there is some $response.body
1.90      paf       341:                        body_file=body_value->as_vfile();
1.45      paf       342: 
                    343:                // OK. write out the result
1.90      paf       344:                output_result(*body_file, header_only);
1.3       paf       345:        } 
1.76      paf       346:        CATCH(e) { // request handling problem
                    347:                // we're returning not result, but error explanation
1.30      paf       348:                TRY {
1.76      paf       349:                        // log the beast
                    350:                        const String *problem_source=e.problem_source();
1.114     paf       351:                        if(problem_source && problem_source->size())
1.76      paf       352:                                SAPI::log(pool(),
                    353: #ifndef NO_STRING_ORIGIN
                    354:                                        "%s(%d): "
                    355: #endif
                    356:                                        "'%s' %s [%s %s]",
                    357: #ifndef NO_STRING_ORIGIN
                    358:                                        problem_source->origin().file?problem_source->origin().file:"global",
                    359:                                        problem_source->origin().line,
                    360: #endif
1.99      paf       361:                                        problem_source->cstr(String::UL_AS_IS),
1.76      paf       362:                                        e.comment(),
1.99      paf       363:                                        e.type()?e.type()->cstr(String::UL_AS_IS):"-",
                    364:                                        e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76      paf       365:                                );
                    366:                        else
                    367:                                SAPI::log(pool(),
                    368:                                        "%s [%s %s]",
                    369:                                        e.comment(),
1.99      paf       370:                                        e.type()?e.type()->cstr(String::UL_AS_IS):"-",
                    371:                                        e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76      paf       372:                                        );
1.43      paf       373: 
                    374:                        // reset language to default
                    375:                        flang=fdefault_lang;
                    376:                        
                    377:                        // reset response
                    378:                        response.fields().clear();
                    379: 
                    380:                        // this is what we'd return in $response:body
1.41      paf       381:                        const String *body_string=0;
1.43      paf       382: 
1.30      paf       383:                        if(main_class) { // we've managed to end up with some main_class
                    384:                                // maybe we'd be lucky enough as to report an error
                    385:                                // in a gracefull way...
                    386:                                if(Value *value=main_class->get_element(*exception_method_name))
                    387:                                        if(Junction *junction=value->get_junction())
                    388:                                                if(const Method *method=junction->method) {
                    389:                                                        // preparing to pass parameters to 
                    390:                                                        //      @exception[origin;source;comment;type;code]
1.98      paf       391:                                                        VMethodFrame frame(pool(), *junction, false);
1.38      paf       392:                                                        frame.set_self(*main_class);
1.30      paf       393: 
                    394:                                                        const String *problem_source=e.problem_source();
                    395:                                                        // origin
1.38      paf       396:                                                        Value *origin_value=0;
1.30      paf       397: #ifndef NO_STRING_ORIGIN
1.114     paf       398:                                                        if(problem_source && problem_source->size()) {
1.38      paf       399:                                                                const Origin& origin=problem_source->origin();
                    400:                                                                if(origin.file) {
                    401:                                                                        char *buf=(char *)malloc(MAX_STRING);
1.106     paf       402:                                                                        size_t buf_size=snprintf(buf, MAX_STRING, "%s(%d):", 
1.38      paf       403:                                                                                origin.file, 1+origin.line);
1.44      paf       404:                                                                        String *origin_file_line=NEW String(pool(),
1.106     paf       405:                                                                                buf, buf_size, true);
1.38      paf       406:                                                                        origin_value=NEW VString(*origin_file_line);
                    407:                                                                }
1.30      paf       408:                                                        }
                    409: #endif
1.91      paf       410:                                                        frame.store_param(method->name, 
1.38      paf       411:                                                                origin_value?origin_value:NEW VUnknown(pool()));
1.28      paf       412: 
1.30      paf       413:                                                        // source
1.38      paf       414:                                                        Value *source_value=0;
1.114     paf       415:                                                        if(problem_source && problem_source->size()) {
1.47      paf       416:                                                                String& problem_source_copy=*NEW String(pool());
                    417:                                                                problem_source_copy.append(*problem_source, 
                    418:                                                                        flang, true);
1.43      paf       419:                                                                source_value=NEW VString(problem_source_copy);
                    420:                                                        }
1.91      paf       421:                                                        frame.store_param(method->name, 
1.38      paf       422:                                                                source_value?source_value:NEW VUnknown(pool()));
1.30      paf       423: 
                    424:                                                        // comment
1.44      paf       425:                                                        String *comment_value=NEW String(pool(),
1.106     paf       426:                                                                e.comment(), 0, true);
1.91      paf       427:                                                        frame.store_param(method->name, 
1.30      paf       428:                                                                NEW VString(*comment_value));
                    429: 
                    430:                                                        // type
                    431:                                                        Value *type_value;
1.43      paf       432:                                                        if(e.type()) {
1.47      paf       433:                                                                String& type_copy=*NEW String(pool());
                    434:                                                                type_value=NEW VString(type_copy.append(*e.type(), 
                    435:                                                                        flang, true));
1.43      paf       436:                                                        } else
1.30      paf       437:                                                                type_value=NEW VUnknown(pool());
1.91      paf       438:                                                        frame.store_param(method->name, type_value);
1.30      paf       439: 
                    440:                                                        // code
                    441:                                                        Value *code_value;
1.43      paf       442:                                                        if(e.code()) {
1.47      paf       443:                                                                String& code_copy=*NEW String(pool());
                    444:                                                                code_value=NEW VString(code_copy.append(*e.code(), 
                    445:                                                                        flang, true));
1.43      paf       446:                                                        } else
1.30      paf       447:                                                                code_value=NEW VUnknown(pool());
1.91      paf       448:                                                        frame.store_param(method->name, code_value);
1.30      paf       449: 
1.43      paf       450:                                                        // future $response:body=
                    451:                                                        //   execute ^exception[origin;source;comment;type;code]
                    452:                                                        body_string=execute_method(frame, *method);
1.30      paf       453:                                                }
                    454:                        }
                    455:                        
1.41      paf       456:                        if(!body_string) {  // couldn't report an error beautifully?
                    457:                                // doing that ugly
                    458: 
                    459:                                // make up result: $origin $source $comment $type $code
                    460:                                char *buf=(char *)malloc(MAX_STRING);
1.30      paf       461:                                size_t printed=0;
                    462:                                const String *problem_source=e.problem_source();
                    463:                                if(problem_source) {
1.16      paf       464: #ifndef NO_STRING_ORIGIN
1.30      paf       465:                                        const Origin& origin=problem_source->origin();
                    466:                                        if(origin.file)
1.41      paf       467:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ", 
1.30      paf       468:                                                origin.file, 1+origin.line);
1.28      paf       469: #endif
1.41      paf       470:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ", 
1.99      paf       471:                                                problem_source->cstr(String::UL_AS_IS));
1.30      paf       472:                                }
1.41      paf       473:                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", 
1.30      paf       474:                                        e.comment());
                    475:                                const String *type=e.type();
                    476:                                if(type) {
1.41      paf       477:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "  type: %s", 
1.99      paf       478:                                                type->cstr(String::UL_AS_IS));
1.30      paf       479:                                        const String *code=e.code();
                    480:                                        if(code)
1.41      paf       481:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s", 
1.99      paf       482:                                                code->cstr(String::UL_AS_IS));
1.30      paf       483:                                }
1.43      paf       484: 
                    485:                                // future $response:content-type
1.49      paf       486:                                response.fields().put(*content_type_name, 
1.82      paf       487:                                        NEW VString(*NEW String(pool(), UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.43      paf       488:                                // future $response:body
1.44      paf       489:                                body_string=NEW String(pool(), buf);
1.30      paf       490:                        }
1.41      paf       491: 
1.90      paf       492:                        const VString body_vstring(*body_string);
                    493:                        const VFile *body_file=body_vstring.as_vfile();
                    494: 
1.45      paf       495:                        // ERROR. write it out
1.90      paf       496:                        output_result(*body_file, header_only);
1.3       paf       497:                }
1.30      paf       498:                CATCH(e) {
1.41      paf       499:                        // exception in request exception handler
                    500:                        // remember to rethrow it
                    501:                        rethrow_me=e;  need_rethrow=true; 
1.1       paf       502:                }
1.30      paf       503:                END_CATCH
1.1       paf       504:        }
1.41      paf       505:        END_CATCH // do not use pool() after this point - no exception handler set
                    506:                  // any throw() would try to use zero exception() pointer 
                    507: 
1.91      paf       508:        if(need_rethrow) // were there an exception for us to rethrow?
1.62      paf       509:                THROW(rethrow_me.type(), rethrow_me.code(),
1.41      paf       510:                        rethrow_me.problem_source(),
                    511:                        rethrow_me.comment());
1.1       paf       512: }
                    513: 
1.77      paf       514: VStateless_class *Request::use_file(const String& file_spec, bool fail_on_read_problem,
1.26      paf       515:                                                                        const String *name, 
                    516:                                                                        VStateless_class *base_class) {
1.73      paf       517:        // cyclic dependence check
1.77      paf       518:        if(used_files.get(file_spec))
1.73      paf       519:                return base_class;
1.77      paf       520:        used_files.put(file_spec, (Hash::Val *)true);
1.73      paf       521: 
                    522: 
1.77      paf       523:        char *source=file_read_text(pool(), file_spec, fail_on_read_problem);
1.3       paf       524:        if(!source)
1.12      paf       525:                return base_class;
1.3       paf       526: 
1.77      paf       527:        return use_buf(source, file_spec.cstr(), 0/*new class*/, name, base_class);
1.16      paf       528: }
                    529: 
1.67      paf       530: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26      paf       531:                                                                   VStateless_class *aclass, const String *name, 
                    532:                                                                   VStateless_class *base_class) {
1.5       paf       533:        // compile loaded class
1.26      paf       534:        VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1       paf       535: 
1.4       paf       536:        // locate and execute possible @auto[] static method
1.25      paf       537:        execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16      paf       538:        return &cclass;
1.19      paf       539: }
                    540: 
1.77      paf       541: const String& Request::relative(const char *apath, const String& relative_name) {
                    542:        int lpath_buf_size=strlen(apath)+1;
                    543:     char *lpath=(char *)malloc(lpath_buf_size);
                    544:        memcpy(lpath, apath, lpath_buf_size);
1.120     paf       545:     if(!rsplit(lpath, '/'))
                    546:                strcpy(lpath, ".");
1.77      paf       547:        String& result=*NEW String(pool(), lpath);
1.104     paf       548:     result << "/" << relative_name;
1.19      paf       549:     return result;
                    550: }
                    551: 
1.77      paf       552: const String& Request::absolute(const String& relative_name) {
                    553:        char *relative_name_cstr=relative_name.cstr();
                    554:        if(relative_name_cstr[0]=='/') {
                    555:                String& result=*NEW String(pool(), info.document_root);
1.104     paf       556:                result << relative_name;
1.19      paf       557:                return result;
                    558:        } else 
1.77      paf       559:                return relative(info.path_translated, relative_name);
1.1       paf       560: }
1.45      paf       561: 
1.101     paf       562: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    563:                                                                 void *info) {
                    564:        String *attribute_to_exclude=static_cast<String *>(info);
                    565:        if(aattribute==*attribute_to_exclude)
                    566:                return;
                    567: 
                    568:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    569:        Pool& pool=lmeaning.pool();
                    570: 
                    571:        SAPI::add_header_attribute(pool,
1.116     paf       572:                aattribute.cstr(String::UL_AS_IS), 
1.101     paf       573:                attributed_meaning_to_string(lmeaning, String::UL_HTTP_HEADER).cstr());
                    574: }
1.90      paf       575: void Request::output_result(const VFile& body_file, bool header_only) {
1.51      paf       576:        // header: cookies
                    577:        cookie.output_result();
                    578:        
1.90      paf       579:        // set content-type
                    580:        if(String *body_file_content_type=static_cast<String *>(
                    581:                body_file.fields().get(*vfile_mime_type_name))) {
                    582:                // body file content type
                    583:                response.fields().put(*content_type_name, body_file_content_type);
                    584:        } else {
                    585:                // default content type
                    586:                response.fields().put_dont_replace(*content_type_name, 
                    587:                        default_content_type?default_content_type
                    588:                        :NEW VString(*NEW String(pool(), DEFAULT_CONTENT_TYPE)));
1.92      paf       589:        }
                    590: 
                    591:        // content-disposition
1.111     paf       592:        if(VString *vfile_name=static_cast<VString *>(body_file.fields().get(*name_name)))
                    593:                if(vfile_name->string()!=NONAME_DAT) {
                    594:                        VHash& vhash=*NEW VHash(pool());
                    595:                        vhash.hash().put(*content_disposition_filename_name, vfile_name);
                    596:                        response.fields().put(*content_disposition_name, &vhash);
                    597:                }
1.49      paf       598: 
1.62      paf       599:        // prepare header: $response:fields without :body
1.73      paf       600:        response.fields().for_each(add_header_attribute, /*excluding*/ body_name);
1.50      paf       601: 
1.62      paf       602:        // prepare...
1.90      paf       603:        const void *body=body_file.value_ptr();
                    604:        size_t content_length=body_file.value_size();
1.50      paf       605: 
1.62      paf       606:        // prepare header: content-length
1.65      paf       607:        if(content_length) { // useful for redirecting [header "location: http://..."]
                    608:                char content_length_cstr[MAX_NUMBER];
1.108     paf       609:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.69      paf       610:                SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.65      paf       611:        }
1.62      paf       612: 
                    613:        // send header
1.69      paf       614:        SAPI::send_header(pool());
1.71      paf       615:        
1.62      paf       616:        // send body
                    617:        if(!header_only)
1.69      paf       618:                SAPI::send_body(pool(), body, content_length);
1.50      paf       619: }
1.104     paf       620: 
                    621: const String& Request::mime_type_of(const char *user_file_name_cstr) {
                    622:        if(mime_types)
                    623:                if(const char *cext=strrchr(user_file_name_cstr, '.')) {
                    624:                        String sext(pool(), ++cext);
                    625:                        if(mime_types->locate(0, sext))
                    626:                                if(const String *result=mime_types->item(1))
                    627:                                        return *result;
                    628:                                else
                    629:                                        THROW(0, 0,
                    630:                                                mime_types->origin_string(),
                    631:                                                "MIME-TYPE table column elements must not be empty");
                    632:                }
                    633:        return *NEW String(pool(), "application/octet-stream");
                    634: }

E-mail: