--- parser3/src/main/pa_request.C 2001/03/18 17:39:29 1.49 +++ parser3/src/main/pa_request.C 2001/03/21 14:06:46 1.62 @@ -1,19 +1,20 @@ -/* - Parser +/** @file + Parser: request class main part. @see compile.C and execute.C. + Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan (http://design.ru/paf) - $Id: pa_request.C,v 1.49 2001/03/18 17:39:29 paf Exp $ + $Id: pa_request.C,v 1.62 2001/03/21 14:06:46 paf Exp $ */ #include +#include "pa_common.h" #include "pa_request.h" #include "pa_wwrapper.h" -#include "pa_common.h" #include "pa_vclass.h" #include "_root.h" -#include "_env.h" #include "_table.h" #include "pa_globals.h" #include "pa_vint.h" @@ -29,6 +30,7 @@ Request::Request(Pool& apool, form(apool), request(apool, *this), response(apool), + cookie(apool), fclasses(apool), fdefault_lang(adefault_lang), flang(adefault_lang), info(ainfo), @@ -41,7 +43,6 @@ Request::Request(Pool& apool, classes().put(*root_class_name, &ROOT); // table class classes().put(*table_class_name, table_class); -// table_class->set_name(*table_class_name); // env class classes().put(*env_class_name, &env); @@ -51,53 +52,28 @@ Request::Request(Pool& apool, classes().put(*request_class_name, &request); // response class classes().put(*response_class_name, &response); + // cookie class + classes().put(*cookie_class_name, &cookie); } -static void append_attribute_subattribute(const Hash::Key& akey, Hash::Value *avalue, - void *info) { - if(akey==VALUE_NAME) - return; - - // ...; charset=windows1251 - String *string=static_cast(info); - string->APPEND_CONST("; "); - - string->append(akey, String::Untaint_lang::URI, true); - - string->APPEND_CONST("="); - - Value *value=static_cast(avalue); - string->append(value->as_string(), String::Untaint_lang::URI, true); -} -static void output_response_attribute(const Hash::Key& aattribute, Hash::Value *ameaning, - void *info) { +static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, + void *info) { String *attribute_to_exclude=static_cast(info); if(aattribute==*attribute_to_exclude) return; - String attribute(aattribute.pool()); - attribute.append(aattribute, String::Untaint_lang::URI, true); + Value& lmeaning=*static_cast(ameaning); + Pool& pool=lmeaning.pool(); - Value *meaning=static_cast(ameaning); - String string(meaning->pool()); - if(Hash *hash=meaning->get_hash()) { - // $value(value) $subattribute(subattribute value) - Value *value=static_cast(hash->get(*value_name)); - if(value) - string.append(value->as_string(), String::Untaint_lang::URI, true); - - hash->foreach(append_attribute_subattribute, &string); - } else // string value - string.append(meaning->as_string(), String::Untaint_lang::URI, true); - - (*service_funcs.output_header_attribute)( - attribute.cstr(), - string.cstr()); + String attribute(pool); + (*service_funcs.add_header_attribute)(pool, + attribute.append(aattribute, String::UL_HEADER, true).cstr(), + attributed_meaning_to_string(lmeaning).cstr()); } -void Request::core(Exception& system_exception, - const char *sys_auto_path1, - const char *sys_auto_path2) { +void Request::core(const char *sys_auto_path1, + const char *sys_auto_path2, + bool header_only) { VStateless_class *main_class=0; bool need_rethrow=false; Exception rethrow_me; TRY { @@ -138,6 +114,7 @@ void Request::core(Exception& system_exc int post_max_size=value?value:10*0x400*400; form.fill_fields(*this, post_max_size); + cookie.fill_fields(*this); // TODO: load site auto.p files, all assigned bases from upper dir /*char *site_auto_file="Y:\\parser3\\src\\auto.p"; @@ -150,7 +127,7 @@ void Request::core(Exception& system_exc fdefault_content_type=defaults?defaults->get_element(*content_type_name):0; // there must be some auto.p - if(!main_class) + if(/*\*/0&&!main_class) THROW(0,0, 0, "no 'auto.p' found (nither system nor any site's)"); @@ -173,7 +150,7 @@ void Request::core(Exception& system_exc body_string=&body_value->as_string();// TODO: IMAGE&FILE // OK. write out the result - output_result(*body_string); + output_result(*body_string, header_only); } CATCH(e) { TRY { @@ -303,7 +280,7 @@ void Request::core(Exception& system_exc } // ERROR. write it out - output_result(*body_string); + output_result(*body_string, header_only); } CATCH(e) { // exception in request exception handler @@ -316,18 +293,18 @@ void Request::core(Exception& system_exc // any throw() would try to use zero exception() pointer if(need_rethrow) // there were an exception set for us to rethrow? - system_exception._throw(rethrow_me.type(), rethrow_me.code(), + THROW(rethrow_me.type(), rethrow_me.code(), rethrow_me.problem_source(), rethrow_me.comment()); } +/// @todo find|solve cyclic dependences VStateless_class *Request::use_file( const char *file, bool fail_on_read_problem, const String *name, VStateless_class *base_class) { - // TODO: обнаружить|решить cyclic dependences - char *source=file_read(pool(), file, fail_on_read_problem); + char *source=file_read_text(pool(), file, fail_on_read_problem); if(!source) return base_class; @@ -346,11 +323,13 @@ VStateless_class *Request::use_buf( return &cclass; } -void Request::fail_if_junction_(bool is, - Value& value, const String& method_name, char *msg) { +/** + - fail_if_junction(true, junction = fail + - fail_if_junction(false, not junction = fail +*/ +void Request::fail_if_junction_(bool is, Value& value, + const String& method_name, const char *msg) { - // fail_if_junction(true, junction = fail - // fail_if_junction(false, not junction = fail if((value.get_junction()!=0) ^ !is) THROW(0, 0, &method_name, @@ -373,23 +352,33 @@ char *Request::absolute(const char *name strcat(result, name); return result; } else - return relative(info.uri, name); + return relative(info.path_translated, name); } -void Request::output_result(const String& body_string) { +void Request::output_result(const String& body_string, bool header_only) { + // header: cookies + cookie.output_result(); + // set default content-type - response.fields().put_dont_replace(*content_type_name, fdefault_content_type); + if(fdefault_content_type) + response.fields().put_dont_replace(*content_type_name, fdefault_content_type); - // header: response fields - response.fields().foreach(output_response_attribute, /*excluding*/ body_name); + // prepare header: $response:fields without :body + response.fields().foreach(add_header_attribute, /*excluding*/ body_name); + + // prepare... const char *body=body_string.cstr(); - if(size_t content_length=strlen(body)) { - // header: content-length - char content_length_cstr[MAX_NUMBER]; - snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length); - (*service_funcs.output_header_attribute)("content-length", - content_length_cstr); - // body - (*service_funcs.output_body)(body, content_length); - } -} \ No newline at end of file + size_t content_length=strlen(body); + + // prepare header: content-length + char content_length_cstr[MAX_NUMBER]; + snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length); + (*service_funcs.add_header_attribute)(pool(), "content-length", content_length_cstr); + + // send header + (*service_funcs.send_header)(pool()); + + // send body + if(!header_only) + (*service_funcs.send_body)(pool(), body, content_length); +}