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

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

E-mail: