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

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

E-mail: