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

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

E-mail: