--- parser3/src/main/pa_http.C 2020/10/14 00:07:42 1.92 +++ parser3/src/main/pa_http.C 2020/10/14 21:22:59 1.96 @@ -14,7 +14,7 @@ #include "pa_vfile.h" #include "pa_random.h" -volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.92 2020/10/14 00:07:42 moko Exp $" IDENT_PA_HTTP_H; +volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.96 2020/10/14 21:22:59 moko Exp $" IDENT_PA_HTTP_H; #ifdef _MSC_VER #include @@ -59,7 +59,7 @@ bool HTTP_Headers::add_header(const char content_type=header.value; if(header.name == String::Body("CONTENT-LENGTH") && content_length==0) - content_length=pa_atoul(header.value.cstr(), 10); + ALTER_EXCEPTION_COMMENT(content_length=pa_atoul(header.value.cstr()), " for content-length"); headers+=header; @@ -153,7 +153,7 @@ public: return status_line; const char *result_str=pa_strdup(status_start, status_end-status_start); - result=pa_atoui(result_str, 10); + ALTER_EXCEPTION_COMMENT(result=pa_atoui(result_str), " for HTTP status"); return result_str; } @@ -227,7 +227,7 @@ int HTTP_response::read_response(int soc parse_headers(); - size_t content_length=check_file_size(headers.content_length, url); + size_t content_length=check_file_size(headers.content_length, &url); if(content_length>0 && (content_length + body_offset) > length){ resize(content_length + body_offset + 0x400*64); } @@ -867,7 +867,8 @@ File_read_http_result pa_internal_file_r HTTP_response response(connect_string); // sending request - int status_code=http_request(response, idna_host, port, request, request_size, timeout_secs, fail_on_status_ne_200); + int status_code; + ALTER_EXCEPTION_SOURCE(status_code=http_request(response, idna_host, port, request, request_size, timeout_secs, fail_on_status_ne_200), &connect_string); // processing results char* raw_body=response.buf + response.body_offset; @@ -1044,10 +1045,6 @@ void HTTPD_Connection::read_header(){ request->read_header(sock); } -void HTTPD_Connection::close(){ - ::closesocket(sock); -} - size_t HTTPD_Connection::read_post(char *body, size_t max_bytes) { return request->read_post(sock, body, max_bytes); } @@ -1060,7 +1057,47 @@ size_t HTTPD_Connection::send_body(const return size; } -static int sock_on = 1; +HTTPD_Connection::~HTTPD_Connection(){ + if(sock != -1) + closesocket(sock); +} + +static int sock_ready(int fd,int operation,int timeout_value){ + struct timeval timeout = {0, timeout_value * 1000}; + fd_set fds; + FD_ZERO(&fds); + FD_SET(fd, &fds); + switch (operation){ + case 0: return select(fd + 1, &fds, NULL, NULL, &timeout)>0; /* read */ + case 1: return select(fd + 1, NULL, &fds, NULL, &timeout)>0; /* write */ + default: return select(fd + 1, &fds, &fds, NULL, &timeout)>0; /* both */ + } +} + +bool HTTPD_Connection::accept(int server_sock, int timeout_value) { + int ready = sock_ready(server_sock, 0, timeout_value); + if (ready < 0) { + int no=pa_socks_errno(); + if(no == EINTR) + return false; + throw Exception("httpd.accept", 0, "error waiting for connection: %s (%d)", pa_socks_strerr(no), no); + } + if (ready == 0) + return false; /* Timeout */ + + struct sockaddr_in addr; + socklen_t sock_addr_len = sizeof(struct sockaddr_in); + memset(&addr, 0, sock_addr_len); + + sock = ::accept(server_sock, (struct sockaddr *)&addr, &sock_addr_len); + if(server_sock == -1){ + int no=pa_socks_errno(); + throw Exception("httpd.accept", 0, "error accepting connection: %s (%d)", pa_socks_strerr(no), no); + } + + remote_addr = pa_strdup(inet_ntoa(addr.sin_addr)); + return true; +} int HTTPD_Server::bind(const char *host_port){ struct sockaddr_in me; @@ -1074,7 +1111,7 @@ int HTTPD_Server::bind(const char *host_ port = host_port; } - if(!set_addr(&me, host, pa_atoui(port, 10))){ + if(!set_addr(&me, host, pa_atoui(port))){ if (host) throw Exception("httpd.bind", 0, "can not resolve hostname \"%s\"", host); me.sin_addr.s_addr=INADDR_ANY; @@ -1087,6 +1124,8 @@ int HTTPD_Server::bind(const char *host_ throw Exception("httpd.bind", 0, "can not make socket: %s (%d)", pa_socks_strerr(no), no); } + static int sock_on = 1; + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_on, sizeof(sock_on)) || setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&sock_on, sizeof(sock_on)) || ::bind(sock, (struct sockaddr*)&me, sizeof(me)) || @@ -1097,42 +1136,3 @@ int HTTPD_Server::bind(const char *host_ } return sock; } - -static int ready(int fd,int operation,int timeout_value){ - struct timeval timeout = {0, timeout_value * 1000}; - fd_set fds; - FD_ZERO(&fds); - FD_SET(fd, &fds); - switch (operation){ - case 0: return select(fd + 1, &fds, NULL, NULL, &timeout)>0; /* read */ - case 1: return select(fd + 1, NULL, &fds, NULL, &timeout)>0; /* write */ - default: return select(fd + 1, &fds, &fds, NULL, &timeout)>0; /* both */ - } -} - -HTTPD_Connection *HTTPD_Server::accept(int sock, int timeout_value) { - int ready = ::ready(sock, 0, timeout_value); - if (ready < 0) { - int no=pa_socks_errno(); - if(no == EINTR) - return NULL; - throw Exception("httpd.accept", 0, "error waiting for connection: %s (%d)", pa_socks_strerr(no), no); - } - if (ready == 0) { - /* Timeout */ - return NULL; - } - - struct sockaddr_in addr; - socklen_t sock_addr_len = sizeof(struct sockaddr_in); - memset(&addr, 0, sock_addr_len); - - int csock = ::accept(sock, (struct sockaddr *)&addr, &sock_addr_len); - if(csock == -1){ - int no=pa_socks_errno(); - throw Exception("httpd.accept", 0, "error accepting connection: %s (%d)", pa_socks_strerr(no), no); - } - - return new HTTPD_Connection(csock, pa_strdup(inet_ntoa(addr.sin_addr))); -} -