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

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

E-mail: