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

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

E-mail: