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

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

E-mail: