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

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

E-mail: