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

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

E-mail: