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

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

E-mail: