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

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

E-mail: