From 24da772e568b0bf2c869cb63ba94bcf9ae5e8140 Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Wed, 23 Nov 2022 06:59:23 +0100 Subject: [PATCH] feat: dissociate timeouts and empty pastes feuille was handling those two errors the same way, by sending `Timeout'd.' even if the client just sent an EOF. --- feuille.c | 5 ++++- server.c | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/feuille.c b/feuille.c index cf3ed3e..f1388e3 100644 --- a/feuille.c +++ b/feuille.c @@ -354,9 +354,12 @@ int main(int argc, char *argv[]) } } else { if (errno == EFBIG) - send_response(connection, "File too big.\n"); + send_response(connection, "Paste too big.\n"); if (errno == ENOENT) + send_response(connection, "Empty paste.\n"); + + if (errno == EAGAIN) send_response(connection, "Timeout'd.\n"); error("error %d while reading paste from incoming connection.", errno); diff --git a/server.c b/server.c index a52226a..12f1602 100644 --- a/server.c +++ b/server.c @@ -188,6 +188,7 @@ char *read_paste(int connection) /* read all data until EOF is received, or max file size is reached, or the socket timeouts... */ /* each time, the data is appended to the buffer, once it's been reallocated a larger size */ + errno = 0; long size; while ((size = recv(connection, buffer + total_size, buffer_size - total_size, 0)) > 0) { total_size += size; @@ -216,11 +217,15 @@ char *read_paste(int connection) } } + printf("%d\n", errno); + /* is the buffer empty? */ if (total_size == 0) { /* yup, free the buffer and return an error */ + if (errno != EAGAIN) + errno = ENOENT; + free(buffer); - errno = ENOENT; return NULL; }