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

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

E-mail: