smol_http

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

commit d61962c084ef57650abb108343033c8c3d75426a
parent c7fc4f0cdb6a01c1a737ca8d07761e83d9e722f4
Author: Anton Kling <anton@kling.gg>
Date:   Wed,  2 Feb 2022 12:16:25 +0100

Added a timeout check to ensure that recv/send do not take too long.
Previously this lead resources being hogged as the forked children could
live forever.

Diffstat:
Msmol_http.c | 14++++++++++++++
1 file changed, 14 insertions(+), 0 deletions(-)

diff --git a/smol_http.c b/smol_http.c @@ -38,6 +38,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // Default directory should -d not be supplied. #define WEBSITE_ROOT "./site/" +#define TIMEOUT_SECOND 3 +#define TIMEOUT_USECOND 0 + #define MAX_BUFFER 4096 // Size of the read buffer #ifndef PATH_MAX @@ -142,6 +145,17 @@ void connection_handler(int socket_desc) COND_PERROR_EXP(SIG_ERR == signal(SIGPIPE, SIG_IGN), "signal", goto cleanup); + // Ensure that we timeout should the send/recv take too long. + struct timeval timeout; + timeout.tv_sec = TIMEOUT_SECOND; + timeout.tv_usec = TIMEOUT_USECOND; + COND_PERROR_EXP(-1 == setsockopt(socket_desc, SOL_SOCKET, SO_RCVTIMEO, + &timeout, sizeof(timeout)), + "setsockopt", goto cleanup); + COND_PERROR_EXP(-1 == setsockopt(socket_desc, SOL_SOCKET, SO_SNDTIMEO, + &timeout, sizeof(timeout)), + "setsockopt", goto cleanup); + ssize_t recv_size; COND_PERROR_EXP(-1 == (recv_size = recv(socket_desc, recv_buffer, MAX_BUFFER - 1, 0)),