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

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

E-mail: