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

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

E-mail: