--- parser3/src/main/pa_common.C 2002/12/15 14:25:22 1.138 +++ parser3/src/main/pa_common.C 2003/03/21 07:18:01 1.148 @@ -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/15 14:25:22 $"; +static const char* IDENT_COMMON_C="$Date: 2003/03/21 07:18:01 $"; #include "pa_common.h" #include "pa_exception.h" @@ -88,6 +88,9 @@ static int unlock(int fd) { FLOCK(F_TLOC void fix_line_breaks(char* buf, size_t& size) { + if(size==0) + return; + //_asm int 3; const char* const eob=buf+size; char* dest=buf; @@ -136,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); @@ -145,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()); @@ -168,42 +171,50 @@ 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 - signal(SIGALRM, timeout_handler); +#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"); + return 0; // never + } else { + alarm(timeout); #endif + try { + int result; struct sockaddr_in dest; - - if(!set_addr(&dest, host, port)) + + if(!set_addr(&dest, host, port)) throw Exception("http.host", origin_string, "can not resolve hostname \"%s\"", host); @@ -222,20 +233,23 @@ static void http_request(String& respons origin_string, "error sending request: %s (%d)", strerror(errno), errno); - http_read_response(response, sock); + result=http_read_response(response, sock, fail_on_status_ne_200); + closesocket(sock); +#ifdef PA_USE_ALARM + alarm(0); +#endif + return result; + } catch(...) { +#ifdef PA_USE_ALARM + alarm(0); +#endif if(sock>=0) closesocket(sock); -#ifdef WE_CAN_USE_ALARM + /*re*/throw; } -#endif - } catch(...) { - if(sock>=0) - closesocket(sock); -#ifdef WE_CAN_USE_ALARM - alarm(0); -#endif - /*re*/throw; +#ifdef PA_USE_ALARM } +#endif } #ifndef DOXYGEN @@ -264,9 +278,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 @@ -297,6 +312,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, @@ -323,8 +343,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); @@ -341,13 +362,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