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

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

E-mail: