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

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

E-mail: