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

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

E-mail: