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

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

E-mail: