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

1.54      paf         1: /** @file
1.55      paf         2:        Parser: request class main part. @see compile.C and execute.C.
                      3: 
1.245     paf         4:        Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.194     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.218     paf         6: */
1.55      paf         7: 
1.245.2.4! paf         8: static const char* IDENT_REQUEST_C="$Date: 2003/01/30 14:59:44 $";
1.1       paf         9: 
1.69      paf        10: #include "pa_sapi.h"
1.53      paf        11: #include "pa_common.h"
1.1       paf        12: #include "pa_request.h"
1.3       paf        13: #include "pa_wwrapper.h"
                     14: #include "pa_vclass.h"
1.31      paf        15: #include "pa_globals.h"
1.30      paf        16: #include "pa_vint.h"
1.112     paf        17: #include "pa_vmethod_frame.h"
1.32      paf        18: #include "pa_types.h"
1.78      paf        19: #include "pa_vtable.h"
1.90      paf        20: #include "pa_vfile.h"
1.147     parser     21: #include "pa_dictionary.h"
1.208     paf        22: #include "pa_charset.h"
1.186     paf        23: #include "pa_charsets.h"
1.245.2.4! paf        24: #include "pa_cache_managers.h"
1.159     parser     25: 
1.245.2.3  paf        26: // defines
                     27: 
                     28: #define MAIN_METHOD_NAME "main"
                     29: 
                     30: // consts
                     31: 
1.207     paf        32: const char *POST_PROCESS_METHOD_NAME="postprocess";
                     33: const char *UNHANDLED_EXCEPTION_METHOD_NAME="unhandled_exception";
                     34: 
1.82      paf        35: /// content type of exception response, when no @MAIN:exception handler defined
                     36: const char *UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain";
                     37: 
                     38: /// content type of response when no $MAIN:defaults.content-type defined
                     39: const char *DEFAULT_CONTENT_TYPE="text/html";
1.144     parser     40: const char *ORIGINS_CONTENT_TYPE="text/plain";
1.82      paf        41: 
1.245.2.3  paf        42: // globals
                     43: 
1.245.2.4! paf        44: StringPtr main_method_name(new String(MAIN_METHOD_NAME));
1.245.2.3  paf        45: 
1.233     paf        46: // op.C
1.245.2.4! paf        47: VStateless_classPtr VClassMAIN_create();
1.195     paf        48: // op.C
                     49: VHash& exception2vhash(Pool& pool, const Exception& e);
1.123     paf        50: 
1.157     parser     51: //
1.245.2.4! paf        52: Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, 
        !            53:                                 uchar adefault_lang, bool status_allowed):
        !            54:        sapi_info(asapi_info),
        !            55:        request_info(arequest_info),
1.157     parser     56:        fdefault_lang(adefault_lang), flang(adefault_lang),
1.186     paf        57: 
1.245.2.4! paf        58:        main_class(VClassMAIN_create()),
        !            59:        charsets(pool(), *UTF8_charset, *UTF8_charset, *UTF8_charset) // default charsets
        !            60: {
1.178     paf        61:        // maybe expire old caches
1.245.2.4! paf        62:        cache_managers.maybe_expire();
1.178     paf        63:        
1.157     parser     64:        /// directly used
1.233     paf        65:        // MAIN class, operators
1.245.2.4! paf        66:        classes().put(main_class->name(), main_class);
1.157     parser     67:        // classes:
                     68:        // table, file, random, mail, image, ...
1.245.2.4! paf        69:        methoded_array.register_directly_used(*this);
1.157     parser     70: 
                     71:        /// methodless
                     72:        // env class
1.245.2.4! paf        73:        classes().put(StringPtr(new String(ENV_CLASS_NAME)), 
        !            74:                        ValuePtr(new VEnv(asapi_info)));
1.175     paf        75:        // status class
1.179     paf        76:        if(status_allowed)
1.245.2.4! paf        77:                classes().put(StringPtr(new String(STATUS_CLASS_NAME)), 
        !            78:                        ValuePtr(new VStatus));
1.157     parser     79:        // request class
1.245.2.4! paf        80:        classes().put(StringPtr(new String(REQUEST_CLASS_NAME)), 
        !            81:                ValuePtr(new VRequest(arequest_info, charsets)));       
1.157     parser     82:        // cookie class
1.245.2.4! paf        83:        classes().put(StringPtr(new String(COOKIE_CLASS_NAME)), 
        !            84:                ValuePtr(new VCookie));
1.157     parser     85: 
                     86:        /// methoded
                     87:        // response class
1.245.2.4! paf        88:        { 
        !            89:                ValuePtr response(new VResponse(arequest_info, charsets));
        !            90:                classes().put(response->get_class()->name(), response);
        !            91:        }
1.157     parser     92: 
                     93:        /// bases used
                     94:        // form class
1.245.2.4! paf        95:        {
        !            96:                ValuePtr form(new VForm);
        !            97:                classes().put(form->get_class()->base_class()->name(), form);   
        !            98:        }
1.213     paf        99:        // mail class
1.245.2.4! paf       100:        {
        !           101:                ValuePtr mail(new VMail);
        !           102:                classes().put(mail->get_class()->base_class()->name(), mail);   
        !           103:        }
1.157     parser    104:        // math class
1.245.2.4! paf       105:        {
        !           106:                ValuePtr math(new VMail);
        !           107:                classes().put(math->get_class()->base_class()->name(), math);   
        !           108:        }
1.117     paf       109: }
1.192     paf       110: 
                    111: Request::~Request() {
                    112: #ifdef XML
                    113:        // if for some strange reason xml generic errors failed to be reported, free them up
                    114:        if(const char *xml_generic_errors=xmlGenericErrors()) {
                    115:                SAPI::log(pool(), "warning: unreported xmlGenericErrors: %s", xml_generic_errors);
                    116:                free((void *)xml_generic_errors);
                    117:        }
                    118: #endif
                    119: }
1.236     paf       120: 
                    121: Value *Request::get_self() { return method_frame?&method_frame->self():0; }
1.192     paf       122: 
1.208     paf       123: static void load_charset(const Hash::Key& akey, Hash::Val *avalue, void *) {
                    124:        charsets->load_charset(akey, static_cast<Value *>(avalue)->as_string());
                    125: }
                    126: void Request::configure_admin(VStateless_class& conf_class, const String *source) {
                    127:        if(configure_admin_done)
                    128:                throw Exception("parser.runtime",
                    129:                source,
                    130:                "parser already configured");
                    131:        configure_admin_done=true;
                    132:        
1.227     paf       133:        // charsets must only be specified in method_frame config
1.208     paf       134:        // so that users would not interfere
                    135: 
                    136:        /* $MAIN:CHARSETS[
                    137:                        $.charsetname1[/full/path/to/charset/file.cfg]
                    138:                        ...
                    139:                ]
                    140:        */
1.237     paf       141:        if(Value *vcharsets=conf_class.get_element(*charsets_name, conf_class, false)) {
1.240     paf       142:                if(!vcharsets->is_string())
                    143:                        if(Hash *charsets=vcharsets->get_hash(0))
                    144:                                charsets->for_each(load_charset);
                    145:                        else
                    146:                                throw Exception("parser.runtime",
                    147:                                        0,
                    148:                                        "$" MAIN_CLASS_NAME ":" CHARSETS_NAME " must be hash");
1.208     paf       149:        }
                    150: 
1.227     paf       151:        // configure method_frame options
1.208     paf       152:        //      until someone with less privileges have overriden them
                    153:        methoded_array->configure_admin(*this);
                    154: }
1.150     parser    155: 
1.64      paf       156: /**
                    157:        load MAIN class, execute @main.
                    158:        MAIN class consists of all the auto.p files we'd manage to find
                    159:        plus
                    160:        the file user requested us to process
                    161:        all located classes become children of one another,
                    162:        composing class we name 'MAIN'
1.180     paf       163: 
                    164:        @test log stack trace
                    165: 
1.64      paf       166: */
1.153     parser    167: void Request::core(
1.211     paf       168:                                   const char *config_filespec, bool config_fail_on_read_problem,
1.62      paf       169:                                   bool header_only) {
1.183     paf       170: 
                    171: #ifdef RESOURCES_DEBUG
                    172: //measures
                    173: struct timeval mt[10];
                    174: //measure:before all
                    175: gettimeofday(&mt[0],NULL);
                    176: #endif
1.169     parser    177:        try {
1.29      paf       178:                char *auto_filespec=(char *)malloc(MAX_STRING);
                    179:                
1.211     paf       180:                // loading config
                    181:                if(config_filespec) {
1.77      paf       182:                        String& filespec=*NEW String(pool());
1.211     paf       183:                        filespec.APPEND_CLEAN(config_filespec, 0, "config", 0);
1.233     paf       184:                        use_file(main_class,
                    185:                                filespec, true/* ignore class_path */, 
                    186:                                config_fail_on_read_problem, true/* file must exist if 'fail on read problem' not set */);
1.29      paf       187:                }
1.8       paf       188: 
1.72      paf       189:                // loading auto.p files from document_root/.. 
                    190:                // to the one beside requested file.
                    191:                // all assigned bases from upper dir
                    192:                {
1.115     paf       193:                        const char *after=info.path_translated;
                    194:                        size_t drlen=strlen(info.document_root);
                    195:                        if(memcmp(after, info.document_root, drlen)==0) {
                    196:                                after+=drlen;
                    197:                                if(after[-1]=='/') 
                    198:                                        --after;
                    199:                        }
                    200:                        
1.152     parser    201:                        int step=0;
1.115     paf       202:                        while(const char *before=strchr(after, '/')) {
1.152     parser    203:                                String& sfile_spec=*NEW String(pool());
1.115     paf       204:                                if(after!=info.path_translated) {
1.152     parser    205:                                        sfile_spec.APPEND_CLEAN(
                    206:                                                info.path_translated, before-info.path_translated,
                    207:                                                "path-translated-scanned", step++);
                    208:                                        sfile_spec << "/" AUTO_FILE_NAME;
                    209: 
1.233     paf       210:                                        use_file(main_class,
                    211:                                                sfile_spec, true/* ignore class_path */, 
                    212:                                                true/* fail on read problem */, false/* but ignore absence, sole user */);
1.115     paf       213:                                }
                    214:                                after=before+1;
1.72      paf       215:                        }
                    216:                }
1.34      paf       217: 
1.125     paf       218:                // compile requested file
1.77      paf       219:                String& spath_translated=*NEW String(pool());
1.136     parser    220:                spath_translated.APPEND_TAINTED(info.path_translated, 0, "user-request", 0);
1.233     paf       221:                use_file(main_class,
                    222:                        spath_translated, true/* ignore class_path */, 
                    223:                        true/* fail on read problem*/, true/* fail on abscence */);
1.214     paf       224: 
1.227     paf       225:                // configure method_frame options if not configured yet
1.214     paf       226:                if(!configure_admin_done)
1.233     paf       227:                        configure_admin(main_class, 0);
1.7       paf       228: 
1.227     paf       229:                // configure not-method_frame=user options
1.124     paf       230:                methoded_array->configure_user(*this);
                    231: 
1.85      paf       232:                // $MAIN:MIME-TYPES
1.237     paf       233:                if(Value *element=main_class.get_element(*mime_types_name, main_class, false))
1.85      paf       234:                        if(Table *table=element->get_table())
                    235:                                mime_types=table;                       
1.102     paf       236: 
1.124     paf       237:                // filling form fields
1.153     parser    238:                form.fill_fields_and_tables(*this);
1.102     paf       239: 
1.124     paf       240:                // filling cookies
                    241:                cookie.fill_fields(*this);
1.213     paf       242: 
                    243:                // filling mail received
                    244:                mail.fill_received(*this);
1.25      paf       245: 
1.183     paf       246: #ifdef RESOURCES_DEBUG
                    247: //measure:after compile
                    248: gettimeofday(&mt[1],NULL);
                    249: #endif
1.25      paf       250:                // execute @main[]
1.233     paf       251:                const String *body_string=execute_virtual_method(main_class, main_method_name);
1.41      paf       252:                if(!body_string)
1.197     paf       253:                        throw Exception("parser.runtime",
1.145     parser    254:                                0, 
                    255:                                "'"MAIN_METHOD_NAME"' method not found");
1.79      paf       256: 
1.183     paf       257: #ifdef RESOURCES_DEBUG
                    258:                //measure:after main
                    259: gettimeofday(&mt[2],NULL);
                    260: #endif
                    261: 
1.91      paf       262:                VString body_vstring_before_post_process(*body_string);
                    263:                VString *body_vstring_after_post_process=&body_vstring_before_post_process;
1.119     paf       264:                // @postprocess
1.233     paf       265:                if(Value *value=main_class.get_element(
1.245.2.4! paf       266:                                StringPtr(new String(POST_PROCESS_METHOD_NAME), 
1.237     paf       267:                                main_class, 
1.226     paf       268:                                false))
1.91      paf       269:                        if(Junction *junction=value->get_junction())
                    270:                                if(const Method *method=junction->method) {
                    271:                                        // preparing to pass parameters to 
1.119     paf       272:                                        //      @postprocess[data]
1.234     paf       273:                                        VMethodFrame frame(pool(), method->name, *junction, 0/*no parent*/);
1.233     paf       274:                                        frame.set_self(main_class);
1.91      paf       275: 
1.202     paf       276:                                        frame.store_param(&body_vstring_before_post_process);
1.91      paf       277:                                        body_vstring_after_post_process=
1.188     paf       278:                                                NEW VString(execute_method(frame, *method));
1.91      paf       279:                                }
1.90      paf       280: 
1.204     paf       281:                bool lorigins_mode=origins_mode();
1.144     parser    282: 
                    283:                const VFile *body_file=body_vstring_after_post_process->as_vfile(
1.204     paf       284:                        String::UL_UNSPECIFIED, lorigins_mode);
1.41      paf       285: 
1.45      paf       286:                // extract response body
                    287:                Value *body_value=static_cast<Value *>(
1.244     paf       288:                        response.fields().get(*download_name));
                    289:                bool as_attachment=body_value!=0;
                    290:                if(!body_value)
                    291:                        body_value=static_cast<Value *>(
                    292:                                response.fields().get(*body_name));
                    293: 
1.216     paf       294:                if(body_value) // there is some $response:body
1.90      paf       295:                        body_file=body_value->as_vfile();
1.204     paf       296:                else if(lorigins_mode)
1.144     parser    297:                        response.fields().put(*content_type_name, 
1.245.2.4! paf       298:                                NEW VString(StringPtr(new String(ORIGINS_CONTENT_TYPE)));
1.45      paf       299: 
1.183     paf       300: #ifdef RESOURCES_DEBUG
                    301: //measure:after postprocess
                    302: gettimeofday(&mt[3],NULL);             
                    303: #endif
                    304: 
1.45      paf       305:                // OK. write out the result
1.244     paf       306:                output_result(*body_file, header_only, as_attachment);
1.183     paf       307: 
                    308: #ifdef RESOURCES_DEBUG
                    309:                //measure:after output_result
                    310: gettimeofday(&mt[9],NULL);             
                    311: t[9]=mt[9].tv_sec+mt[9].tv_usec/1000000.0;
                    312: 
                    313: double t[10];
                    314: for(int i=0;i<10;i++)
                    315:     t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
                    316: //measure:log2 compile,main,postprocess,output
                    317: SAPI::log(pool(), "rmeasure: %s,%.2f,%.2f,%.2f %.2f,%.2f %.2f", 
                    318: info.uri,
                    319: t[1]-t[0],
                    320: t[2]-t[1],
                    321: t[3]-t[2],
                    322: sql_connect_time,sql_request_time,
                    323: t[9]-t[3]
                    324: );
                    325: #endif
1.171     parser    326:        } catch(const Exception& e) { // request handling problem
1.76      paf       327:                // we're returning not result, but error explanation
1.169     parser    328:                try {
1.76      paf       329:                        // log the beast
1.205     paf       330:                        if(const String *problem_source=e.problem_source())
1.76      paf       331:                                SAPI::log(pool(),
1.199     paf       332:                                        "%s: "
1.76      paf       333: #ifndef NO_STRING_ORIGIN
1.180     paf       334:                                        ORIGIN_FILE_LINE_FORMAT": "
1.76      paf       335: #endif
1.197     paf       336:                                        "'%s' %s [%s]",
1.199     paf       337:                                        info.uri,
1.76      paf       338: #ifndef NO_STRING_ORIGIN
                    339:                                        problem_source->origin().file?problem_source->origin().file:"global",
                    340:                                        problem_source->origin().line,
                    341: #endif
1.173     paf       342:                                        problem_source->cstr(),
1.76      paf       343:                                        e.comment(),
1.206     paf       344:                                        e.type()
1.76      paf       345:                                );
                    346:                        else
                    347:                                SAPI::log(pool(),
1.199     paf       348:                                        "%s: "
1.197     paf       349:                                        "%s [%s]",
1.199     paf       350:                                        info.uri,
1.206     paf       351:                                        e.comment(), e.type()
1.180     paf       352:                                );
1.171     parser    353: 
1.43      paf       354:                        // reset language to default
                    355:                        flang=fdefault_lang;
                    356:                        
                    357:                        // reset response
                    358:                        response.fields().clear();
                    359: 
                    360:                        // this is what we'd return in $response:body
1.41      paf       361:                        const String *body_string=0;
1.43      paf       362: 
1.233     paf       363:                        // maybe we'd be lucky enough as to report an error
                    364:                        // in a gracefull way...
                    365:                        if(Value *value=main_class.get_element(
1.245.2.4! paf       366:                                        StringPtr(new String(UNHANDLED_EXCEPTION_METHOD_NAME), 
1.237     paf       367:                                        main_class,
1.233     paf       368:                                        false)) {
                    369:                                if(Junction *junction=value->get_junction()) {
                    370:                                        if(const Method *method=junction->method) {
                    371:                                                // preparing to pass parameters to 
                    372:                                                //      @unhandled_exception[exception;stack]
1.234     paf       373:                                                VMethodFrame frame(pool(), method->name, *junction, 0/*no caller*/);
1.233     paf       374:                                                frame.set_self(main_class);
                    375: 
                    376:                                                // $exception
                    377:                                                frame.store_param(&exception2vhash(pool(), e));
                    378:                                                // $stack[^table::set{name      origin}]
                    379:                                                Array& stack_trace_columns=*NEW Array(pool());
                    380:                                                stack_trace_columns+=NEW String(pool(), "name");
                    381:                                                stack_trace_columns+=NEW String(pool(), "file");
                    382:                                                stack_trace_columns+=NEW String(pool(), "lineno");
                    383:                                                Table& stack_trace=*NEW Table(pool(), 0, &stack_trace_columns);
                    384:                                                Array_iter tracei(exception_trace);
                    385:                                                while(tracei.has_next()) {
                    386:                                                        Array& row=*NEW Array(pool());
1.171     parser    387: 
1.233     paf       388:                                                        const String *name=(const String *)tracei.next();
                    389:                                                        row+=name; // name column
1.171     parser    390: #ifndef NO_STRING_ORIGIN
1.233     paf       391:                                                        const Origin& origin=name->origin();
                    392:                                                        if(origin.file) {
                    393:                                                                row+=NEW String(pool(), origin.file, 0, true); // file column
                    394:                                                                char *buf=(char *)malloc(MAX_NUMBER);
                    395:                                                                size_t buf_size=snprintf(buf, MAX_NUMBER, "%d", 1+origin.line);
                    396:                                                                row+=NEW String(pool(), buf, buf_size, true); // line column
                    397:                                                        }
1.171     parser    398: #endif
1.233     paf       399:                                                        stack_trace+=&row;
                    400:                                                }
                    401:                                                frame.store_param(NEW VTable(pool(), &stack_trace));
1.171     parser    402: 
1.233     paf       403:                                                // future $response:body=
                    404:                                                //   execute ^unhandled_exception[exception;stack]
                    405:                                                body_string=&execute_method(frame, *method);
                    406:                                        }
                    407:                                }
1.30      paf       408:                        }
                    409:                        
1.41      paf       410:                        if(!body_string) {  // couldn't report an error beautifully?
                    411:                                // doing that ugly
                    412: 
                    413:                                // make up result: $origin $source $comment $type $code
                    414:                                char *buf=(char *)malloc(MAX_STRING);
1.30      paf       415:                                size_t printed=0;
1.205     paf       416:                                if(const String *problem_source=e.problem_source()) {
1.16      paf       417: #ifndef NO_STRING_ORIGIN
1.30      paf       418:                                        const Origin& origin=problem_source->origin();
                    419:                                        if(origin.file)
1.180     paf       420:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, 
                    421:                                                        ORIGIN_FILE_LINE_FORMAT": ", 
                    422:                                                        origin.file, 1+origin.line
                    423:                                                );
1.28      paf       424: #endif
1.41      paf       425:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ", 
1.173     paf       426:                                                problem_source->cstr());
1.30      paf       427:                                }
1.206     paf       428:                                if(const char *comment=e.comment(true))
                    429:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", comment);
                    430:                                if(const char *type=e.type(true)) 
1.197     paf       431:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "  type: %s",  type);
1.43      paf       432: 
                    433:                                // future $response:content-type
1.49      paf       434:                                response.fields().put(*content_type_name, 
1.245.2.4! paf       435:                                        NEW VString(StringPtr(new String(UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.43      paf       436:                                // future $response:body
1.44      paf       437:                                body_string=NEW String(pool(), buf);
1.30      paf       438:                        }
1.41      paf       439: 
1.144     parser    440:                        VString body_vstring(*body_string);
1.90      paf       441:                        const VFile *body_file=body_vstring.as_vfile();
                    442: 
1.45      paf       443:                        // ERROR. write it out
1.244     paf       444:                        output_result(*body_file, header_only, false);
1.170     parser    445:                } catch(const Exception& ) {
1.169     parser    446:                        /*re*/throw;
1.3       paf       447:                }
1.1       paf       448:        }
                    449: }
                    450: 
1.245.2.3  paf       451: VStateless_classPtr Request::use_file(VStateless_class& aclass,
1.245.2.4! paf       452:                                                                        StringPtr file_name, bool ignore_class_path, 
1.233     paf       453:                                                                        bool fail_on_read_problem, bool fail_on_file_absence) {
1.73      paf       454:        // cyclic dependence check
1.148     parser    455:        if(used_files.get(file_name))
1.233     paf       456:                return 0;
1.245.2.2  paf       457:        used_files.put(file_name, true);
1.73      paf       458: 
1.148     parser    459:        const String *file_spec;
1.153     parser    460:        if(ignore_class_path) // ignore_class_path?
1.148     parser    461:                file_spec=&file_name;
1.153     parser    462:        else if(file_name.first_char()=='/') //absolute path, no need to scan MAIN:CLASS_PATH?
                    463:                file_spec=&absolute(file_name);
                    464:        else {
                    465:                file_spec=0;
1.237     paf       466:                if(Value *element=main_class.get_element(*class_path_name, main_class, false)) {
1.233     paf       467:                        if(element->is_string()) {
                    468:                                file_spec=file_readable(absolute(element->as_string()), file_name); // found at class_path?
                    469:                        } else if(Table *table=element->get_table()) {
                    470:                                int size=table->size();
                    471:                                for(int i=size; i--; ) {
                    472:                                        const String& path=*static_cast<Array *>(table->get(i))->get_string(0);
                    473:                                        if(file_spec=file_readable(absolute(path), file_name))
                    474:                                                break; // found along class_path
                    475:                                }
                    476:                        } else
                    477:                                throw Exception("parser.runtime",
                    478:                                        0,
                    479:                                        "$" CLASS_PATH_NAME " must be string or table");
                    480:                        if(!file_spec)
                    481:                                throw Exception("parser.runtime",
                    482:                                        &file_name,
                    483:                                        "not found along " MAIN_CLASS_NAME ":" CLASS_PATH_NAME);
                    484:                }
1.153     parser    485:                if(!file_spec)
1.197     paf       486:                        throw Exception("parser.runtime",
1.153     parser    487:                                &file_name,
1.203     paf       488:                                "usage failed - no $" MAIN_CLASS_NAME  ":" CLASS_PATH_NAME " were specified");
1.148     parser    489:        }
1.230     paf       490: 
                    491:        if(fail_on_read_problem && !fail_on_file_absence) // ignore file absence if asked for
                    492:                if(!entry_exists(*file_spec))
1.233     paf       493:                        return 0;
1.73      paf       494: 
1.148     parser    495:        char *source=file_read_text(pool(), *file_spec, fail_on_read_problem);
1.3       paf       496:        if(!source)
1.233     paf       497:                return 0;
1.3       paf       498: 
1.233     paf       499:        return use_buf(aclass, source, *file_spec, file_spec->cstr());
1.16      paf       500: }
                    501: 
1.208     paf       502: 
1.245.2.3  paf       503: VStateless_classPtr Request::use_buf(VStateless_class& aclass,
1.233     paf       504:                                                                   const char *source, 
                    505:                                                                   const String& filespec, const char *filespec_cstr) {
                    506:        // temporary zero @conf so to maybe-replace it in compiled code
                    507:        Temp_method temp_method_conf(aclass, *conf_method_name, 0);
                    508:        // temporary zero @auto so to maybe-replace it in compiled code
                    509:        Temp_method temp_method_auto(aclass, *auto_method_name, 0);
                    510: 
1.5       paf       511:        // compile loaded class
1.233     paf       512:        VStateless_class& cclass=COMPILE(aclass, source, filespec_cstr);
1.212     paf       513: 
1.233     paf       514:        // locate and execute possible @conf[] static
1.212     paf       515:        VString *vfilespec=NEW VString(filespec);
1.209     paf       516:        const Method *method_called;
1.212     paf       517:        execute_nonvirtual_method(cclass, 
1.233     paf       518:                *conf_method_name, vfilespec,
1.212     paf       519:                0/*no result needed*/, &method_called);
1.233     paf       520:        if(method_called)
1.209     paf       521:                configure_admin(cclass, &method_called->name);
1.208     paf       522: 
                    523:        // locate and execute possible @auto[] static
1.233     paf       524:        execute_nonvirtual_method(cclass, 
                    525:                *auto_method_name, vfilespec,
1.212     paf       526:                0/*no result needed*/);
1.16      paf       527:        return &cclass;
1.19      paf       528: }
                    529: 
1.77      paf       530: const String& Request::relative(const char *apath, const String& relative_name) {
1.217     paf       531:        int hpath_buf_size=strlen(apath)+1;
                    532:     char *hpath=(char *)malloc(hpath_buf_size);
                    533:        memcpy(hpath, apath, hpath_buf_size);
                    534:        String& result=*NEW String(pool());
                    535:     if(rsplit(hpath, '/')) // if something/splitted
                    536:                result << hpath << "/";
                    537:     result << relative_name;
1.19      paf       538:     return result;
                    539: }
                    540: 
1.77      paf       541: const String& Request::absolute(const String& relative_name) {
1.217     paf       542:        if(relative_name.first_char()=='/') {
1.245.2.4! paf       543:                String& result=StringPtr(new String(info.document_root);
1.104     paf       544:                result << relative_name;
1.19      paf       545:                return result;
                    546:        } else 
1.239     paf       547:                return relative_name.pos("://")<0? relative(info.path_translated, relative_name)
                    548:                        :relative_name; // something like "http://xxx"
1.1       paf       549: }
1.45      paf       550: 
1.101     paf       551: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    552:                                                                 void *info) {
1.178     paf       553:        Request& r=*static_cast<Request *>(info);
1.184     paf       554:        if(aattribute==BODY_NAME
1.244     paf       555:                || aattribute==DOWNLOAD_NAME
1.184     paf       556:                || aattribute==CHARSET_NAME)
1.101     paf       557:                return;
                    558: 
                    559:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    560:        Pool& pool=lmeaning.pool();
                    561: 
                    562:        SAPI::add_header_attribute(pool,
1.173     paf       563:                aattribute.cstr(), 
1.243     paf       564:                attributed_meaning_to_string(lmeaning, String::UL_HTTP_HEADER, false)
                    565:                        .cstr(String::UL_UNSPECIFIED));
1.101     paf       566: }
1.244     paf       567: void Request::output_result(const VFile& body_file, bool header_only, bool as_attachment) {
1.51      paf       568:        // header: cookies
                    569:        cookie.output_result();
                    570:        
1.223     paf       571:        Value *body_file_content_type;
1.90      paf       572:        // set content-type
1.223     paf       573:        if(body_file_content_type=static_cast<Value *>(
1.222     paf       574:                body_file.fields().get(*content_type_name))) {
1.90      paf       575:                // body file content type
                    576:                response.fields().put(*content_type_name, body_file_content_type);
                    577:        } else {
                    578:                // default content type
                    579:                response.fields().put_dont_replace(*content_type_name, 
                    580:                        default_content_type?default_content_type
1.245.2.4! paf       581:                        :NEW VString(StringPtr(new String(DEFAULT_CONTENT_TYPE)));
1.92      paf       582:        }
                    583: 
                    584:        // content-disposition
1.111     paf       585:        if(VString *vfile_name=static_cast<VString *>(body_file.fields().get(*name_name)))
                    586:                if(vfile_name->string()!=NONAME_DAT) {
                    587:                        VHash& vhash=*NEW VHash(pool());
1.242     paf       588:                        Hash& hash=vhash.hash(0);
1.244     paf       589:                        if(as_attachment)
                    590:                                hash.put(*value_name, NEW VString(*content_disposition_value));
1.242     paf       591:                        hash.put(*content_disposition_filename_name, vfile_name);
1.111     paf       592:                        response.fields().put(*content_disposition_name, &vhash);
                    593:                }
1.49      paf       594: 
1.62      paf       595:        // prepare header: $response:fields without :body
1.178     paf       596:        response.fields().for_each(add_header_attribute, this);
1.50      paf       597: 
1.184     paf       598:        const void *client_body;
                    599:        size_t client_content_length;
1.220     paf       600:        // transcode text body when "text/*" or simple result
1.223     paf       601:        if(body_file_content_type)
                    602:                if(Hash *hash=body_file_content_type->get_hash(0))
                    603:                        body_file_content_type=static_cast<Value *>(hash->get(*value_name));
1.220     paf       604:        if(!body_file_content_type/*vstring.as_vfile*/ || body_file_content_type->as_string().pos("text/")==0) {
                    605:                Charset::transcode(pool(),
                    606:                        pool().get_source_charset(), body_file.value_ptr(), body_file.value_size(),
                    607:                        pool().get_client_charset(), client_body, client_content_length
                    608:                );
                    609:        } else {
                    610:                client_body=body_file.value_ptr();
                    611:                client_content_length=body_file.value_size();
                    612:        }
1.50      paf       613: 
1.62      paf       614:        // prepare header: content-length
1.200     paf       615:        char content_length_cstr[MAX_NUMBER];
                    616:        snprintf(content_length_cstr, MAX_NUMBER, "%u", client_content_length);
                    617:        SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.62      paf       618: 
                    619:        // send header
1.69      paf       620:        SAPI::send_header(pool());
1.71      paf       621:        
1.62      paf       622:        // send body
                    623:        if(!header_only)
1.184     paf       624:                SAPI::send_body(pool(), client_body, client_content_length);
1.50      paf       625: }
1.104     paf       626: 
                    627: const String& Request::mime_type_of(const char *user_file_name_cstr) {
                    628:        if(mime_types)
                    629:                if(const char *cext=strrchr(user_file_name_cstr, '.')) {
                    630:                        String sext(pool(), ++cext);
1.221     paf       631:                        if(mime_types->locate(0, sext.change_case(pool(), String::CC_LOWER)))
1.104     paf       632:                                if(const String *result=mime_types->item(1))
                    633:                                        return *result;
                    634:                                else
1.197     paf       635:                                        throw Exception("parser.runtime",
1.104     paf       636:                                                mime_types->origin_string(),
1.212     paf       637:                                                MIME_TYPES_NAME  " table column elements must not be empty");
1.104     paf       638:                }
1.245.2.4! paf       639:        return StringPtr(new String("application/octet-stream");
1.233     paf       640: }
                    641: 
                    642: bool Request::origins_mode() {
1.237     paf       643:        return main_class.get_element(*origins_mode_name, main_class, false)!=0;  // $ORIGINS mode
1.133     parser    644: }

E-mail: