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

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

E-mail: