|
|
| version 1.53, 2001/03/19 16:06:14 | version 1.66, 2001/03/22 21:33:33 |
|---|---|
| Line 1 | Line 1 |
| /* | /** @file |
| Parser | Parser: request class main part. @see compile.C and execute.C. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) |
| $Id$ | $Id$ |
| Line 13 | Line 15 |
| #include "pa_wwrapper.h" | #include "pa_wwrapper.h" |
| #include "pa_vclass.h" | #include "pa_vclass.h" |
| #include "_root.h" | #include "_root.h" |
| #include "_env.h" | |
| #include "_table.h" | #include "_table.h" |
| #include "pa_globals.h" | #include "pa_globals.h" |
| #include "pa_vint.h" | #include "pa_vint.h" |
| Line 42 Request::Request(Pool& apool, | Line 43 Request::Request(Pool& apool, |
| classes().put(*root_class_name, &ROOT); | classes().put(*root_class_name, &ROOT); |
| // table class | // table class |
| classes().put(*table_class_name, table_class); | classes().put(*table_class_name, table_class); |
| // table_class->set_name(*table_class_name); | |
| // env class | // env class |
| classes().put(*env_class_name, &env); | classes().put(*env_class_name, &env); |
| Line 56 Request::Request(Pool& apool, | Line 56 Request::Request(Pool& apool, |
| classes().put(*cookie_class_name, &cookie); | classes().put(*cookie_class_name, &cookie); |
| } | } |
| static void output_response_attribute(const Hash::Key& aattribute, Hash::Value *ameaning, | static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, |
| void *info) { | void *info) { |
| String *attribute_to_exclude=static_cast<String *>(info); | String *attribute_to_exclude=static_cast<String *>(info); |
| if(aattribute==*attribute_to_exclude) | if(aattribute==*attribute_to_exclude) |
| return; | return; |
| String attribute(aattribute.pool()); | Value& lmeaning=*static_cast<Value *>(ameaning); |
| attribute.append(aattribute, String::Untaint_lang::HEADER, true); | Pool& pool=lmeaning.pool(); |
| (*service_funcs.output_header_attribute)( | String attribute(pool); |
| attribute.cstr(), | (*service_funcs.add_header_attribute)(pool, |
| attributed_meaning_string(static_cast<Value *>(ameaning)).cstr()); | 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, | load MAIN class, execute @main. |
| const char *sys_auto_path2) { | MAIN class consists of all the auto.p files we'd manage to find |
| plus | |
| the file user requested us to process | |
| all located classes become children of one another, | |
| composing class we name 'MAIN' | |
| */ | |
| void Request::core(const char *root_auto_path, bool root_auto_fail, | |
| const char *site_auto_path, bool site_auto_fail, | |
| bool header_only) { | |
| VStateless_class *main_class=0; | VStateless_class *main_class=0; |
| bool need_rethrow=false; Exception rethrow_me; | bool need_rethrow=false; Exception rethrow_me; |
| TRY { | TRY { |
| char *auto_filespec=(char *)malloc(MAX_STRING); | char *auto_filespec=(char *)malloc(MAX_STRING); |
| // load MAIN class, | // loading root auto.p |
| // it consists of all the auto.p files we'd manage to find | if(root_auto_path) { |
| // plus | strncpy(auto_filespec, root_auto_path, MAX_STRING-strlen("/" AUTO_FILE_NAME)); |
| // the file user requested us to process | strcat(auto_filespec, "/" AUTO_FILE_NAME); |
| // all located classes become children of one another, | |
| // composing class we name 'MAIN' | |
| // loading system auto.p 1 | |
| if(sys_auto_path1) { | |
| strncpy(auto_filespec, sys_auto_path1, MAX_STRING-strlen(AUTO_FILE_NAME)); | |
| strcat(auto_filespec, AUTO_FILE_NAME); | |
| main_class=use_file( | main_class=use_file( |
| auto_filespec, false/*ignore possible read problem*/, | auto_filespec, root_auto_fail, |
| main_class_name, main_class); | main_class_name, main_class); |
| } | } |
| // loading system auto.p 2 | // loading site auto.p |
| if(sys_auto_path2) { | if(site_auto_path) { |
| strncpy(auto_filespec, sys_auto_path2, MAX_STRING-strlen(AUTO_FILE_NAME)); | strncpy(auto_filespec, site_auto_path, MAX_STRING-strlen("/" AUTO_FILE_NAME)); |
| strcat(auto_filespec, AUTO_FILE_NAME); | strcat(auto_filespec, "/" AUTO_FILE_NAME); |
| main_class=use_file( | main_class=use_file( |
| auto_filespec, false/*ignore possible read problem*/, | auto_filespec, site_auto_fail, |
| main_class_name, main_class); | main_class_name, main_class); |
| } | } |
| Line 125 void Request::core(Exception& system_exc | Line 127 void Request::core(Exception& system_exc |
| Value *defaults=main_class?main_class->get_element(*defaults_name):0; | Value *defaults=main_class?main_class->get_element(*defaults_name):0; |
| fdefault_content_type=defaults?defaults->get_element(*content_type_name):0; | fdefault_content_type=defaults?defaults->get_element(*content_type_name):0; |
| // there must be some auto.p | |
| if(!main_class) | |
| THROW(0,0, | |
| 0, | |
| "no 'auto.p' found (nither system nor any site's)"); | |
| // compiling requested file | // compiling requested file |
| main_class=use_file(info.path_translated, true/*don't ignore read problem*/, | main_class=use_file(info.path_translated, true/*don't ignore read problem*/, |
| main_class_name, main_class); | main_class_name, main_class); |
| Line 149 void Request::core(Exception& system_exc | Line 145 void Request::core(Exception& system_exc |
| body_string=&body_value->as_string();// TODO: IMAGE&FILE | body_string=&body_value->as_string();// TODO: IMAGE&FILE |
| // OK. write out the result | // OK. write out the result |
| output_result(*body_string); | output_result(*body_string, header_only); |
| } | } |
| CATCH(e) { | CATCH(e) { |
| TRY { | TRY { |
| Line 279 void Request::core(Exception& system_exc | Line 275 void Request::core(Exception& system_exc |
| } | } |
| // ERROR. write it out | // ERROR. write it out |
| output_result(*body_string); | output_result(*body_string, header_only); |
| } | } |
| CATCH(e) { | CATCH(e) { |
| // exception in request exception handler | // exception in request exception handler |
| Line 292 void Request::core(Exception& system_exc | Line 288 void Request::core(Exception& system_exc |
| // any throw() would try to use zero exception() pointer | // any throw() would try to use zero exception() pointer |
| if(need_rethrow) // there were an exception set for us to rethrow? | 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.problem_source(), |
| rethrow_me.comment()); | rethrow_me.comment()); |
| } | } |
| /// \todo find|solve cyclic dependences | /// @todo find|solve cyclic dependences |
| VStateless_class *Request::use_file( | VStateless_class *Request::use_file( |
| const char *file, bool fail_on_read_problem, | const char *file, bool fail_on_read_problem, |
| const String *name, | const String *name, |
| VStateless_class *base_class) { | VStateless_class *base_class) { |
| char *source=file_read(pool(), file, fail_on_read_problem); | char *source=file_read_text(pool(), file, fail_on_read_problem); |
| if(!source) | if(!source) |
| return base_class; | return base_class; |
| Line 326 VStateless_class *Request::use_buf( | Line 322 VStateless_class *Request::use_buf( |
| - fail_if_junction(true, junction = fail | - fail_if_junction(true, junction = fail |
| - fail_if_junction(false, not junction = fail | - fail_if_junction(false, not junction = fail |
| */ | */ |
| void Request::fail_if_junction_(bool is, | void Request::fail_if_junction_(bool is, Value& value, |
| Value& value, const String& method_name, char *msg) { | const String& method_name, const char *msg) { |
| if((value.get_junction()!=0) ^ !is) | if((value.get_junction()!=0) ^ !is) |
| THROW(0, 0, | THROW(0, 0, |
| Line 338 void Request::fail_if_junction_(bool is, | Line 334 void Request::fail_if_junction_(bool is, |
| char *Request::relative(const char *path, const char *file) { | char *Request::relative(const char *path, const char *file) { |
| char *result=(char *)malloc(strlen(path)+strlen(file)+1); | char *result=(char *)malloc(strlen(path)+strlen(file)+1); |
| strcpy(result, path); | strcpy(result, path); |
| rsplit(result, PATH_DELIMITER_CHAR); | rsplit(result, '/'); |
| strcat(result, PATH_DELIMITER_STRING); | strcat(result, "/"); |
| strcat(result, file); | strcat(result, file); |
| return result; | return result; |
| } | } |
| Line 351 char *Request::absolute(const char *name | Line 347 char *Request::absolute(const char *name |
| strcat(result, name); | strcat(result, name); |
| return result; | return result; |
| } else | } 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 | // header: cookies |
| cookie.output_result(); | cookie.output_result(); |
| Line 362 void Request::output_result(const String | Line 358 void Request::output_result(const String |
| if(fdefault_content_type) | if(fdefault_content_type) |
| response.fields().put_dont_replace(*content_type_name, fdefault_content_type); | response.fields().put_dont_replace(*content_type_name, fdefault_content_type); |
| // header: $response:fields without :body | // prepare header: $response:fields without :body |
| response.fields().foreach(output_response_attribute, /*excluding*/ body_name); | response.fields().foreach(add_header_attribute, /*excluding*/ body_name); |
| // prepare | // prepare... |
| const char *body=body_string.cstr(); | const char *body=body_string.cstr(); |
| size_t content_length=strlen(body); | size_t content_length=strlen(body); |
| // header: content-length | // prepare header: content-length |
| char content_length_cstr[MAX_NUMBER]; | if(content_length) { // useful for redirecting [header "location: http://..."] |
| snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length); | char content_length_cstr[MAX_NUMBER]; |
| (*service_funcs.output_header_attribute)("content-length", content_length_cstr); | snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length); |
| // body | (*service_funcs.add_header_attribute)(pool(), "content-length", content_length_cstr); |
| (*service_funcs.output_body)(body, content_length); | } |
| // send header | |
| (*service_funcs.send_header)(pool()); | |
| // send body | |
| if(!header_only) | |
| (*service_funcs.send_body)(pool(), body, content_length); | |
| } | } |