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

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

E-mail: