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

1.54      paf         1: /** @file
1.55      paf         2:        Parser: request class main part. @see compile.C and execute.C.
                      3: 
1.193     paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.194     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.55      paf         6: 
1.198   ! paf         7:        $Id: pa_request.C,v 1.197 2002/03/27 15:30:36 paf Exp $
1.1       paf         8: */
                      9: 
1.69      paf        10: #include "pa_sapi.h"
1.53      paf        11: #include "pa_common.h"
1.1       paf        12: #include "pa_request.h"
1.3       paf        13: #include "pa_wwrapper.h"
                     14: #include "pa_vclass.h"
1.31      paf        15: #include "pa_globals.h"
1.30      paf        16: #include "pa_vint.h"
1.112     paf        17: #include "pa_vmethod_frame.h"
1.32      paf        18: #include "pa_types.h"
1.78      paf        19: #include "pa_vtable.h"
1.90      paf        20: #include "pa_vfile.h"
1.147     parser     21: #include "pa_dictionary.h"
1.186     paf        22: #include "pa_charsets.h"
                     23: #include "pa_charset.h"
1.159     parser     24: 
1.82      paf        25: /// content type of exception response, when no @MAIN:exception handler defined
                     26: const char *UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain";
                     27: 
                     28: /// content type of response when no $MAIN:defaults.content-type defined
                     29: const char *DEFAULT_CONTENT_TYPE="text/html";
1.144     parser     30: const char *ORIGINS_CONTENT_TYPE="text/plain";
1.82      paf        31: 
1.123     paf        32: Methoded *MOP_create(Pool&);
1.195     paf        33: // op.C
                     34: VHash& exception2vhash(Pool& pool, const Exception& e);
1.123     paf        35: 
1.160     parser     36: static void load_charset(const Hash::Key& akey, Hash::Val *avalue, 
1.186     paf        37:                                                                                  void *) {
1.190     paf        38:        charsets->load_charset(akey, static_cast<Value *>(avalue)->as_string());
1.157     parser     39: }
                     40: 
                     41: //
                     42: Request::Request(Pool& apool,
                     43:                                 Info& ainfo,
1.182     paf        44:                                 uchar adefault_lang,
1.179     paf        45:                                 bool status_allowed) : Pooled(apool),
1.157     parser     46:        stack(apool),
                     47:        OP(*MOP_create(apool)),
                     48:        env(apool),
1.175     paf        49:        status(apool),
1.157     parser     50:        form(apool),
                     51:        math(apool),
                     52:        request(apool, *this),
                     53:        response(apool),
                     54:        cookie(apool),
                     55:        fclasses(apool),
                     56:        fdefault_lang(adefault_lang), flang(adefault_lang),
                     57:        info(ainfo),
                     58:        post_data(0), post_size(0),
                     59:        used_files(apool),
                     60:        default_content_type(0),
                     61:        mime_types(0),
                     62:        main_class(0),
1.191     paf        63:        fconnection(0),
1.157     parser     64:        classes_conf(apool),
1.171     parser     65:        anti_endless_execute_recoursion(0),
1.196     paf        66:        exception_trace(apool)
1.183     paf        67: #ifdef RESOURCES_DEBUG
1.198   ! paf        68:        , sql_connect_time(0),sql_request_time(0)
1.183     paf        69: #endif
1.198   ! paf        70:        , self(0), root(0), rcontext(0), wcontext(0)
1.157     parser     71: {
1.186     paf        72:        // default charsets
                     73:        pool().set_source_charset(*utf8_charset);
                     74:        pool().set_client_charset(*utf8_charset);
                     75: 
1.178     paf        76:        // maybe expire old caches
                     77:        cache_managers->maybe_expire();
                     78:        
1.157     parser     79:        /// directly used
                     80:        // operators
                     81:        OP.register_directly_used(*this);
                     82:        // classes:
                     83:        // table, file, random, mail, image, ...
                     84:        methoded_array->register_directly_used(*this);
                     85: 
                     86:        /// methodless
                     87:        // env class
                     88:        classes().put(*NEW String(pool(), ENV_CLASS_NAME), &env);
1.175     paf        89:        // status class
1.179     paf        90:        if(status_allowed)
                     91:                classes().put(*NEW String(pool(), STATUS_CLASS_NAME), &status);
1.157     parser     92:        // request class
                     93:        classes().put(*NEW String(pool(), REQUEST_CLASS_NAME), &request);       
                     94:        // cookie class
                     95:        classes().put(*NEW String(pool(), COOKIE_CLASS_NAME), &cookie);
                     96: 
                     97:        /// methoded
                     98:        // response class
                     99:        classes().put(response.get_class()->name(), &response); 
                    100: 
                    101:        /// bases used
                    102:        // form class
                    103:        classes().put(form.get_class()->base()->name(), &form); 
                    104:        // math class
                    105:        classes().put(math.get_class()->base()->name(), &math); 
1.117     paf       106: }
1.192     paf       107: 
                    108: Request::~Request() {
                    109: #ifdef XML
                    110:        // if for some strange reason xml generic errors failed to be reported, free them up
                    111:        if(const char *xml_generic_errors=xmlGenericErrors()) {
                    112:                SAPI::log(pool(), "warning: unreported xmlGenericErrors: %s", xml_generic_errors);
                    113:                free((void *)xml_generic_errors);
                    114:        }
                    115: #endif
                    116: }
                    117: 
1.150     parser    118: 
1.64      paf       119: /**
                    120:        load MAIN class, execute @main.
                    121:        MAIN class consists of all the auto.p files we'd manage to find
                    122:        plus
                    123:        the file user requested us to process
                    124:        all located classes become children of one another,
                    125:        composing class we name 'MAIN'
1.180     paf       126: 
                    127:        @test log stack trace
                    128: 
1.64      paf       129: */
1.153     parser    130: void Request::core(
                    131:                                   const char *root_config_filespec, bool root_config_fail_on_read_problem,
                    132:                                   const char *site_config_filespec, bool site_config_fail_on_read_problem,
1.62      paf       133:                                   bool header_only) {
1.183     paf       134: 
                    135: #ifdef RESOURCES_DEBUG
                    136: //measures
                    137: struct timeval mt[10];
                    138: //measure:before all
                    139: gettimeofday(&mt[0],NULL);
                    140: #endif
1.169     parser    141:        try {
1.29      paf       142:                char *auto_filespec=(char *)malloc(MAX_STRING);
                    143:                
1.153     parser    144:                // loading root config
                    145:                if(root_config_filespec) {
1.77      paf       146:                        String& filespec=*NEW String(pool());
1.153     parser    147:                        filespec.APPEND_CLEAN(root_config_filespec, 0, "root_config", 0);
1.29      paf       148:                        main_class=use_file(
1.148     parser    149:                                filespec, 
1.153     parser    150:                                true/*ignore class_path*/, root_config_fail_on_read_problem,
1.37      paf       151:                                main_class_name, main_class);
1.29      paf       152:                }
1.186     paf       153:                // charsets must only be specified in root config
                    154:                // so that users would not interfere
1.164     parser    155:                if(main_class) {
                    156:                        /* $MAIN:CHARSETS[
                    157:                                        $.charsetname1[/full/path/to/charset/file.cfg]
                    158:                                        ...
                    159:                                ]
                    160:                        */
                    161:                        if(Value *vcharsets=main_class->get_element(*charsets_name)) {
1.172     parser    162:                                if(Hash *charsets=vcharsets->get_hash(0))
1.186     paf       163:                                        charsets->for_each(load_charset);
1.164     parser    164:                                else
1.197     paf       165:                                        throw Exception("parser.runtime",
1.164     parser    166:                                                &vcharsets->name(),
                    167:                                                "must be hash");
                    168:                        }
                    169:                }
1.124     paf       170:                // configure root options
1.72      paf       171:                //      until someone with less privileges have overriden them
1.129     paf       172:                OP.configure_admin(*this);
1.124     paf       173:                methoded_array->configure_admin(*this);
1.72      paf       174: 
1.153     parser    175:                // loading site config
                    176:                if(site_config_filespec) {
1.77      paf       177:                        String& filespec=*NEW String(pool());
1.153     parser    178:                        filespec.APPEND_CLEAN(site_config_filespec, 0, "site_config", 0);
1.37      paf       179:                        main_class=use_file(
1.148     parser    180:                                filespec, 
1.153     parser    181:                                true/*ignore class_path*/, site_config_fail_on_read_problem,
1.37      paf       182:                                main_class_name, main_class);
1.29      paf       183:                }
1.8       paf       184: 
1.72      paf       185:                // loading auto.p files from document_root/.. 
                    186:                // to the one beside requested file.
                    187:                // all assigned bases from upper dir
                    188:                {
1.115     paf       189:                        const char *after=info.path_translated;
                    190:                        size_t drlen=strlen(info.document_root);
                    191:                        if(memcmp(after, info.document_root, drlen)==0) {
                    192:                                after+=drlen;
                    193:                                if(after[-1]=='/') 
                    194:                                        --after;
                    195:                        }
                    196:                        
1.152     parser    197:                        int step=0;
1.115     paf       198:                        while(const char *before=strchr(after, '/')) {
1.152     parser    199:                                String& sfile_spec=*NEW String(pool());
1.115     paf       200:                                if(after!=info.path_translated) {
1.152     parser    201:                                        sfile_spec.APPEND_CLEAN(
                    202:                                                info.path_translated, before-info.path_translated,
                    203:                                                "path-translated-scanned", step++);
                    204:                                        sfile_spec << "/" AUTO_FILE_NAME;
                    205: 
                    206:                                        main_class=use_file(sfile_spec, 
                    207:                                                true/*ignore class_path*/, false/*ignore read problem*/,
                    208:                                                main_class_name, main_class);
1.115     paf       209:                                }
                    210:                                after=before+1;
1.72      paf       211:                        }
                    212:                }
1.34      paf       213: 
1.125     paf       214:                // compile requested file
1.77      paf       215:                String& spath_translated=*NEW String(pool());
1.136     parser    216:                spath_translated.APPEND_TAINTED(info.path_translated, 0, "user-request", 0);
1.148     parser    217:                main_class=use_file(spath_translated, 
                    218:                        true/*ignore class_path*/, true/*don't ignore read problem*/,
1.72      paf       219:                        main_class_name, main_class);
1.7       paf       220: 
1.124     paf       221:                // configure not-root=user options
1.129     paf       222:                OP.configure_user(*this);
1.124     paf       223:                methoded_array->configure_user(*this);
                    224: 
1.85      paf       225:                // $MAIN:MIME-TYPES
                    226:                if(Value *element=main_class->get_element(*mime_types_name))
                    227:                        if(Table *table=element->get_table())
                    228:                                mime_types=table;                       
1.102     paf       229: 
1.124     paf       230:                // filling form fields
1.153     parser    231:                form.fill_fields_and_tables(*this);
1.102     paf       232: 
1.124     paf       233:                // filling cookies
                    234:                cookie.fill_fields(*this);
1.25      paf       235: 
1.183     paf       236: #ifdef RESOURCES_DEBUG
                    237: //measure:after compile
                    238: gettimeofday(&mt[1],NULL);
                    239: #endif
1.25      paf       240:                // execute @main[]
1.189     paf       241:                const String *body_string=execute_virtual_method(*main_class, *main_method_name);
1.41      paf       242:                if(!body_string)
1.197     paf       243:                        throw Exception("parser.runtime",
1.145     parser    244:                                0, 
                    245:                                "'"MAIN_METHOD_NAME"' method not found");
1.79      paf       246: 
1.183     paf       247: #ifdef RESOURCES_DEBUG
                    248:                //measure:after main
                    249: gettimeofday(&mt[2],NULL);
                    250: #endif
                    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;
1.119     paf       254:                // @postprocess
1.91      paf       255:                if(Value *value=main_class->get_element(*post_process_method_name))
                    256:                        if(Junction *junction=value->get_junction())
                    257:                                if(const Method *method=junction->method) {
                    258:                                        // preparing to pass parameters to 
1.119     paf       259:                                        //      @postprocess[data]
1.146     parser    260:                                        VMethodFrame frame(pool(), value->name(), *junction);
1.91      paf       261:                                        frame.set_self(*main_class);
                    262: 
                    263:                                        frame.store_param(method->name, 
                    264:                                                &body_vstring_before_post_process);
                    265:                                        body_vstring_after_post_process=
1.188     paf       266:                                                NEW VString(execute_method(frame, *method));
1.91      paf       267:                                }
1.90      paf       268: 
1.144     parser    269:                bool origins_mode=main_class->get_element(*origins_mode_name)!=0;
                    270: 
                    271:                const VFile *body_file=body_vstring_after_post_process->as_vfile(
                    272:                        String::UL_UNSPECIFIED, origins_mode);
1.41      paf       273: 
1.45      paf       274:                // extract response body
                    275:                Value *body_value=static_cast<Value *>(
                    276:                        response.fields().get(*body_name));
1.119     paf       277:                if(body_value) // there is some $response.body
1.90      paf       278:                        body_file=body_value->as_vfile();
1.144     parser    279:                else if(origins_mode)
                    280:                        response.fields().put(*content_type_name, 
                    281:                                NEW VString(*NEW String(pool(), ORIGINS_CONTENT_TYPE)));
1.45      paf       282: 
1.183     paf       283: #ifdef RESOURCES_DEBUG
                    284: //measure:after postprocess
                    285: gettimeofday(&mt[3],NULL);             
                    286: #endif
                    287: 
1.45      paf       288:                // OK. write out the result
1.90      paf       289:                output_result(*body_file, header_only);
1.183     paf       290: 
                    291: #ifdef RESOURCES_DEBUG
                    292:                //measure:after output_result
                    293: gettimeofday(&mt[9],NULL);             
                    294: t[9]=mt[9].tv_sec+mt[9].tv_usec/1000000.0;
                    295: 
                    296: double t[10];
                    297: for(int i=0;i<10;i++)
                    298:     t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
                    299: //measure:log2 compile,main,postprocess,output
                    300: SAPI::log(pool(), "rmeasure: %s,%.2f,%.2f,%.2f %.2f,%.2f %.2f", 
                    301: info.uri,
                    302: t[1]-t[0],
                    303: t[2]-t[1],
                    304: t[3]-t[2],
                    305: sql_connect_time,sql_request_time,
                    306: t[9]-t[3]
                    307: );
                    308: #endif
1.171     parser    309:        } catch(const Exception& e) { // request handling problem
1.76      paf       310:                // we're returning not result, but error explanation
1.169     parser    311:                try {
1.76      paf       312:                        // log the beast
                    313:                        const String *problem_source=e.problem_source();
1.114     paf       314:                        if(problem_source && problem_source->size())
1.76      paf       315:                                SAPI::log(pool(),
                    316: #ifndef NO_STRING_ORIGIN
1.180     paf       317:                                        ORIGIN_FILE_LINE_FORMAT": "
1.76      paf       318: #endif
1.197     paf       319:                                        "'%s' %s [%s]",
1.76      paf       320: #ifndef NO_STRING_ORIGIN
                    321:                                        problem_source->origin().file?problem_source->origin().file:"global",
                    322:                                        problem_source->origin().line,
                    323: #endif
1.173     paf       324:                                        problem_source->cstr(),
1.76      paf       325:                                        e.comment(),
1.197     paf       326:                                        e.type()?e.type():"-"
1.76      paf       327:                                );
                    328:                        else
                    329:                                SAPI::log(pool(),
1.197     paf       330:                                        "%s [%s]",
1.76      paf       331:                                        e.comment(),
1.197     paf       332:                                        e.type()?e.type():"-"
1.180     paf       333:                                );
1.171     parser    334: 
1.43      paf       335:                        // reset language to default
                    336:                        flang=fdefault_lang;
                    337:                        
                    338:                        // reset response
                    339:                        response.fields().clear();
                    340: 
                    341:                        // this is what we'd return in $response:body
1.41      paf       342:                        const String *body_string=0;
1.43      paf       343: 
1.30      paf       344:                        if(main_class) { // we've managed to end up with some main_class
                    345:                                // maybe we'd be lucky enough as to report an error
                    346:                                // in a gracefull way...
1.195     paf       347:                                if(Value *value=main_class->get_element(*unhandled_exception_method_name))
1.30      paf       348:                                        if(Junction *junction=value->get_junction())
                    349:                                                if(const Method *method=junction->method) {
1.169     parser    350:                                                        // preparing to pass parameters to 
1.196     paf       351:                                                        //      @unhandled_exception[exception;stack]
1.146     parser    352:                                                        VMethodFrame frame(pool(), value->name(), *junction);
1.38      paf       353:                                                        frame.set_self(*main_class);
1.30      paf       354: 
1.196     paf       355:                                                        // $exception
1.195     paf       356:                                                        frame.store_param(method->name, &exception2vhash(pool(), e));
1.171     parser    357:                                                        // $stack[^table::set{name      origin}]
                    358:                                                        Array& stack_trace_columns=*NEW Array(pool());
                    359:                                                        stack_trace_columns+=NEW String(pool(), "name");
1.195     paf       360:                                                        stack_trace_columns+=NEW String(pool(), "file");
                    361:                                                        stack_trace_columns+=NEW String(pool(), "lineno");
1.171     parser    362:                                                        Table& stack_trace=*NEW Table(pool(), 0, &stack_trace_columns);
1.196     paf       363:                                                        Array_iter tracei(exception_trace);
1.171     parser    364:                                                        while(tracei.has_next()) {
                    365:                                                                Array& row=*NEW Array(pool());
                    366: 
                    367:                                                                const String *name=(const String *)tracei.next();
                    368:                                                                row+=name; // name column
                    369: #ifndef NO_STRING_ORIGIN
                    370:                                                                const Origin& origin=name->origin();
                    371:                                                                if(origin.file) {
1.195     paf       372:                                                                        row+=NEW String(pool(), origin.file, 0, true); // file column
                    373:                                                                        char *buf=(char *)malloc(MAX_NUMBER);
                    374:                                                                        size_t buf_size=snprintf(buf, MAX_NUMBER, "%d", 1+origin.line);
                    375:                                                                        row+=NEW String(pool(), buf, buf_size, true); // line column
1.171     parser    376:                                                                }
                    377: #endif
                    378:                                                                stack_trace+=&row;
                    379:                                                        }
                    380:                                                        frame.store_param(method->name, 
                    381:                                                                NEW VTable(pool(), &stack_trace));
                    382: 
1.43      paf       383:                                                        // future $response:body=
1.171     parser    384:                                                        //   execute ^exception[origin;source;comment;type;code;stack]
1.188     paf       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.180     paf       400:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, 
                    401:                                                        ORIGIN_FILE_LINE_FORMAT": ", 
                    402:                                                        origin.file, 1+origin.line
                    403:                                                );
1.28      paf       404: #endif
1.41      paf       405:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ", 
1.173     paf       406:                                                problem_source->cstr());
1.30      paf       407:                                }
1.41      paf       408:                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", 
1.30      paf       409:                                        e.comment());
1.197     paf       410:                                if(const char *type=e.type()) 
                    411:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "  type: %s",  type);
1.43      paf       412: 
                    413:                                // future $response:content-type
1.49      paf       414:                                response.fields().put(*content_type_name, 
1.82      paf       415:                                        NEW VString(*NEW String(pool(), UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.43      paf       416:                                // future $response:body
1.44      paf       417:                                body_string=NEW String(pool(), buf);
1.30      paf       418:                        }
1.41      paf       419: 
1.144     parser    420:                        VString body_vstring(*body_string);
1.90      paf       421:                        const VFile *body_file=body_vstring.as_vfile();
                    422: 
1.45      paf       423:                        // ERROR. write it out
1.90      paf       424:                        output_result(*body_file, header_only);
1.170     parser    425:                } catch(const Exception& ) {
1.169     parser    426:                        /*re*/throw;
1.3       paf       427:                }
1.1       paf       428:        }
                    429: }
                    430: 
1.148     parser    431: VStateless_class *Request::use_file(const String& file_name, 
                    432:                                                                        bool ignore_class_path, bool fail_on_read_problem,
1.26      paf       433:                                                                        const String *name, 
                    434:                                                                        VStateless_class *base_class) {
1.73      paf       435:        // cyclic dependence check
1.148     parser    436:        if(used_files.get(file_name))
1.73      paf       437:                return base_class;
1.148     parser    438:        used_files.put(file_name, (Hash::Val *)true);
1.73      paf       439: 
1.148     parser    440:        const String *file_spec;
1.153     parser    441:        if(ignore_class_path) // ignore_class_path?
1.148     parser    442:                file_spec=&file_name;
1.153     parser    443:        else if(file_name.first_char()=='/') //absolute path, no need to scan MAIN:CLASS_PATH?
                    444:                file_spec=&absolute(file_name);
                    445:        else {
                    446:                file_spec=0;
                    447:                if(main_class)
                    448:                        if(Value *element=main_class->get_element(*class_path_name)) {
                    449:                                if(element->is_string()) {
1.187     paf       450:                                        file_spec=file_readable(absolute(element->as_string()), file_name); // found at class_path?
1.153     parser    451:                                } else if(Table *table=element->get_table()) {
                    452:                                        int size=table->size();
                    453:                                        for(int i=size; i--; ) {
                    454:                                                const String& path=*static_cast<Array *>(table->get(i))->get_string(0);
1.187     paf       455:                                                if(file_spec=file_readable(absolute(path), file_name))
1.153     parser    456:                                                        break; // found along class_path
1.148     parser    457:                                        }
1.153     parser    458:                                } else
1.197     paf       459:                                        throw Exception("parser.runtime",
1.153     parser    460:                                                &element->name(),
                    461:                                                "must be string or table");
                    462:                                if(!file_spec)
1.197     paf       463:                                        throw Exception("parser.runtime",
1.153     parser    464:                                                &file_name,
                    465:                                                "not found along " MAIN_CLASS_NAME ":" CLASS_PATH_NAME);
                    466:                        }
                    467:                if(!file_spec)
1.197     paf       468:                        throw Exception("parser.runtime",
1.153     parser    469:                                &file_name,
                    470:                                "usage failed - no " MAIN_CLASS_NAME  ":" CLASS_PATH_NAME " were specified");
1.148     parser    471:        }
1.73      paf       472: 
1.148     parser    473:        char *source=file_read_text(pool(), *file_spec, fail_on_read_problem);
1.3       paf       474:        if(!source)
1.12      paf       475:                return base_class;
1.3       paf       476: 
1.148     parser    477:        return use_buf(source, file_spec->cstr(), 0/*new class*/, name, base_class);
1.16      paf       478: }
                    479: 
1.67      paf       480: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26      paf       481:                                                                   VStateless_class *aclass, const String *name, 
                    482:                                                                   VStateless_class *base_class) {
1.5       paf       483:        // compile loaded class
1.26      paf       484:        VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1       paf       485: 
1.4       paf       486:        // locate and execute possible @auto[] static method
1.143     parser    487:        execute_nonvirtual_method(cclass, *auto_method_name, false /*no result needed*/);
1.16      paf       488:        return &cclass;
1.19      paf       489: }
                    490: 
1.77      paf       491: const String& Request::relative(const char *apath, const String& relative_name) {
                    492:        int lpath_buf_size=strlen(apath)+1;
                    493:     char *lpath=(char *)malloc(lpath_buf_size);
                    494:        memcpy(lpath, apath, lpath_buf_size);
1.120     paf       495:     if(!rsplit(lpath, '/'))
                    496:                strcpy(lpath, ".");
1.77      paf       497:        String& result=*NEW String(pool(), lpath);
1.104     paf       498:     result << "/" << relative_name;
1.19      paf       499:     return result;
                    500: }
                    501: 
1.77      paf       502: const String& Request::absolute(const String& relative_name) {
                    503:        char *relative_name_cstr=relative_name.cstr();
                    504:        if(relative_name_cstr[0]=='/') {
                    505:                String& result=*NEW String(pool(), info.document_root);
1.104     paf       506:                result << relative_name;
1.19      paf       507:                return result;
                    508:        } else 
1.77      paf       509:                return relative(info.path_translated, relative_name);
1.1       paf       510: }
1.45      paf       511: 
1.101     paf       512: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    513:                                                                 void *info) {
1.178     paf       514:        Request& r=*static_cast<Request *>(info);
1.184     paf       515:        if(aattribute==BODY_NAME
                    516:                || aattribute==CHARSET_NAME)
1.101     paf       517:                return;
                    518: 
                    519:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    520:        Pool& pool=lmeaning.pool();
                    521: 
                    522:        SAPI::add_header_attribute(pool,
1.173     paf       523:                aattribute.cstr(), 
1.101     paf       524:                attributed_meaning_to_string(lmeaning, String::UL_HTTP_HEADER).cstr());
                    525: }
1.90      paf       526: void Request::output_result(const VFile& body_file, bool header_only) {
1.51      paf       527:        // header: cookies
                    528:        cookie.output_result();
                    529:        
1.90      paf       530:        // set content-type
                    531:        if(String *body_file_content_type=static_cast<String *>(
                    532:                body_file.fields().get(*vfile_mime_type_name))) {
                    533:                // body file content type
                    534:                response.fields().put(*content_type_name, body_file_content_type);
                    535:        } else {
                    536:                // default content type
                    537:                response.fields().put_dont_replace(*content_type_name, 
                    538:                        default_content_type?default_content_type
                    539:                        :NEW VString(*NEW String(pool(), DEFAULT_CONTENT_TYPE)));
1.92      paf       540:        }
                    541: 
                    542:        // content-disposition
1.111     paf       543:        if(VString *vfile_name=static_cast<VString *>(body_file.fields().get(*name_name)))
                    544:                if(vfile_name->string()!=NONAME_DAT) {
                    545:                        VHash& vhash=*NEW VHash(pool());
1.174     paf       546:                        vhash.hash(0).put(*content_disposition_filename_name, vfile_name);
1.111     paf       547:                        response.fields().put(*content_disposition_name, &vhash);
                    548:                }
1.49      paf       549: 
1.62      paf       550:        // prepare header: $response:fields without :body
1.178     paf       551:        response.fields().for_each(add_header_attribute, this);
1.50      paf       552: 
1.185     paf       553:        // transcode
1.184     paf       554:        const void *client_body;
                    555:        size_t client_content_length;
1.186     paf       556:        Charset::transcode(pool(),
                    557:                pool().get_source_charset(), body_file.value_ptr(), body_file.value_size(),
                    558:                pool().get_client_charset(), client_body, client_content_length
1.184     paf       559:        );
1.50      paf       560: 
1.62      paf       561:        // prepare header: content-length
1.184     paf       562:        if(client_content_length) { // useful for redirecting [header "location: http://..."]
1.65      paf       563:                char content_length_cstr[MAX_NUMBER];
1.184     paf       564:                snprintf(content_length_cstr, MAX_NUMBER, "%u", client_content_length);
1.69      paf       565:                SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.65      paf       566:        }
1.62      paf       567: 
                    568:        // send header
1.69      paf       569:        SAPI::send_header(pool());
1.71      paf       570:        
1.62      paf       571:        // send body
                    572:        if(!header_only)
1.184     paf       573:                SAPI::send_body(pool(), client_body, client_content_length);
1.50      paf       574: }
1.104     paf       575: 
                    576: const String& Request::mime_type_of(const char *user_file_name_cstr) {
                    577:        if(mime_types)
                    578:                if(const char *cext=strrchr(user_file_name_cstr, '.')) {
                    579:                        String sext(pool(), ++cext);
                    580:                        if(mime_types->locate(0, sext))
                    581:                                if(const String *result=mime_types->item(1))
                    582:                                        return *result;
                    583:                                else
1.197     paf       584:                                        throw Exception("parser.runtime",
1.104     paf       585:                                                mime_types->origin_string(),
                    586:                                                "MIME-TYPE table column elements must not be empty");
                    587:                }
                    588:        return *NEW String(pool(), "application/octet-stream");
1.133     parser    589: }

E-mail: