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

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

E-mail: