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

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

E-mail: