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

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.150     parser      8: static const char *RCSId="$Id: pa_request.C,v 1.149 2001/08/07 13:23:33 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.63      paf       157: void Request::core(const char *root_auto_path, bool root_auto_fail,
                    158:                                   const char *site_auto_path, bool site_auto_fail,
1.62      paf       159:                                   bool header_only) {
1.121     paf       160:        //_asm { int 3 }
1.41      paf       161:        bool need_rethrow=false;  Exception rethrow_me;
1.3       paf       162:        TRY {
1.29      paf       163:                char *auto_filespec=(char *)malloc(MAX_STRING);
                    164:                
1.63      paf       165:                // loading root auto.p 
                    166:                if(root_auto_path) {
1.77      paf       167:                        String& filespec=*NEW String(pool());
1.88      paf       168:                        filespec.APPEND_CLEAN(root_auto_path, 0, "root_auto", 0);
1.77      paf       169:                        filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.29      paf       170:                        main_class=use_file(
1.148     parser    171:                                filespec, 
                    172:                                true/*ignore class_path*/, root_auto_fail,
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.64      paf       181:                // loading site auto.p
1.63      paf       182:                if(site_auto_path) {
1.77      paf       183:                        String& filespec=*NEW String(pool());
1.88      paf       184:                        filespec.APPEND_CLEAN(site_auto_path, 0, "site_auto", 0);
1.77      paf       185:                        filespec.APPEND_CONST("/" AUTO_FILE_NAME);
1.37      paf       186:                        main_class=use_file(
1.148     parser    187:                                filespec, 
                    188:                                true/*ignore class_path*/, site_auto_fail,
1.37      paf       189:                                main_class_name, main_class);
1.29      paf       190:                }
1.8       paf       191: 
1.72      paf       192:                // loading auto.p files from document_root/.. 
                    193:                // to the one beside requested file.
                    194:                // all assigned bases from upper dir
                    195:                {
1.115     paf       196:                        Array ladder(pool());
                    197:                        const char *after=info.path_translated;
                    198:                        size_t drlen=strlen(info.document_root);
                    199:                        if(memcmp(after, info.document_root, drlen)==0) {
                    200:                                after+=drlen;
                    201:                                if(after[-1]=='/') 
                    202:                                        --after;
                    203:                        }
                    204:                        
                    205:                        while(const char *before=strchr(after, '/')) {
                    206:                                String& step=*NEW String(pool());
                    207:                                if(after!=info.path_translated) {
                    208:                                        step.APPEND_CLEAN(
                    209:                                                info.path_translated, before+1/* / */-info.path_translated,
                    210:                                                "path-translated-scanned", ladder.size());
                    211:                                        step << AUTO_FILE_NAME;
                    212:                                        ladder+=&step;
                    213:                                }
                    214:                                after=before+1;
                    215:                        }
1.142     parser    216:                        for(int i=0; i<ladder.size(); i++) {
1.115     paf       217:                                const String& sfile_spec=*ladder.get_string(i);
1.148     parser    218:                                main_class=use_file(sfile_spec, 
                    219:                                        true/*ignore class_path*/, false/*ignore read problem*/,
1.115     paf       220:                                        main_class_name, main_class);
1.72      paf       221:                        }
                    222:                }
1.34      paf       223: 
1.125     paf       224:                // compile requested file
1.77      paf       225:                String& spath_translated=*NEW String(pool());
1.136     parser    226:                spath_translated.APPEND_TAINTED(info.path_translated, 0, "user-request", 0);
1.148     parser    227:                main_class=use_file(spath_translated, 
                    228:                        true/*ignore class_path*/, true/*don't ignore read problem*/,
1.72      paf       229:                        main_class_name, main_class);
1.7       paf       230: 
1.124     paf       231:                // configure not-root=user options
1.129     paf       232:                OP.configure_user(*this);
1.124     paf       233:                methoded_array->configure_user(*this);
                    234: 
1.85      paf       235:                // $MAIN:DEFAULTS
1.78      paf       236:                Value *defaults=main_class->get_element(*defaults_name);
1.75      paf       237:                // value must be allocated on request's pool for that pool used on
                    238:                // meaning constructing @see attributed_meaning_to_string
1.80      paf       239:                default_content_type=defaults?defaults->get_element(*content_type_name):0;
1.117     paf       240:                if(Value *element=main_class->get_element(*user_html_name))
1.87      paf       241:                        if(Table *table=element->get_table())
1.147     parser    242:                                pool().set_tag(NEW Dictionary(*table));
1.85      paf       243: 
                    244:                // $MAIN:MIME-TYPES
                    245:                if(Value *element=main_class->get_element(*mime_types_name))
                    246:                        if(Table *table=element->get_table())
                    247:                                mime_types=table;                       
1.96      paf       248: 
1.117     paf       249:                /*
                    250:                        $MAIN:CTYPE[
1.147     parser    251:                                $.white-space[
1.117     paf       252:                                        ^#09^#0A^#0B^#0C^#0D^#20]
1.147     parser    253:                                $.digit[
1.117     paf       254:                                        0123456789]
1.147     parser    255:                                $.hex-digit[
1.117     paf       256:                                        0123456789ABCDEFabcdef]
1.147     parser    257:                                $.letter[
1.117     paf       258:                                        ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]
1.147     parser    259:                                $.word[
1.117     paf       260:                                        0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz]
                    261: 
1.147     parser    262:                                $.lowercase[
1.117     paf       263:                                        AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz]
                    264:                        ]
                    265:                */
                    266:                if(Value *ctype=main_class->get_element(*ctype_name)) {
                    267:                        // lowcase, flipcase, bits digit+word+whitespace, masks
                    268:                        pcre_tables=(unsigned char *)calloc(tables_length);
                    269:                        prepare_case_tables(pcre_tables);
                    270: 
                    271:                        element2ctypes(pcre_tables, *ctype, *ctype_white_space_name, ctype_space, cbit_space, false);
                    272:                        element2ctypes(pcre_tables, *ctype, *ctype_digit_name, ctype_digit, cbit_digit);
                    273:                        element2ctypes(pcre_tables, *ctype, *ctype_hex_digit_name, ctype_xdigit);
                    274:                        element2ctypes(pcre_tables, *ctype, *ctype_letter_name, ctype_letter);
                    275:                        element2ctypes(pcre_tables, *ctype, *ctype_word_name, ctype_word, cbit_word);
                    276:                        cstr2ctypes(pcre_tables, (const unsigned char *)"*+?{^.$|()[", ctype_meta);
                    277: 
                    278:                        element2case(pcre_tables, *ctype, *ctype_lowercase_name);
1.132     parser    279:                } else
                    280:                        pcre_tables=pcre_default_tables;
1.102     paf       281: 
1.124     paf       282:                // filling form fields
                    283:                form.fill_fields(*this);
1.102     paf       284: 
1.124     paf       285:                // filling cookies
                    286:                cookie.fill_fields(*this);
1.25      paf       287: 
                    288:                // execute @main[]
1.143     parser    289:                const String *body_string=execute_virtual_method(
                    290:                        *main_class, *main_method_name);
1.41      paf       291:                if(!body_string)
1.25      paf       292:                        THROW(0,0,
1.145     parser    293:                                0, 
                    294:                                "'"MAIN_METHOD_NAME"' method not found");
1.79      paf       295: 
1.91      paf       296:                VString body_vstring_before_post_process(*body_string);
                    297:                VString *body_vstring_after_post_process=&body_vstring_before_post_process;
                    298:                
1.119     paf       299:                // @postprocess
1.91      paf       300:                if(Value *value=main_class->get_element(*post_process_method_name))
                    301:                        if(Junction *junction=value->get_junction())
                    302:                                if(const Method *method=junction->method) {
                    303:                                        // preparing to pass parameters to 
1.119     paf       304:                                        //      @postprocess[data]
1.146     parser    305:                                        VMethodFrame frame(pool(), value->name(), *junction);
1.91      paf       306:                                        frame.set_self(*main_class);
                    307: 
                    308:                                        frame.store_param(method->name, 
                    309:                                                &body_vstring_before_post_process);
                    310:                                        body_vstring_after_post_process=
                    311:                                                NEW VString(*execute_method(frame, *method));
                    312:                                }
1.90      paf       313: 
1.144     parser    314:                bool origins_mode=main_class->get_element(*origins_mode_name)!=0;
                    315: 
                    316:                const VFile *body_file=body_vstring_after_post_process->as_vfile(
                    317:                        String::UL_UNSPECIFIED, origins_mode);
1.41      paf       318: 
1.45      paf       319:                // extract response body
                    320:                Value *body_value=static_cast<Value *>(
                    321:                        response.fields().get(*body_name));
1.119     paf       322:                if(body_value) // there is some $response.body
1.90      paf       323:                        body_file=body_value->as_vfile();
1.144     parser    324:                else if(origins_mode)
                    325:                        response.fields().put(*content_type_name, 
                    326:                                NEW VString(*NEW String(pool(), ORIGINS_CONTENT_TYPE)));
1.45      paf       327: 
                    328:                // OK. write out the result
1.90      paf       329:                output_result(*body_file, header_only);
1.3       paf       330:        } 
1.76      paf       331:        CATCH(e) { // request handling problem
                    332:                // we're returning not result, but error explanation
1.30      paf       333:                TRY {
1.76      paf       334:                        // log the beast
                    335:                        const String *problem_source=e.problem_source();
1.114     paf       336:                        if(problem_source && problem_source->size())
1.76      paf       337:                                SAPI::log(pool(),
                    338: #ifndef NO_STRING_ORIGIN
                    339:                                        "%s(%d): "
                    340: #endif
                    341:                                        "'%s' %s [%s %s]",
                    342: #ifndef NO_STRING_ORIGIN
                    343:                                        problem_source->origin().file?problem_source->origin().file:"global",
                    344:                                        problem_source->origin().line,
                    345: #endif
1.99      paf       346:                                        problem_source->cstr(String::UL_AS_IS),
1.76      paf       347:                                        e.comment(),
1.99      paf       348:                                        e.type()?e.type()->cstr(String::UL_AS_IS):"-",
                    349:                                        e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76      paf       350:                                );
                    351:                        else
                    352:                                SAPI::log(pool(),
                    353:                                        "%s [%s %s]",
                    354:                                        e.comment(),
1.99      paf       355:                                        e.type()?e.type()->cstr(String::UL_AS_IS):"-",
                    356:                                        e.code()?e.code()->cstr(String::UL_AS_IS):"-"
1.76      paf       357:                                        );
1.43      paf       358: 
                    359:                        // reset language to default
                    360:                        flang=fdefault_lang;
1.135     parser    361:                        if(flang==String::UL_USER_HTML)
1.137     parser    362:                                flang=String::UL_HTML; // no _ & Co conversions in @exception[params]
1.43      paf       363:                        
                    364:                        // reset response
                    365:                        response.fields().clear();
                    366: 
                    367:                        // this is what we'd return in $response:body
1.41      paf       368:                        const String *body_string=0;
1.43      paf       369: 
1.30      paf       370:                        if(main_class) { // we've managed to end up with some main_class
                    371:                                // maybe we'd be lucky enough as to report an error
                    372:                                // in a gracefull way...
                    373:                                if(Value *value=main_class->get_element(*exception_method_name))
                    374:                                        if(Junction *junction=value->get_junction())
                    375:                                                if(const Method *method=junction->method) {
                    376:                                                        // preparing to pass parameters to 
                    377:                                                        //      @exception[origin;source;comment;type;code]
1.146     parser    378:                                                        VMethodFrame frame(pool(), value->name(), *junction);
1.38      paf       379:                                                        frame.set_self(*main_class);
1.30      paf       380: 
                    381:                                                        const String *problem_source=e.problem_source();
                    382:                                                        // origin
1.38      paf       383:                                                        Value *origin_value=0;
1.30      paf       384: #ifndef NO_STRING_ORIGIN
1.114     paf       385:                                                        if(problem_source && problem_source->size()) {
1.38      paf       386:                                                                const Origin& origin=problem_source->origin();
                    387:                                                                if(origin.file) {
                    388:                                                                        char *buf=(char *)malloc(MAX_STRING);
1.106     paf       389:                                                                        size_t buf_size=snprintf(buf, MAX_STRING, "%s(%d):", 
1.38      paf       390:                                                                                origin.file, 1+origin.line);
1.44      paf       391:                                                                        String *origin_file_line=NEW String(pool(),
1.106     paf       392:                                                                                buf, buf_size, true);
1.38      paf       393:                                                                        origin_value=NEW VString(*origin_file_line);
                    394:                                                                }
1.30      paf       395:                                                        }
                    396: #endif
1.91      paf       397:                                                        frame.store_param(method->name, 
1.138     parser    398:                                                                origin_value?origin_value:NEW VVoid(pool()));
1.28      paf       399: 
1.30      paf       400:                                                        // source
1.38      paf       401:                                                        Value *source_value=0;
1.114     paf       402:                                                        if(problem_source && problem_source->size()) {
1.47      paf       403:                                                                String& problem_source_copy=*NEW String(pool());
                    404:                                                                problem_source_copy.append(*problem_source, 
                    405:                                                                        flang, true);
1.43      paf       406:                                                                source_value=NEW VString(problem_source_copy);
                    407:                                                        }
1.91      paf       408:                                                        frame.store_param(method->name, 
1.138     parser    409:                                                                source_value?source_value:NEW VVoid(pool()));
1.30      paf       410: 
                    411:                                                        // comment
1.44      paf       412:                                                        String *comment_value=NEW String(pool(),
1.106     paf       413:                                                                e.comment(), 0, true);
1.91      paf       414:                                                        frame.store_param(method->name, 
1.30      paf       415:                                                                NEW VString(*comment_value));
                    416: 
                    417:                                                        // type
                    418:                                                        Value *type_value;
1.43      paf       419:                                                        if(e.type()) {
1.47      paf       420:                                                                String& type_copy=*NEW String(pool());
                    421:                                                                type_value=NEW VString(type_copy.append(*e.type(), 
                    422:                                                                        flang, true));
1.43      paf       423:                                                        } else
1.138     parser    424:                                                                type_value=NEW VVoid(pool());
1.91      paf       425:                                                        frame.store_param(method->name, type_value);
1.30      paf       426: 
                    427:                                                        // code
                    428:                                                        Value *code_value;
1.43      paf       429:                                                        if(e.code()) {
1.47      paf       430:                                                                String& code_copy=*NEW String(pool());
                    431:                                                                code_value=NEW VString(code_copy.append(*e.code(), 
                    432:                                                                        flang, true));
1.43      paf       433:                                                        } else
1.138     parser    434:                                                                code_value=NEW VVoid(pool());
1.91      paf       435:                                                        frame.store_param(method->name, code_value);
1.30      paf       436: 
1.43      paf       437:                                                        // future $response:body=
                    438:                                                        //   execute ^exception[origin;source;comment;type;code]
                    439:                                                        body_string=execute_method(frame, *method);
1.30      paf       440:                                                }
                    441:                        }
                    442:                        
1.41      paf       443:                        if(!body_string) {  // couldn't report an error beautifully?
                    444:                                // doing that ugly
                    445: 
                    446:                                // make up result: $origin $source $comment $type $code
                    447:                                char *buf=(char *)malloc(MAX_STRING);
1.30      paf       448:                                size_t printed=0;
                    449:                                const String *problem_source=e.problem_source();
                    450:                                if(problem_source) {
1.16      paf       451: #ifndef NO_STRING_ORIGIN
1.30      paf       452:                                        const Origin& origin=problem_source->origin();
                    453:                                        if(origin.file)
1.41      paf       454:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ", 
1.30      paf       455:                                                origin.file, 1+origin.line);
1.28      paf       456: #endif
1.41      paf       457:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ", 
1.99      paf       458:                                                problem_source->cstr(String::UL_AS_IS));
1.30      paf       459:                                }
1.41      paf       460:                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", 
1.30      paf       461:                                        e.comment());
                    462:                                const String *type=e.type();
                    463:                                if(type) {
1.41      paf       464:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "  type: %s", 
1.99      paf       465:                                                type->cstr(String::UL_AS_IS));
1.30      paf       466:                                        const String *code=e.code();
                    467:                                        if(code)
1.41      paf       468:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s", 
1.99      paf       469:                                                code->cstr(String::UL_AS_IS));
1.30      paf       470:                                }
1.43      paf       471: 
                    472:                                // future $response:content-type
1.49      paf       473:                                response.fields().put(*content_type_name, 
1.82      paf       474:                                        NEW VString(*NEW String(pool(), UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.43      paf       475:                                // future $response:body
1.44      paf       476:                                body_string=NEW String(pool(), buf);
1.30      paf       477:                        }
1.41      paf       478: 
1.144     parser    479:                        VString body_vstring(*body_string);
1.90      paf       480:                        const VFile *body_file=body_vstring.as_vfile();
                    481: 
1.45      paf       482:                        // ERROR. write it out
1.90      paf       483:                        output_result(*body_file, header_only);
1.3       paf       484:                }
1.30      paf       485:                CATCH(e) {
1.41      paf       486:                        // exception in request exception handler
                    487:                        // remember to rethrow it
                    488:                        rethrow_me=e;  need_rethrow=true; 
1.1       paf       489:                }
1.30      paf       490:                END_CATCH
1.1       paf       491:        }
1.41      paf       492:        END_CATCH // do not use pool() after this point - no exception handler set
                    493:                  // any throw() would try to use zero exception() pointer 
                    494: 
1.91      paf       495:        if(need_rethrow) // were there an exception for us to rethrow?
1.62      paf       496:                THROW(rethrow_me.type(), rethrow_me.code(),
1.41      paf       497:                        rethrow_me.problem_source(),
                    498:                        rethrow_me.comment());
1.1       paf       499: }
                    500: 
1.148     parser    501: VStateless_class *Request::use_file(const String& file_name, 
                    502:                                                                        bool ignore_class_path, bool fail_on_read_problem,
1.26      paf       503:                                                                        const String *name, 
                    504:                                                                        VStateless_class *base_class) {
1.73      paf       505:        // cyclic dependence check
1.148     parser    506:        if(used_files.get(file_name))
1.73      paf       507:                return base_class;
1.148     parser    508:        used_files.put(file_name, (Hash::Val *)true);
1.73      paf       509: 
1.148     parser    510:        const String *file_spec;
                    511:        if(ignore_class_path || file_name.first_char()=='/') {
                    512:                // ignore_class_path | absolute path, no need to scan MAIN:class_path
                    513:                file_spec=&file_name;
                    514:        } else {
                    515:                        file_spec=0;
                    516:                        if(main_class)
                    517:                                if(Value *element=main_class->get_element(*class_path_name))
                    518:                                        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:                                                        String *test_file_spec=NEW String(path);
                    523:                                                        *test_file_spec << "/";
                    524:                                                        *test_file_spec << file_name;
                    525:                                                        if(file_readable(*test_file_spec)) {
                    526:                                                                file_spec=test_file_spec; // found along class_path
                    527:                                                                break;
                    528:                                                        }
                    529:                                                }
                    530:                                                if(!file_spec)
                    531:                                                        THROW(0, 0,
                    532:                                                                &file_name,
                    533:                                                                "not found along " CLASS_PATH_NAME);
                    534:                                        }
                    535:                        if(!file_spec)
                    536:                                THROW(0, 0,
                    537:                                        &file_name,
1.149     parser    538:                                        "can not be found because no " CLASS_PATH_NAME " were specified");
1.148     parser    539:        }
1.73      paf       540: 
1.148     parser    541:        char *source=file_read_text(pool(), *file_spec, fail_on_read_problem);
1.3       paf       542:        if(!source)
1.12      paf       543:                return base_class;
1.3       paf       544: 
1.148     parser    545:        return use_buf(source, file_spec->cstr(), 0/*new class*/, name, base_class);
1.16      paf       546: }
                    547: 
1.67      paf       548: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26      paf       549:                                                                   VStateless_class *aclass, const String *name, 
                    550:                                                                   VStateless_class *base_class) {
1.5       paf       551:        // compile loaded class
1.26      paf       552:        VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1       paf       553: 
1.4       paf       554:        // locate and execute possible @auto[] static method
1.143     parser    555:        execute_nonvirtual_method(cclass, *auto_method_name, false /*no result needed*/);
1.16      paf       556:        return &cclass;
1.19      paf       557: }
                    558: 
1.77      paf       559: const String& Request::relative(const char *apath, const String& relative_name) {
                    560:        int lpath_buf_size=strlen(apath)+1;
                    561:     char *lpath=(char *)malloc(lpath_buf_size);
                    562:        memcpy(lpath, apath, lpath_buf_size);
1.120     paf       563:     if(!rsplit(lpath, '/'))
                    564:                strcpy(lpath, ".");
1.77      paf       565:        String& result=*NEW String(pool(), lpath);
1.104     paf       566:     result << "/" << relative_name;
1.19      paf       567:     return result;
                    568: }
                    569: 
1.77      paf       570: const String& Request::absolute(const String& relative_name) {
                    571:        char *relative_name_cstr=relative_name.cstr();
                    572:        if(relative_name_cstr[0]=='/') {
                    573:                String& result=*NEW String(pool(), info.document_root);
1.104     paf       574:                result << relative_name;
1.19      paf       575:                return result;
                    576:        } else 
1.77      paf       577:                return relative(info.path_translated, relative_name);
1.1       paf       578: }
1.45      paf       579: 
1.101     paf       580: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    581:                                                                 void *info) {
                    582:        String *attribute_to_exclude=static_cast<String *>(info);
                    583:        if(aattribute==*attribute_to_exclude)
                    584:                return;
                    585: 
                    586:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    587:        Pool& pool=lmeaning.pool();
                    588: 
                    589:        SAPI::add_header_attribute(pool,
1.116     paf       590:                aattribute.cstr(String::UL_AS_IS), 
1.101     paf       591:                attributed_meaning_to_string(lmeaning, String::UL_HTTP_HEADER).cstr());
                    592: }
1.90      paf       593: void Request::output_result(const VFile& body_file, bool header_only) {
1.51      paf       594:        // header: cookies
                    595:        cookie.output_result();
                    596:        
1.90      paf       597:        // set content-type
                    598:        if(String *body_file_content_type=static_cast<String *>(
                    599:                body_file.fields().get(*vfile_mime_type_name))) {
                    600:                // body file content type
                    601:                response.fields().put(*content_type_name, body_file_content_type);
                    602:        } else {
                    603:                // default content type
                    604:                response.fields().put_dont_replace(*content_type_name, 
                    605:                        default_content_type?default_content_type
                    606:                        :NEW VString(*NEW String(pool(), DEFAULT_CONTENT_TYPE)));
1.92      paf       607:        }
                    608: 
                    609:        // content-disposition
1.111     paf       610:        if(VString *vfile_name=static_cast<VString *>(body_file.fields().get(*name_name)))
                    611:                if(vfile_name->string()!=NONAME_DAT) {
                    612:                        VHash& vhash=*NEW VHash(pool());
                    613:                        vhash.hash().put(*content_disposition_filename_name, vfile_name);
                    614:                        response.fields().put(*content_disposition_name, &vhash);
                    615:                }
1.49      paf       616: 
1.62      paf       617:        // prepare header: $response:fields without :body
1.73      paf       618:        response.fields().for_each(add_header_attribute, /*excluding*/ body_name);
1.50      paf       619: 
1.62      paf       620:        // prepare...
1.90      paf       621:        const void *body=body_file.value_ptr();
                    622:        size_t content_length=body_file.value_size();
1.50      paf       623: 
1.62      paf       624:        // prepare header: content-length
1.65      paf       625:        if(content_length) { // useful for redirecting [header "location: http://..."]
                    626:                char content_length_cstr[MAX_NUMBER];
1.108     paf       627:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.69      paf       628:                SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.65      paf       629:        }
1.62      paf       630: 
                    631:        // send header
1.69      paf       632:        SAPI::send_header(pool());
1.71      paf       633:        
1.62      paf       634:        // send body
                    635:        if(!header_only)
1.69      paf       636:                SAPI::send_body(pool(), body, content_length);
1.50      paf       637: }
1.104     paf       638: 
                    639: const String& Request::mime_type_of(const char *user_file_name_cstr) {
                    640:        if(mime_types)
                    641:                if(const char *cext=strrchr(user_file_name_cstr, '.')) {
                    642:                        String sext(pool(), ++cext);
                    643:                        if(mime_types->locate(0, sext))
                    644:                                if(const String *result=mime_types->item(1))
                    645:                                        return *result;
                    646:                                else
                    647:                                        THROW(0, 0,
                    648:                                                mime_types->origin_string(),
                    649:                                                "MIME-TYPE table column elements must not be empty");
                    650:                }
                    651:        return *NEW String(pool(), "application/octet-stream");
1.133     parser    652: }

E-mail: