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

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

E-mail: