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

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.76    ! paf         8:        $Id: pa_request.C,v 1.75 2001/03/24 11:33:27 paf Exp $
1.1       paf         9: */
                     10: 
1.70      paf        11: #include "pa_config_includes.h"
1.19      paf        12: 
1.69      paf        13: #include "pa_sapi.h"
1.53      paf        14: #include "pa_common.h"
1.1       paf        15: #include "pa_request.h"
1.3       paf        16: #include "pa_wwrapper.h"
                     17: #include "pa_vclass.h"
1.11      paf        18: #include "_root.h"
1.17      paf        19: #include "_table.h"
1.31      paf        20: #include "pa_globals.h"
1.30      paf        21: #include "pa_vint.h"
                     22: #include "pa_vmframe.h"
1.32      paf        23: #include "pa_types.h"
1.3       paf        24: 
1.72      paf        25: /// $limits.post_max_size default 10M
                     26: const size_t MAX_POST_SIZE_DEFAULT=10*0x400*400;
                     27: 
                     28: //
1.28      paf        29: Request::Request(Pool& apool,
1.33      paf        30:                                 Info& ainfo,
1.43      paf        31:                                 String::Untaint_lang adefault_lang) : Pooled(apool),
1.3       paf        32:        stack(apool),
1.41      paf        33:        ROOT(apool),
                     34:        env(apool),
                     35:        form(apool),
                     36:        request(apool, *this),
                     37:        response(apool),
1.51      paf        38:        cookie(apool),
1.3       paf        39:        fclasses(apool),
1.43      paf        40:        fdefault_lang(adefault_lang), flang(adefault_lang),
                     41:        info(ainfo),
1.74      paf        42:        default_content_type(0),
1.73      paf        43:        used_files(apool)
1.3       paf        44: {
1.27      paf        45:        // root superclass, 
1.3       paf        46:        //   parent of all classes, 
                     47:        //   operators holder
1.41      paf        48:        initialize_root_class(pool(), ROOT);
                     49:        classes().put(*root_class_name, &ROOT);
1.27      paf        50:        // table class
                     51:        classes().put(*table_class_name, table_class);  
1.3       paf        52: 
                     53:        // env class
1.41      paf        54:        classes().put(*env_class_name, &env);
1.27      paf        55:        // form class
1.41      paf        56:        classes().put(*form_class_name, &form); 
1.40      paf        57:        // request class
1.41      paf        58:        classes().put(*request_class_name, &request);   
                     59:        // response class
                     60:        classes().put(*response_class_name, &response); 
1.51      paf        61:        // cookie class
                     62:        classes().put(*cookie_class_name, &cookie);
1.3       paf        63: }
1.1       paf        64: 
1.62      paf        65: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                     66:                                                                 void *info) {
1.48      paf        67:        String *attribute_to_exclude=static_cast<String *>(info);
                     68:        if(aattribute==*attribute_to_exclude)
1.45      paf        69:                return;
                     70: 
1.62      paf        71:        Value& lmeaning=*static_cast<Value *>(ameaning);
                     72:        Pool& pool=lmeaning.pool();
1.45      paf        73: 
1.62      paf        74:        String attribute(pool);
1.69      paf        75:        SAPI::add_header_attribute(pool,
1.62      paf        76:                attribute.append(aattribute, String::UL_HEADER, true).cstr(), 
                     77:                attributed_meaning_to_string(lmeaning).cstr());
1.45      paf        78: }
                     79: 
1.64      paf        80: /**
                     81:        load MAIN class, execute @main.
                     82:        MAIN class consists of all the auto.p files we'd manage to find
                     83:        plus
                     84:        the file user requested us to process
                     85:        all located classes become children of one another,
                     86:        composing class we name 'MAIN'
                     87: */
1.63      paf        88: void Request::core(const char *root_auto_path, bool root_auto_fail,
                     89:                                   const char *site_auto_path, bool site_auto_fail,
1.62      paf        90:                                   bool header_only) {
1.29      paf        91:        VStateless_class *main_class=0;
1.41      paf        92:        bool need_rethrow=false;  Exception rethrow_me;
1.3       paf        93:        TRY {
1.29      paf        94:                char *auto_filespec=(char *)malloc(MAX_STRING);
                     95:                
1.63      paf        96:                // loading root auto.p 
                     97:                if(root_auto_path) {
1.64      paf        98:                        strncpy(auto_filespec, root_auto_path, MAX_STRING-strlen("/" AUTO_FILE_NAME));
                     99:                        strcat(auto_filespec, "/" AUTO_FILE_NAME);
1.29      paf       100:                        main_class=use_file(
1.63      paf       101:                                auto_filespec, root_auto_fail,
1.37      paf       102:                                main_class_name, main_class);
1.29      paf       103:                }
                    104: 
1.72      paf       105:                // $MAIN:limits hash used here,
                    106:                //      until someone with less privileges have overriden them
                    107:                Value *limits=main_class?main_class->get_element(*limits_name):0;
                    108:                Value *element;
                    109:                // $limits.post_max_size default 10M
                    110:                element=limits?limits->get_element(*post_max_size_name):0;
                    111:                size_t value=element?(size_t)element->get_double():0;
                    112:                size_t post_max_size=value?value:MAX_POST_SIZE_DEFAULT;
                    113: 
                    114:                form.fill_fields(*this, post_max_size);
                    115:                cookie.fill_fields(*this);
                    116: 
1.64      paf       117:                // loading site auto.p
1.63      paf       118:                if(site_auto_path) {
1.64      paf       119:                        strncpy(auto_filespec, site_auto_path, MAX_STRING-strlen("/" AUTO_FILE_NAME));
                    120:                        strcat(auto_filespec, "/" AUTO_FILE_NAME);
1.37      paf       121:                        main_class=use_file(
1.63      paf       122:                                auto_filespec, site_auto_fail,
1.37      paf       123:                                main_class_name, main_class);
1.29      paf       124:                }
1.8       paf       125: 
1.72      paf       126:                // loading auto.p files from document_root/.. 
                    127:                // to the one beside requested file.
                    128:                // all assigned bases from upper dir
                    129:                {
                    130:                        /* /document/root */
                    131:                        char *monkey_auto_path=
                    132:                                (char *)malloc(strlen(info.path_translated)+strlen(AUTO_FILE_NAME)+1);
                    133:                        strcpy(monkey_auto_path, info.document_root);
                    134: 
                    135:                        /* /requested/file.html */
                    136:                        char *branches=(char *)malloc(strlen(info.path_translated)+1);
                    137:                        strcpy(branches, info.path_translated+strlen(info.document_root));
                    138:                        char *next=branches;
                    139: 
                    140:                        char *append_here=monkey_auto_path+strlen(monkey_auto_path);
                    141:                        size_t slash_auto_p_size=strlen("/" AUTO_FILE_NAME);
                    142:                        while(true) {
                    143:                                char *step=lsplit(&next, '/');
                    144:                                if(next) { // not 'file.html' part
                    145:                                        size_t step_size=strlen(step);
                    146:                                        memcpy(append_here, step, step_size);
                    147:                                        append_here+=step_size;
                    148:                                        memcpy(append_here, "/" AUTO_FILE_NAME, slash_auto_p_size+1);
                    149:                                        append_here++/* / */;
                    150: 
                    151:                                        main_class=use_file(monkey_auto_path, false/*ignore read problem*/,
                    152:                                                main_class_name, main_class);
                    153:                                } else
                    154:                                        break;
                    155:                        }
                    156:                }
1.34      paf       157: 
1.72      paf       158:                // compiling requested file
                    159:                main_class=use_file(info.path_translated, true/*don't ignore read problem*/,
                    160:                        main_class_name, main_class);
1.7       paf       161: 
1.42      paf       162:                // $MAIN:defaults
                    163:                Value *defaults=main_class?main_class->get_element(*defaults_name):0;
1.75      paf       164:                // value must be allocated on request's pool for that pool used on
                    165:                // meaning constructing @see attributed_meaning_to_string
1.74      paf       166:                default_content_type=defaults?
                    167:                        defaults->get_element(*content_type_name)
1.76    ! paf       168:                        :NEW VString(*NEW String(pool(), "text/html"));
1.25      paf       169: 
                    170:                // execute @main[]
1.41      paf       171:                const String *body_string=execute_method(*main_class, *main_method_name);
                    172:                if(!body_string)
1.25      paf       173:                        THROW(0,0,
                    174:                        0, 
                    175:                        "'"MAIN_METHOD_NAME"' method not found");
1.41      paf       176: 
1.45      paf       177:                // extract response body
                    178:                Value *body_value=static_cast<Value *>(
                    179:                        response.fields().get(*body_name));
                    180:                if(body_value) // there is some $request.body
                    181:                        body_string=&body_value->as_string();// TODO: IMAGE&FILE
                    182: 
                    183:                // OK. write out the result
1.62      paf       184:                output_result(*body_string, header_only);
1.3       paf       185:        } 
1.76    ! paf       186:        CATCH(e) { // request handling problem
        !           187:                // we're returning not result, but error explanation
1.30      paf       188:                TRY {
1.76    ! paf       189:                        // log the beast
        !           190:                        const String *problem_source=e.problem_source();
        !           191:                        if(problem_source)
        !           192:                                SAPI::log(pool(),
        !           193: #ifndef NO_STRING_ORIGIN
        !           194:                                        "%s(%d): "
        !           195: #endif
        !           196:                                        "'%s' %s [%s %s]",
        !           197: #ifndef NO_STRING_ORIGIN
        !           198:                                        problem_source->origin().file?problem_source->origin().file:"global",
        !           199:                                        problem_source->origin().line,
        !           200: #endif
        !           201:                                        problem_source->cstr(),
        !           202:                                        e.comment(),
        !           203:                                        e.type()?e.type()->cstr():"-",
        !           204:                                        e.code()?e.code()->cstr():"-"
        !           205:                                );
        !           206:                        else
        !           207:                                SAPI::log(pool(),
        !           208:                                        "%s [%s %s]",
        !           209:                                        e.comment(),
        !           210:                                        e.type()?e.type()->cstr():"-",
        !           211:                                        e.code()?e.code()->cstr():"-"
        !           212:                                        );
1.43      paf       213: 
                    214:                        // reset language to default
                    215:                        flang=fdefault_lang;
                    216:                        
                    217:                        // reset response
                    218:                        response.fields().clear();
                    219: 
                    220:                        // this is what we'd return in $response:body
1.41      paf       221:                        const String *body_string=0;
1.43      paf       222: 
1.30      paf       223:                        if(main_class) { // we've managed to end up with some main_class
                    224:                                // maybe we'd be lucky enough as to report an error
                    225:                                // in a gracefull way...
                    226:                                if(Value *value=main_class->get_element(*exception_method_name))
                    227:                                        if(Junction *junction=value->get_junction())
                    228:                                                if(const Method *method=junction->method) {
                    229:                                                        // preparing to pass parameters to 
                    230:                                                        //      @exception[origin;source;comment;type;code]
1.38      paf       231:                                                        VMethodFrame frame(pool(), *junction);
                    232:                                                        frame.set_self(*main_class);
1.30      paf       233: 
                    234:                                                        const String *problem_source=e.problem_source();
                    235:                                                        // origin
1.44      paf       236:                                                        String origin_name(pool(), "origin");
1.38      paf       237:                                                        Value *origin_value=0;
1.30      paf       238: #ifndef NO_STRING_ORIGIN
1.38      paf       239:                                                        if(problem_source) {
                    240:                                                                const Origin& origin=problem_source->origin();
                    241:                                                                if(origin.file) {
                    242:                                                                        char *buf=(char *)malloc(MAX_STRING);
1.42      paf       243:                                                                        snprintf(buf, MAX_STRING, "%s(%d):", 
1.38      paf       244:                                                                                origin.file, 1+origin.line);
1.44      paf       245:                                                                        String *origin_file_line=NEW String(pool(),
                    246:                                                                                buf, true);
1.38      paf       247:                                                                        origin_value=NEW VString(*origin_file_line);
                    248:                                                                }
1.30      paf       249:                                                        }
                    250: #endif
1.38      paf       251:                                                        frame.store_param(origin_name, 
                    252:                                                                origin_value?origin_value:NEW VUnknown(pool()));
1.28      paf       253: 
1.30      paf       254:                                                        // source
1.44      paf       255:                                                        String source_name(pool(), "source");
1.38      paf       256:                                                        Value *source_value=0;
1.43      paf       257:                                                        if(problem_source) {
1.47      paf       258:                                                                String& problem_source_copy=*NEW String(pool());
                    259:                                                                problem_source_copy.append(*problem_source, 
                    260:                                                                        flang, true);
1.43      paf       261:                                                                source_value=NEW VString(problem_source_copy);
                    262:                                                        }
1.38      paf       263:                                                        frame.store_param(source_name, 
                    264:                                                                source_value?source_value:NEW VUnknown(pool()));
1.30      paf       265: 
                    266:                                                        // comment
1.44      paf       267:                                                        String comment_name(pool(), "comment");
                    268:                                                        String *comment_value=NEW String(pool(),
                    269:                                                                e.comment(), true);
1.38      paf       270:                                                        frame.store_param(comment_name, 
1.30      paf       271:                                                                NEW VString(*comment_value));
                    272: 
                    273:                                                        // type
1.44      paf       274:                                                        String type_name(pool(), "type");
1.30      paf       275:                                                        Value *type_value;
1.43      paf       276:                                                        if(e.type()) {
1.47      paf       277:                                                                String& type_copy=*NEW String(pool());
                    278:                                                                type_value=NEW VString(type_copy.append(*e.type(), 
                    279:                                                                        flang, true));
1.43      paf       280:                                                        } else
1.30      paf       281:                                                                type_value=NEW VUnknown(pool());
1.38      paf       282:                                                        frame.store_param(type_name, type_value);
1.30      paf       283: 
                    284:                                                        // code
1.44      paf       285:                                                        String code_name(pool(), "code");
1.30      paf       286:                                                        Value *code_value;
1.43      paf       287:                                                        if(e.code()) {
1.47      paf       288:                                                                String& code_copy=*NEW String(pool());
                    289:                                                                code_value=NEW VString(code_copy.append(*e.code(), 
                    290:                                                                        flang, true));
1.43      paf       291:                                                        } else
1.30      paf       292:                                                                code_value=NEW VUnknown(pool());
1.38      paf       293:                                                        frame.store_param(code_name, code_value);
1.30      paf       294: 
1.43      paf       295:                                                        // future $response:body=
                    296:                                                        //   execute ^exception[origin;source;comment;type;code]
                    297:                                                        body_string=execute_method(frame, *method);
1.30      paf       298:                                                }
                    299:                        }
                    300:                        
1.41      paf       301:                        if(!body_string) {  // couldn't report an error beautifully?
                    302:                                // doing that ugly
                    303: 
                    304:                                // make up result: $origin $source $comment $type $code
                    305:                                char *buf=(char *)malloc(MAX_STRING);
1.30      paf       306:                                size_t printed=0;
                    307:                                const String *problem_source=e.problem_source();
                    308:                                if(problem_source) {
1.16      paf       309: #ifndef NO_STRING_ORIGIN
1.30      paf       310:                                        const Origin& origin=problem_source->origin();
                    311:                                        if(origin.file)
1.41      paf       312:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ", 
1.30      paf       313:                                                origin.file, 1+origin.line);
1.28      paf       314: #endif
1.41      paf       315:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ", 
1.30      paf       316:                                                problem_source->cstr());
                    317:                                }
1.41      paf       318:                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", 
1.30      paf       319:                                        e.comment());
                    320:                                const String *type=e.type();
                    321:                                if(type) {
1.41      paf       322:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "  type: %s", 
1.30      paf       323:                                                type->cstr());
                    324:                                        const String *code=e.code();
                    325:                                        if(code)
1.41      paf       326:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s", 
1.30      paf       327:                                                code->cstr());
                    328:                                }
1.43      paf       329: 
                    330:                                // future $response:content-type
1.49      paf       331:                                response.fields().put(*content_type_name, 
                    332:                                        NEW VString(*NEW String(pool(), "text/plain")));
1.43      paf       333:                                // future $response:body
1.44      paf       334:                                body_string=NEW String(pool(), buf);
1.30      paf       335:                        }
1.41      paf       336: 
1.45      paf       337:                        // ERROR. write it out
1.62      paf       338:                        output_result(*body_string, header_only);
1.3       paf       339:                }
1.30      paf       340:                CATCH(e) {
1.41      paf       341:                        // exception in request exception handler
                    342:                        // remember to rethrow it
                    343:                        rethrow_me=e;  need_rethrow=true; 
1.1       paf       344:                }
1.30      paf       345:                END_CATCH
1.1       paf       346:        }
1.41      paf       347:        END_CATCH // do not use pool() after this point - no exception handler set
                    348:                  // any throw() would try to use zero exception() pointer 
                    349: 
                    350:        if(need_rethrow) // there were an exception set for us to rethrow?
1.62      paf       351:                THROW(rethrow_me.type(), rethrow_me.code(),
1.41      paf       352:                        rethrow_me.problem_source(),
                    353:                        rethrow_me.comment());
1.28      paf       354: 
1.1       paf       355: }
                    356: 
1.67      paf       357: VStateless_class *Request::use_file(const char *file, bool fail_on_read_problem,
1.26      paf       358:                                                                        const String *name, 
                    359:                                                                        VStateless_class *base_class) {
1.73      paf       360:        // cyclic dependence check
                    361:        String& sfile=*NEW String(pool(), file);
                    362:        if(used_files.get(sfile))
                    363:                return base_class;
                    364:        used_files.put(sfile, (Hash::Val *)true);
                    365: 
                    366: 
1.61      paf       367:        char *source=file_read_text(pool(), file, fail_on_read_problem);
1.3       paf       368:        if(!source)
1.12      paf       369:                return base_class;
1.3       paf       370: 
1.25      paf       371:        return use_buf(source, file, 0/*new class*/, name, base_class);
1.16      paf       372: }
                    373: 
1.67      paf       374: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26      paf       375:                                                                   VStateless_class *aclass, const String *name, 
                    376:                                                                   VStateless_class *base_class) {
1.5       paf       377:        // compile loaded class
1.26      paf       378:        VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1       paf       379: 
1.4       paf       380:        // locate and execute possible @auto[] static method
1.25      paf       381:        execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16      paf       382:        return &cclass;
1.20      paf       383: }
                    384: 
1.52      paf       385: /**
                    386:        - fail_if_junction(true, junction = fail
                    387:        - fail_if_junction(false, not junction = fail
                    388: */
1.62      paf       389: void Request::fail_if_junction_(bool is, Value& value, 
                    390:                                                                const String& method_name, const char *msg) {
1.20      paf       391: 
                    392:        if((value.get_junction()!=0) ^ !is)
                    393:                THROW(0, 0,
                    394:                        &method_name,
                    395:                        msg);
1.19      paf       396: }
                    397: 
                    398: char *Request::relative(const char *path, const char *file) {
1.21      paf       399:     char *result=(char *)malloc(strlen(path)+strlen(file)+1);
1.19      paf       400:        strcpy(result, path);
1.63      paf       401:     rsplit(result, '/');
                    402:     strcat(result, "/");
1.19      paf       403:     strcat(result, file);
                    404:     return result;
                    405: }
                    406: 
                    407: char *Request::absolute(const char *name) {
                    408:        if(name[0]=='/') {
1.35      paf       409:                char *result=(char *)malloc(strlen(info.document_root)+strlen(name)+1);
                    410:                strcpy(result, info.document_root);
1.19      paf       411:                strcat(result, name);
                    412:                return result;
                    413:        } else 
1.59      paf       414:                return relative(info.path_translated, name);
1.1       paf       415: }
1.45      paf       416: 
1.62      paf       417: void Request::output_result(const String& body_string, bool header_only) {
1.51      paf       418:        // header: cookies
                    419:        cookie.output_result();
                    420:        
1.49      paf       421:        // set default content-type
1.74      paf       422:        response.fields().put_dont_replace(*content_type_name, default_content_type);
1.49      paf       423: 
1.62      paf       424:        // prepare header: $response:fields without :body
1.73      paf       425:        response.fields().for_each(add_header_attribute, /*excluding*/ body_name);
1.50      paf       426: 
1.62      paf       427:        // prepare...
1.45      paf       428:        const char *body=body_string.cstr();
1.50      paf       429:        size_t content_length=strlen(body);
                    430: 
1.62      paf       431:        // prepare header: content-length
1.65      paf       432:        if(content_length) { // useful for redirecting [header "location: http://..."]
                    433:                char content_length_cstr[MAX_NUMBER];
1.66      paf       434:                snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.69      paf       435:                SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.65      paf       436:        }
1.62      paf       437: 
                    438:        // send header
1.69      paf       439:        SAPI::send_header(pool());
1.71      paf       440:        
1.62      paf       441:        // send body
                    442:        if(!header_only)
1.69      paf       443:                SAPI::send_body(pool(), body, content_length);
1.50      paf       444: }

E-mail: