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

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

E-mail: