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

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

E-mail: