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

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

E-mail: