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

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

E-mail: