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

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

E-mail: