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

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.44    ! paf         6:        $Id: pa_request.C,v 1.43 2001/03/18 13:22:07 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: 
1.30      paf       148:                        if(main_class) { // we've managed to end up with some main_class
                    149:                                // maybe we'd be lucky enough as to report an error
                    150:                                // in a gracefull way...
                    151:                                if(Value *value=main_class->get_element(*exception_method_name))
                    152:                                        if(Junction *junction=value->get_junction())
                    153:                                                if(const Method *method=junction->method) {
                    154:                                                        // preparing to pass parameters to 
                    155:                                                        //      @exception[origin;source;comment;type;code]
1.38      paf       156:                                                        VMethodFrame frame(pool(), *junction);
                    157:                                                        frame.set_self(*main_class);
1.30      paf       158: 
                    159:                                                        const String *problem_source=e.problem_source();
                    160:                                                        // origin
1.44    ! paf       161:                                                        String origin_name(pool(), "origin");
1.38      paf       162:                                                        Value *origin_value=0;
1.30      paf       163: #ifndef NO_STRING_ORIGIN
1.38      paf       164:                                                        if(problem_source) {
                    165:                                                                const Origin& origin=problem_source->origin();
                    166:                                                                if(origin.file) {
                    167:                                                                        char *buf=(char *)malloc(MAX_STRING);
1.42      paf       168:                                                                        snprintf(buf, MAX_STRING, "%s(%d):", 
1.38      paf       169:                                                                                origin.file, 1+origin.line);
1.44    ! paf       170:                                                                        String *origin_file_line=NEW String(pool(),
        !           171:                                                                                buf, true);
1.38      paf       172:                                                                        origin_value=NEW VString(*origin_file_line);
                    173:                                                                }
1.30      paf       174:                                                        }
                    175: #endif
1.38      paf       176:                                                        frame.store_param(origin_name, 
                    177:                                                                origin_value?origin_value:NEW VUnknown(pool()));
1.28      paf       178: 
1.30      paf       179:                                                        // source
1.44    ! paf       180:                                                        String source_name(pool(), "source");
1.38      paf       181:                                                        Value *source_value=0;
1.43      paf       182:                                                        if(problem_source) {
                    183:                                                                String& problem_source_copy=*NEW String(*problem_source);
                    184:                                                                problem_source_copy.change_lang(flang);
                    185:                                                                source_value=NEW VString(problem_source_copy);
                    186:                                                        }
1.38      paf       187:                                                        frame.store_param(source_name, 
                    188:                                                                source_value?source_value:NEW VUnknown(pool()));
1.30      paf       189: 
                    190:                                                        // comment
1.44    ! paf       191:                                                        String comment_name(pool(), "comment");
        !           192:                                                        String *comment_value=NEW String(pool(),
        !           193:                                                                e.comment(), true);
1.38      paf       194:                                                        frame.store_param(comment_name, 
1.30      paf       195:                                                                NEW VString(*comment_value));
                    196: 
                    197:                                                        // type
1.44    ! paf       198:                                                        String type_name(pool(), "type");
1.30      paf       199:                                                        Value *type_value;
1.43      paf       200:                                                        if(e.type()) {
                    201:                                                                String& type_copy=*NEW String(*e.type());
                    202:                                                                type_copy.change_lang(flang);
                    203:                                                                type_value=NEW VString(type_copy);
                    204:                                                        } else
1.30      paf       205:                                                                type_value=NEW VUnknown(pool());
1.38      paf       206:                                                        frame.store_param(type_name, type_value);
1.30      paf       207: 
                    208:                                                        // code
1.44    ! paf       209:                                                        String code_name(pool(), "code");
1.30      paf       210:                                                        Value *code_value;
1.43      paf       211:                                                        if(e.code()) {
                    212:                                                                String& code_copy=*NEW String(*e.code());
                    213:                                                                code_copy.change_lang(flang);
                    214:                                                                code_value=NEW VString(code_copy);
                    215:                                                        } else
1.30      paf       216:                                                                code_value=NEW VUnknown(pool());
1.38      paf       217:                                                        frame.store_param(code_name, code_value);
1.30      paf       218: 
1.43      paf       219:                                                        // future $response:content-type=
                    220:                                                        //   content-type from any auto.p
                    221:                                                        content_type=fdefault_content_type;
                    222:                                                        // future $response:body=
                    223:                                                        //   execute ^exception[origin;source;comment;type;code]
                    224:                                                        body_string=execute_method(frame, *method);
1.30      paf       225:                                                }
                    226:                        }
                    227:                        
1.41      paf       228:                        if(!body_string) {  // couldn't report an error beautifully?
                    229:                                // doing that ugly
                    230: 
                    231:                                // make up result: $origin $source $comment $type $code
                    232:                                char *buf=(char *)malloc(MAX_STRING);
1.30      paf       233:                                size_t printed=0;
                    234:                                const String *problem_source=e.problem_source();
                    235:                                if(problem_source) {
1.16      paf       236: #ifndef NO_STRING_ORIGIN
1.30      paf       237:                                        const Origin& origin=problem_source->origin();
                    238:                                        if(origin.file)
1.41      paf       239:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s(%d): ", 
1.30      paf       240:                                                origin.file, 1+origin.line);
1.28      paf       241: #endif
1.41      paf       242:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "'%s' ", 
1.30      paf       243:                                                problem_source->cstr());
                    244:                                }
1.41      paf       245:                                printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", 
1.30      paf       246:                                        e.comment());
                    247:                                const String *type=e.type();
                    248:                                if(type) {
1.41      paf       249:                                        printed+=snprintf(buf+printed, MAX_STRING-printed, "  type: %s", 
1.30      paf       250:                                                type->cstr());
                    251:                                        const String *code=e.code();
                    252:                                        if(code)
1.41      paf       253:                                                printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s", 
1.30      paf       254:                                                code->cstr());
                    255:                                }
1.43      paf       256: 
                    257:                                // future $response:content-type
1.44    ! paf       258:                                String &content_type_value=*NEW String(pool(), "text/plain");
1.43      paf       259:                                content_type=NEW VString(content_type_value);
                    260:                                // future $response:body
1.44    ! paf       261:                                body_string=NEW String(pool(), buf);
1.30      paf       262:                        }
1.41      paf       263: 
1.43      paf       264:                        // store $response:content-type
                    265:                        response.fields().put(*content_type_name, content_type);
                    266:                        // store $response:body
1.41      paf       267:                        response.fields().put(*body_name, NEW VString(*body_string));
1.3       paf       268:                }
1.30      paf       269:                CATCH(e) {
1.41      paf       270:                        // exception in request exception handler
                    271:                        // remember to rethrow it
                    272:                        rethrow_me=e;  need_rethrow=true; 
1.1       paf       273:                }
1.30      paf       274:                END_CATCH
1.1       paf       275:        }
1.41      paf       276:        END_CATCH // do not use pool() after this point - no exception handler set
                    277:                  // any throw() would try to use zero exception() pointer 
                    278: 
                    279:        if(need_rethrow) // there were an exception set for us to rethrow?
                    280:                system_exception._throw(rethrow_me.type(), rethrow_me.code(),
                    281:                        rethrow_me.problem_source(),
                    282:                        rethrow_me.comment());
1.28      paf       283: 
1.1       paf       284: }
                    285: 
1.26      paf       286: VStateless_class *Request::use_file(
                    287:                                                                        const char *file, bool fail_on_read_problem,
                    288:                                                                        const String *name, 
                    289:                                                                        VStateless_class *base_class) {
1.3       paf       290:        // TODO: обнаружить|решить cyclic dependences
1.5       paf       291:        char *source=file_read(pool(), file, fail_on_read_problem);
1.3       paf       292:        if(!source)
1.12      paf       293:                return base_class;
1.3       paf       294: 
1.25      paf       295:        return use_buf(source, file, 0/*new class*/, name, base_class);
1.16      paf       296: }
                    297: 
1.26      paf       298: VStateless_class *Request::use_buf(
                    299:                                                                   const char *source, const char *file,
                    300:                                                                   VStateless_class *aclass, const String *name, 
                    301:                                                                   VStateless_class *base_class) {
1.5       paf       302:        // compile loaded class
1.26      paf       303:        VStateless_class& cclass=COMPILE(source, aclass, name, base_class, file);
1.1       paf       304: 
1.4       paf       305:        // locate and execute possible @auto[] static method
1.25      paf       306:        execute_method(cclass, *auto_method_name, false /*no result needed*/);
1.16      paf       307:        return &cclass;
1.20      paf       308: }
                    309: 
                    310: void Request::fail_if_junction_(bool is, 
1.26      paf       311:                                                                Value& value, const String& method_name, char *msg) {
1.20      paf       312: 
                    313:        // fail_if_junction(true, junction = fail
                    314:        // fail_if_junction(false, not junction = fail
                    315:        if((value.get_junction()!=0) ^ !is)
                    316:                THROW(0, 0,
                    317:                        &method_name,
                    318:                        msg);
1.19      paf       319: }
                    320: 
                    321: char *Request::relative(const char *path, const char *file) {
1.21      paf       322:     char *result=(char *)malloc(strlen(path)+strlen(file)+1);
1.19      paf       323:        strcpy(result, path);
1.32      paf       324:     rsplit(result, PATH_DELIMITER_CHAR);
                    325:     strcat(result, PATH_DELIMITER_STRING);
1.19      paf       326:     strcat(result, file);
                    327:     return result;
                    328: }
                    329: 
                    330: char *Request::absolute(const char *name) {
                    331:        if(name[0]=='/') {
1.35      paf       332:                char *result=(char *)malloc(strlen(info.document_root)+strlen(name)+1);
                    333:                strcpy(result, info.document_root);
1.19      paf       334:                strcat(result, name);
                    335:                return result;
                    336:        } else 
1.40      paf       337:                return relative(info.uri, name);
1.1       paf       338: }

E-mail: