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

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

E-mail: