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

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

E-mail: