--- parser3/src/main/pa_common.C 2003/04/15 06:44:34 1.140.2.3 +++ parser3/src/main/pa_common.C 2003/03/21 07:08:28 1.147 @@ -1,11 +1,11 @@ /** @file Parser: commonly functions. - Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_COMMON_C="$Date: 2003/04/15 06:44:34 $"; +static const char* IDENT_COMMON_C="$Date: 2003/03/21 07:08:28 $"; #include "pa_common.h" #include "pa_exception.h" @@ -13,7 +13,6 @@ static const char* IDENT_COMMON_C="$Date #include "pa_hash.h" #include "pa_vstring.h" #include "pa_vdate.h" -#include "pa_request.h" #ifdef WIN32 # include @@ -118,10 +117,9 @@ void fix_line_breaks(char* buf, size_t& char* file_read_text(Pool& pool, const String& file_spec, bool fail_on_read_problem, - Hash* options, Request* r, - Hash** out_fields) { + Hash *params, Hash** out_fields) { void *result; size_t size; - return file_read(pool, file_spec, result, size, true, options, r, out_fields, fail_on_read_problem)?(char *)result:0; + return file_read(pool, file_spec, result, size, true, params, out_fields, fail_on_read_problem)?(char *)result:0; } //http request stuff @@ -141,8 +139,8 @@ static bool set_addr(struct sockaddr_in return true; } -static void http_read_response(String& response, int sock){ - bool check_status=true; +static int http_read_response(String& response, int sock, bool fail_on_status_ne_200){ + const String* status_code=0; ssize_t EOLat=0; while(true) { char *buf=(char *)response.pool().malloc(MAX_STRING); @@ -150,21 +148,21 @@ static void http_read_response(String& r if(size<=0) break; response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0); - if(check_status && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response - check_status=false; - + if(!status_code && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response const String& status_line=response.mid(0, (size_t)EOLat); Array astatus(response.pool()); size_t pos_after_ref=0; status_line.split(astatus, &pos_after_ref, " ", 1); - const String* status_code=astatus.get_string(1); + status_code=astatus.get_string(1); - if(!status_code || *status_code!="200") + if(fail_on_status_ne_200 && *status_code!="200") throw Exception("http.status", status_code, "invalid HTTP response status"); } - } - if(check_status) + } + if(status_code) + return status_code->as_int(); + else throw Exception("http.response", 0, "bad response from host - no status found (size=%lu)", response.size()); @@ -183,18 +181,19 @@ static void timeout_handler(int sig){ } #endif -static void http_request(String& response, +static int http_request(String& response, const String *origin_string, const char* host, int port, const char* request, - int timeout) { + int timeout, + bool fail_on_status_ne_200){ if(!host) throw Exception("http.host", origin_string, "zero hostname"); //never #ifdef PA_USE_ALARM - signal(SIGALRM, timeout_handler); + signal(SIGALRM, timeout_handler); #endif int sock=-1; #ifdef PA_USE_ALARM @@ -207,43 +206,47 @@ static void http_request(String& respons throw Exception("http.timeout", origin_string, "timeout occured while retrieving document"); - } else + return 0; // never + } else { alarm(timeout); #endif - try { - struct sockaddr_in dest; - - if(!set_addr(&dest, host, port)) - throw Exception("http.host", - origin_string, - "can not resolve hostname \"%s\"", host); - - if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) - throw Exception("http.connect", - origin_string, - "can not make socket: %s (%d)", strerror(errno), errno); - if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) - throw Exception("http.connect", - origin_string, - "can not connect to host \"%s\": %s (%d)", host, strerror(errno), errno); - size_t request_size=strlen(request); - if(send(sock, request, request_size, 0)!=(ssize_t)request_size) - throw Exception("http.connect", - origin_string, - "error sending request: %s (%d)", strerror(errno), errno); + try { + int result; + struct sockaddr_in dest; + + if(!set_addr(&dest, host, port)) + throw Exception("http.host", + origin_string, + "can not resolve hostname \"%s\"", host); + + if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) + throw Exception("http.connect", + origin_string, + "can not make socket: %s (%d)", strerror(errno), errno); + if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) + throw Exception("http.connect", + origin_string, + "can not connect to host \"%s\": %s (%d)", host, strerror(errno), errno); + size_t request_size=strlen(request); + if(send(sock, request, request_size, 0)!=(ssize_t)request_size) + throw Exception("http.connect", + origin_string, + "error sending request: %s (%d)", strerror(errno), errno); - http_read_response(response, sock); - closesocket(sock); + result=http_read_response(response, sock, fail_on_status_ne_200); + closesocket(sock); #ifdef PA_USE_ALARM - alarm(0); + alarm(0); #endif - } catch(...) { - if(sock>=0) - closesocket(sock); + return result; + } catch(...) { #ifdef PA_USE_ALARM - alarm(0); + alarm(0); #endif - /*re*/throw; + if(sock>=0) + closesocket(sock); + /*re*/throw; + } } } @@ -273,9 +276,10 @@ static void file_read_http(Pool& pool, c int port; const char* method="GET"; int timeout=2; + bool fail_on_status_ne_200=true; Value *vheaders=0; - String connect_string(pool); + String& connect_string=*new(pool) String(pool); // must not be local [exception may be reported outside] // not in ^sql{... UL_SQL ...} spirit, but closer to ^file::load one connect_string.append(file_spec, String::UL_URI); // tainted pieces -> URI pieces @@ -306,6 +310,11 @@ static void file_read_http(Pool& pool, c if(vheaders=static_cast(options->get(*http_headers_name))) { valid_options++; } + if(Value *vany_status=static_cast(options->get(*http_any_status_name))) { + valid_options++; + fail_on_status_ne_200=!vany_status->as_bool(); + } + if(valid_options!=options->size()) throw Exception("parser.runtime", 0, @@ -332,8 +341,9 @@ static void file_read_http(Pool& pool, c //sending request String response(pool); - http_request(response, - &connect_string, host, port, request.cstr(String::UL_UNSPECIFIED), timeout); + int status_code=http_request(response, + &connect_string, host, port, request.cstr(String::UL_UNSPECIFIED), + timeout, fail_on_status_ne_200); //processing results int pos=response.pos("\r\n\r\n", 4); @@ -350,13 +360,6 @@ static void file_read_http(Pool& pool, c size_t pos_after_ref=0; header_block.split(aheaders, &pos_after_ref, "\r\n", 2); - //processing status code - const String *status_line=aheaders.get_string(0); - if(!status_line){ - throw Exception("http.response", - &connect_string, - "bad response from host - no status line "); - } //processing headers for(int i=1;i(context); + if(size_t to_read_size=(size_t)finfo.st_size) { + *info.data=pool.malloc(to_read_size+(as_text?1:0), 3); + *info.data_size=(size_t)read(f, *info.data, to_read_size); - int offset=0; - int limit=-1; - if(info.options) { - int valid_options=0; - if(Value *voffset=(Value *)info.options->get(*sql_offset_name)) { - valid_options++; - offset=info.r->process_to_value(*voffset).as_int(); - } - if(Value *vlimit=(Value *)info.options->get(*sql_limit_name)) { - valid_options++; - limit=info.r->process_to_value(*vlimit).as_int(); - } - if(valid_options!=info.options->size()) - throw Exception("parser.runtime", - &file_spec, - "load called with invalid option"); - } - - int m=finfo.st_size; - if(offset<0 || offset>=m || limit==0) { // bad options/empty file: returning empty file + if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size) + throw Exception(0, + &file_spec, + "read failed: actually read %lu bytes count not in [0..%lu] valid range", + *info.data_size, to_read_size); + } else { // empty file if(as_text) { *info.data=pool.malloc(1); *(char*)(*info.data)=0; @@ -422,38 +415,18 @@ static void file_read_action(Pool& pool, *info.data_size=0; return; } - if(limit<0 || limit>m) - limit=m; - - if(offset) - if(lseek(f, offset, SEEK_SET)<0) - throw Exception("file.read", - &file_spec, - "seek to %d position failed: %s (%d), actual filename '%s'", - offset, strerror(errno), errno, fname); - - *info.data=pool.malloc(limit+(as_text?1:0), 3); - *info.data_size=(size_t)read(f, *info.data, limit); - - if((ssize_t)*info.data_size<0) - throw Exception(0, - &file_spec, - "failed to read %d bytes from %d offset of %d size (stat)", // may be in text mode... - limit, offset, m); } bool file_read(Pool& pool, const String& file_spec, void*& data, size_t& data_size, - bool as_text, - Hash* options, Request* r, - Hash** out_fields, + bool as_text, Hash *params, Hash** out_fields, bool fail_on_read_problem) { bool result; if(file_spec.starts_with("http://", 7)) { // fail on read problem - file_read_http(pool, file_spec, data, data_size, options, out_fields); + file_read_http(pool, file_spec, data, data_size, params, out_fields); result=true; } else { - File_read_action_info info={&data, &data_size, options, r}; + File_read_action_info info={&data, &data_size}; result=file_read_action_under_lock(pool, file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem); @@ -479,7 +452,8 @@ bool file_read(Pool& pool, const String& bool file_read_action_under_lock(Pool& pool, const String& file_spec, const char* action_name, File_read_action action, void *context, - bool as_text, bool fail_on_read_problem) { + bool as_text, + bool fail_on_read_problem) { const char* fname=file_spec.cstr(String::UL_FILE_SPEC); int f;