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

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

E-mail: