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

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

E-mail: