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

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.84    ! paf         8:        $Id: pa_request.C,v 1.83 2001/03/27 15:37:52 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;
1.84    ! paf       121:                        size_t value=element?(size_t)element->as_double():0;
1.78      paf       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.83      paf       147:                        size_t document_root_size=strlen(info.document_root);
                    148:                        if(info.document_root[document_root_size-1]=='/')
                    149:                                document_root_size--;
                    150:                        memcpy(file_spec, info.document_root, document_root_size);
1.72      paf       151: 
                    152:                        /* /requested/file.html */
                    153:                        char *branches=(char *)malloc(strlen(info.path_translated)+1);
1.83      paf       154:                        strcpy(branches, info.path_translated+document_root_size);
1.72      paf       155:                        char *next=branches;
1.83      paf       156:                        char *append_here=file_spec+document_root_size;
1.72      paf       157: 
                    158:                        size_t slash_auto_p_size=strlen("/" AUTO_FILE_NAME);
                    159:                        while(true) {
                    160:                                char *step=lsplit(&next, '/');
                    161:                                if(next) { // not 'file.html' part
                    162:                                        size_t step_size=strlen(step);
                    163:                                        memcpy(append_here, step, step_size);
                    164:                                        append_here+=step_size;
                    165:                                        memcpy(append_here, "/" AUTO_FILE_NAME, slash_auto_p_size+1);
                    166:                                        append_here++/* / */;
                    167: 
1.77      paf       168:                                        String& sfile_spec=*NEW String(pool());
                    169:                                        sfile_spec.APPEND(file_spec, 0, "scanned", 0);
                    170:                                        main_class=use_file(sfile_spec, false/*ignore read problem*/,
1.72      paf       171:                                                main_class_name, main_class);
                    172:                                } else
                    173:                                        break;
                    174:                        }
                    175:                }
1.34      paf       176: 
1.72      paf       177:                // compiling requested file
1.77      paf       178:                String& spath_translated=*NEW String(pool());
                    179:                spath_translated.APPEND(info.path_translated, 0, "user-request", 0);
                    180:                main_class=use_file(spath_translated, true/*don't ignore read problem*/,
1.72      paf       181:                        main_class_name, main_class);
1.7       paf       182: 
1.42      paf       183:                // $MAIN:defaults
1.78      paf       184:                Value *defaults=main_class->get_element(*defaults_name);
1.75      paf       185:                // value must be allocated on request's pool for that pool used on
                    186:                // meaning constructing @see attributed_meaning_to_string
1.80      paf       187:                default_content_type=defaults?defaults->get_element(*content_type_name):0;
1.78      paf       188:                if(Value *element=main_class->get_element(*html_typo_name))
1.84    ! paf       189:                        if(Table *table=element->get_table())
        !           190:                                pool().set_tag(table);
1.25      paf       191: 
                    192:                // execute @main[]
1.41      paf       193:                const String *body_string=execute_method(*main_class, *main_method_name);
                    194:                if(!body_string)
1.25      paf       195:                        THROW(0,0,
                    196:                        0, 
                    197:                        "'"MAIN_METHOD_NAME"' method not found");
1.79      paf       198: 
                    199:                // post-process
                    200:                // todo
1.41      paf       201: 
1.45      paf       202:                // extract response body
                    203:                Value *body_value=static_cast<Value *>(
                    204:                        response.fields().get(*body_name));
                    205:                if(body_value) // there is some $request.body
                    206:                        body_string=&body_value->as_string();// TODO: IMAGE&FILE
                    207: 
                    208:                // OK. write out the result
1.62      paf       209:                output_result(*body_string, header_only);
1.3       paf       210:        } 
1.76      paf       211:        CATCH(e) { // request handling problem
                    212:                // we're returning not result, but error explanation
1.30      paf       213:                TRY {
1.76      paf       214:                        // log the beast
                    215:                        const String *problem_source=e.problem_source();
                    216:                        if(problem_source)
                    217:                                SAPI::log(pool(),
                    218: #ifndef NO_STRING_ORIGIN
                    219:                                        "%s(%d): "
                    220: #endif
                    221:                                        "'%s' %s [%s %s]",
                    222: #ifndef NO_STRING_ORIGIN
                    223:                                        problem_source->origin().file?problem_source->origin().file:"global",
                    224:                                        problem_source->origin().line,
                    225: #endif
                    226:                                        problem_source->cstr(),
                    227:                                        e.comment(),
                    228:                                        e.type()?e.type()->cstr():"-",
                    229:                                        e.code()?e.code()->cstr():"-"
                    230:                                );
                    231:                        else
                    232:                                SAPI::log(pool(),
                    233:                                        "%s [%s %s]",
                    234:                                        e.comment(),
                    235:                                        e.type()?e.type()->cstr():"-",
                    236:                                        e.code()?e.code()->cstr():"-"
                    237:                                        );
1.43      paf       238: 
                    239:                        // reset language to default
                    240:                        flang=fdefault_lang;
                    241:                        
                    242:                        // reset response
                    243:                        response.fields().clear();
                    244: 
                    245:                        // this is what we'd return in $response:body
1.41      paf       246:                        const String *body_string=0;
1.43      paf       247: 
1.30      paf       248:                        if(main_class) { // we've managed to end up with some main_class
                    249:                                // maybe we'd be lucky enough as to report an error
                    250:                                // in a gracefull way...
                    251:                                if(Value *value=main_class->get_element(*exception_method_name))
                    252:                                        if(Junction *junction=value->get_junction())
                    253:                                                if(const Method *method=junction->method) {
                    254:                                                        // preparing to pass parameters to 
                    255:                                                        //      @exception[origin;source;comment;type;code]
1.38      paf       256:                                                        VMethodFrame frame(pool(), *junction);
                    257:                                                        frame.set_self(*main_class);
1.30      paf       258: 
                    259:                                                        const String *problem_source=e.problem_source();
                    260:                                                        // origin
1.44      paf       261:                                                        String origin_name(pool(), "origin");
1.38      paf       262:                                                        Value *origin_value=0;
1.30      paf       263: #ifndef NO_STRING_ORIGIN
1.38      paf       264:                                                        if(problem_source) {
                    265:                                                                const Origin& origin=problem_source->origin();
                    266:                                                                if(origin.file) {
                    267:                                                                        char *buf=(char *)malloc(MAX_STRING);
1.42      paf       268:                                                                        snprintf(buf, MAX_STRING, "%s(%d):", 
1.38      paf       269:                                                                                origin.file, 1+origin.line);
1.44      paf       270:                                                                        String *origin_file_line=NEW String(pool(),
                    271:                                                                                buf, true);
1.38      paf       272:                                                                        origin_value=NEW VString(*origin_file_line);
                    273:                                                                }
1.30      paf       274:                                                        }
                    275: #endif
1.38      paf       276:                                                        frame.store_param(origin_name, 
                    277:                                                                origin_value?origin_value:NEW VUnknown(pool()));
1.28      paf       278: 
1.30      paf       279:                                                        // source
1.44      paf       280:                                                        String source_name(pool(), "source");
1.38      paf       281:                                                        Value *source_value=0;
1.43      paf       282:                                                        if(problem_source) {
1.47      paf       283:                                                                String& problem_source_copy=*NEW String(pool());
                    284:                                                                problem_source_copy.append(*problem_source, 
                    285:                                                                        flang, true);
1.43      paf       286:                                                                source_value=NEW VString(problem_source_copy);
                    287:                                                        }
1.38      paf       288:                                                        frame.store_param(source_name, 
                    289:                                                                source_value?source_value:NEW VUnknown(pool()));
1.30      paf       290: 
                    291:                                                        // comment
1.44      paf       292:                                                        String comment_name(pool(), "comment");
                    293:                                                        String *comment_value=NEW String(pool(),
                    294:                                                                e.comment(), true);
1.38      paf       295:                                                        frame.store_param(comment_name, 
1.30      paf       296:                                                                NEW VString(*comment_value));
                    297: 
                    298:                                                        // type
1.44      paf       299:                                                        String type_name(pool(), "type");
1.30      paf       300:                                                        Value *type_value;
1.43      paf       301:                                                        if(e.type()) {
1.47      paf       302:                                                                String& type_copy=*NEW String(pool());
                    303:                                                                type_value=NEW VString(type_copy.append(*e.type(), 
                    304:                                                                        flang, true));
1.43      paf       305:                                                        } else
1.30      paf       306:                                                                type_value=NEW VUnknown(pool());
1.38      paf       307:                                                        frame.store_param(type_name, type_value);
1.30      paf       308: 
                    309:                                                        // code
1.44      paf       310:                                                        String code_name(pool(), "code");
1.30      paf       311:                                                        Value *code_value;
1.43      paf       312:                                                        if(e.code()) {
1.47      paf       313:                                                                String& code_copy=*NEW String(pool());
                    314:                                                                code_value=NEW VString(code_copy.append(*e.code(), 
                    315:                                                                        flang, true));
1.43      paf       316:                                                        } else
1.30      paf       317:                                                                code_value=NEW VUnknown(pool());
1.38      paf       318:                                                        frame.store_param(code_name, code_value);
1.30      paf       319: 
1.43      paf       320:                                                        // future $response:body=
                    321:                                                        //   execute ^exception[origin;source;comment;type;code]
                    322:                                                        body_string=execute_method(frame, *method);
1.30      paf       323:                                                }
                    324:                        }
                    325:                        
1.41      paf       326:                        if(!body_string) {  // couldn't report an error beautifully?
                    327:                                // doing that ugly
                    328: 
                    329:                                // make up result: $origin $source $comment $type $code
                    330:                                char *buf=(char *)malloc(MAX_STRING);
1.30      paf       331:                                size_t printed=0;
                    332:                                const String *problem_source=e.problem_source();
                    333:                                if(problem_source) {
1.16      paf       334: #ifndef NO_STRING_ORIGIN
1.30      paf       335:                                        const Origin& origin=problem_source->origin();
                    336:                                        if(origin.file)
1.41      paf       337:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ", 
1.30      paf       338:                                                origin.file, 1+origin.line);
1.28      paf       339: #endif
1.41      paf       340:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ", 
1.30      paf       341:                                                problem_source->cstr());
                    342:                                }
1.41      paf       343:                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", 
1.30      paf       344:                                        e.comment());
                    345:                                const String *type=e.type();
                    346:                                if(type) {
1.41      paf       347:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "  type: %s", 
1.30      paf       348:                                                type->cstr());
                    349:                                        const String *code=e.code();
                    350:                                        if(code)
1.41      paf       351:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s", 
1.30      paf       352:                                                code->cstr());
                    353:                                }
1.43      paf       354: 
                    355:                                // future $response:content-type
1.49      paf       356:                                response.fields().put(*content_type_name, 
1.82      paf       357:                                        NEW VString(*NEW String(pool(), UNHANDLED_EXCEPTION_CONTENT_TYPE)));
1.43      paf       358:                                // future $response:body
1.44      paf       359:                                body_string=NEW String(pool(), buf);
1.30      paf       360:                        }
1.41      paf       361: 
1.45      paf       362:                        // ERROR. write it out
1.62      paf       363:                        output_result(*body_string, header_only);
1.3       paf       364:                }
1.30      paf       365:                CATCH(e) {
1.41      paf       366:                        // exception in request exception handler
                    367:                        // remember to rethrow it
                    368:                        rethrow_me=e;  need_rethrow=true; 
1.1       paf       369:                }
1.30      paf       370:                END_CATCH
1.1       paf       371:        }
1.41      paf       372:        END_CATCH // do not use pool() after this point - no exception handler set
                    373:                  // any throw() would try to use zero exception() pointer 
                    374: 
                    375:        if(need_rethrow) // there were an exception set for us to rethrow?
1.62      paf       376:                THROW(rethrow_me.type(), rethrow_me.code(),
1.41      paf       377:                        rethrow_me.problem_source(),
                    378:                        rethrow_me.comment());
1.28      paf       379: 
1.1       paf       380: }
                    381: 
1.77      paf       382: VStateless_class *Request::use_file(const String& file_spec, bool fail_on_read_problem,
1.26      paf       383:                                                                        const String *name, 
                    384:                                                                        VStateless_class *base_class) {
1.73      paf       385:        // cyclic dependence check
1.77      paf       386:        if(used_files.get(file_spec))
1.73      paf       387:                return base_class;
1.77      paf       388:        used_files.put(file_spec, (Hash::Val *)true);
1.73      paf       389: 
                    390: 
1.77      paf       391:        char *source=file_read_text(pool(), file_spec, fail_on_read_problem);
1.3       paf       392:        if(!source)
1.12      paf       393:                return base_class;
1.3       paf       394: 
1.77      paf       395:        return use_buf(source, file_spec.cstr(), 0/*new class*/, name, base_class);
1.16      paf       396: }
                    397: 
1.67      paf       398: VStateless_class *Request::use_buf(const char *source, const char *file,
1.26      paf       399:                                                                   VStateless_class *aclass, const String *name, 
                    400:                                                                   VStateless_class *base_class) {
1.5       paf       401:        // compile loaded class
1.26      paf       402:        VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1       paf       403: 
1.4       paf       404:        // locate and execute possible @auto[] static method
1.25      paf       405:        execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16      paf       406:        return &cclass;
1.20      paf       407: }
                    408: 
1.52      paf       409: /**
                    410:        - fail_if_junction(true, junction = fail
                    411:        - fail_if_junction(false, not junction = fail
                    412: */
1.62      paf       413: void Request::fail_if_junction_(bool is, Value& value, 
                    414:                                                                const String& method_name, const char *msg) {
1.20      paf       415: 
                    416:        if((value.get_junction()!=0) ^ !is)
                    417:                THROW(0, 0,
                    418:                        &method_name,
                    419:                        msg);
1.19      paf       420: }
                    421: 
1.77      paf       422: const String& Request::relative(const char *apath, const String& relative_name) {
                    423:        int lpath_buf_size=strlen(apath)+1;
                    424:     char *lpath=(char *)malloc(lpath_buf_size);
                    425:        memcpy(lpath, apath, lpath_buf_size);
                    426:     rsplit(lpath, '/');
                    427:        String& result=*NEW String(pool(), lpath);
                    428:     result.APPEND_CONST("/");
                    429:        result.append(relative_name, String::UL_PASS_APPENDED);
1.19      paf       430:     return result;
                    431: }
                    432: 
1.77      paf       433: const String& Request::absolute(const String& relative_name) {
                    434:        char *relative_name_cstr=relative_name.cstr();
                    435:        if(relative_name_cstr[0]=='/') {
                    436:                String& result=*NEW String(pool(), info.document_root);
                    437:                result.append(relative_name, String::UL_PASS_APPENDED);
1.19      paf       438:                return result;
                    439:        } else 
1.77      paf       440:                return relative(info.path_translated, relative_name);
1.1       paf       441: }
1.45      paf       442: 
1.62      paf       443: void Request::output_result(const String& body_string, bool header_only) {
1.51      paf       444:        // header: cookies
                    445:        cookie.output_result();
                    446:        
1.49      paf       447:        // set default content-type
1.80      paf       448:        response.fields().put_dont_replace(*content_type_name, 
                    449:                default_content_type?default_content_type
1.82      paf       450:                :NEW VString(*NEW String(pool(), DEFAULT_CONTENT_TYPE)));
1.49      paf       451: 
1.62      paf       452:        // prepare header: $response:fields without :body
1.73      paf       453:        response.fields().for_each(add_header_attribute, /*excluding*/ body_name);
1.50      paf       454: 
1.62      paf       455:        // prepare...
1.45      paf       456:        const char *body=body_string.cstr();
1.50      paf       457:        size_t content_length=strlen(body);
                    458: 
1.62      paf       459:        // prepare header: content-length
1.65      paf       460:        if(content_length) { // useful for redirecting [header "location: http://..."]
                    461:                char content_length_cstr[MAX_NUMBER];
1.66      paf       462:                snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.69      paf       463:                SAPI::add_header_attribute(pool(), "content-length", content_length_cstr);
1.65      paf       464:        }
1.62      paf       465: 
                    466:        // send header
1.69      paf       467:        SAPI::send_header(pool());
1.71      paf       468:        
1.62      paf       469:        // send body
                    470:        if(!header_only)
1.69      paf       471:                SAPI::send_body(pool(), body, content_length);
1.50      paf       472: }

E-mail: