--- parser3/src/main/pa_common.C 2002/12/26 11:45:37 1.140 +++ parser3/src/main/pa_common.C 2003/03/17 14:48:05 1.145.2.1 @@ -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: 2002/12/26 11:45:37 $"; +static const char* IDENT_COMMON_C="$Date: 2003/03/17 14:48:05 $"; #include "pa_common.h" #include "pa_exception.h" @@ -139,30 +139,30 @@ 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); + char *buf=(char *)response.pool().malloc_atomic(MAX_STRING); ssize_t size=recv(sock, buf, MAX_STRING, 0); 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()); @@ -171,39 +171,46 @@ static void http_read_response(String& r /* ********************** request *************************** */ #if defined(SIGALRM) && defined(HAVE_SIGSETJMP) && defined(HAVE_SIGLONGJMP) -# define WE_CAN_USE_ALARM +# define PA_USE_ALARM #endif -#ifdef WE_CAN_USE_ALARM +#ifdef PA_USE_ALARM static sigjmp_buf timeout_env; static void timeout_handler(int sig){ siglongjmp(timeout_env, 1); } #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 WE_CAN_USE_ALARM +#ifdef PA_USE_ALARM signal(SIGALRM, timeout_handler); #endif int sock=-1; - try { -#ifdef WE_CAN_USE_ALARM - if(sigsetjmp(timeout_env, 1)) - throw Exception("http.timeout", - origin_string, - "timeout occured while retrieving document"); - else { - alarm(timeout); +#ifdef PA_USE_ALARM + if(sigsetjmp(timeout_env, 1)) { + // stupid gcc [2.95.4] generated bad code + // which failed to handle sigsetjmp+throw: crashed inside of pre-throw code. + // rewritten simplier [though duplicating closesocket code] + if(sock>=0) + closesocket(sock); + throw Exception("http.timeout", + origin_string, + "timeout occured while retrieving document"); + } else { + alarm(timeout); #endif + try { + int result; struct sockaddr_in dest; if(!set_addr(&dest, host, port)) @@ -225,16 +232,17 @@ static void http_request(String& respons origin_string, "error sending request: %s (%d)", strerror(errno), errno); - http_read_response(response, sock); - if(sock>=0) - closesocket(sock); -#ifdef WE_CAN_USE_ALARM + result=http_read_response(response, sock, fail_on_status_ne_200); + closesocket(sock); +#ifdef PA_USE_ALARM + alarm(0); } #endif + return result; } catch(...) { if(sock>=0) closesocket(sock); -#ifdef WE_CAN_USE_ALARM +#ifdef PA_USE_ALARM alarm(0); #endif /*re*/throw; @@ -267,9 +275,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 @@ -300,6 +309,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, @@ -326,8 +340,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); @@ -344,13 +359,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=pool.malloc_atomic(to_read_size+(as_text?1:0), 3); *info.data_size=(size_t)read(f, *info.data, to_read_size); if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size) @@ -397,7 +407,7 @@ static void file_read_action(Pool& pool, *info.data_size, to_read_size); } else { // empty file if(as_text) { - *info.data=pool.malloc(1); + *info.data=pool.malloc_atomic(1); *(char*)(*info.data)=0; } else *info.data=0; @@ -755,7 +765,7 @@ char* format(Pool& pool, double value, c else size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value); - char* pool_buf=(char *)pool.malloc(size+1, 4); + char* pool_buf=(char *)pool.malloc_atomic(size+1, 4); memcpy(pool_buf, local_buf, size+1); return pool_buf; } @@ -777,7 +787,7 @@ size_t stdout_write(const void *buf, siz } char* unescape_chars(Pool& pool, const char* cp, int len) { - char* s=(char *)pool.malloc(len + 1, 5); + char* s=(char *)pool.malloc_atomic(len + 1, 5); enum EscapeState { EscapeRest, EscapeFirst, @@ -973,7 +983,7 @@ static void append_attribute_meaning(Str result.append(string->join_chains(result.pool(), 0), lang, forced); else if(Value *vdate=value.as(VDATE_TYPE, false)) { - char *buf=(char *)result.malloc(MAX_STRING); + char *buf=(char *)result.malloc_atomic(MAX_STRING); size_t size=date_attribute(*static_cast(vdate), buf, MAX_STRING);