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

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

E-mail: