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

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

E-mail: