medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f7e6830239d20e09b0d6259a32c74b0ec0bd6af4
parent 0a0b3ad62cf3a8f13ad496a5903aa10e8966344e
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Nov 18 21:15:44 +0200

Return GET_ERROR_TIMEOUT if the remote stops sending data for 10 seconds

Diffstat:
http.cc | 21+++++++++++++++++++++
http.h | 1+
2 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/http.cc b/http.cc @@ -1,4 +1,5 @@ #include <sys/types.h> +#include <sys/select.h> #include <sys/socket.h> #include <stdio.h> @@ -63,8 +64,28 @@ GetResult http_get( const struct sockaddr_storage & address, const char * host, std::string response; char buf[ BUFSIZ ]; while( true ) { + { + fd_set select_fds; + FD_ZERO( &select_fds ); + FD_SET( sock, &select_fds ); + + struct timeval timeout; + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + int ok = select( sock + 1, &select_fds, NULL, NULL, &timeout ); + if( ok == 0 ) { + return GET_ERROR_TIMEOUT; + } + if( ok == -1 ) { + if( errno != EINTR ) continue; + return GET_ERROR_IO; + } + } + ssize_t bytes_read = recv( sock, buf, sizeof( buf ), 0 ); if( bytes_read == -1 ) { + if( errno == EINTR ) continue; return GET_ERROR_IO; } diff --git a/http.h b/http.h @@ -14,6 +14,7 @@ enum GetResult { GET_ERROR_SOCKET, GET_ERROR_CONNECT, GET_ERROR_IO, + GET_ERROR_TIMEOUT, GET_ERROR_HTTP_BAD_REQUEST, GET_ERROR_HTTP_NOT_FOUND, GET_ERROR_HTTP_INTERNAL_SERVER_ERROR,