From fb0546312b07595b999069f415fc54c29bfb66a6 Mon Sep 17 00:00:00 2001 From: Gregoire Lodi Date: Sun, 8 Apr 2018 20:10:52 +0200 Subject: [PATCH 1/2] Changed timeout from 5 seconds to 500ms (tested against 1M files) --- fiche.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fiche.c b/fiche.c index c0bbe8d..5ff0b06 100644 --- a/fiche.c +++ b/fiche.c @@ -495,7 +495,7 @@ static void dispatch_connection(int socket, Fiche_Settings *settings) { } // Set timeout for accepted socket - const struct timeval timeout = { 5, 0 }; + const struct timeval timeout = { 0, 500 }; if ( setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0 ) { print_error("Couldn't set a timeout!"); From 2e9d26295abb1e40c20fe0a2f4dcf97c363fc198 Mon Sep 17 00:00:00 2001 From: Gregoire Lodi Date: Sun, 8 Apr 2018 20:11:48 +0200 Subject: [PATCH 2/2] Changed the way to write responsee - Writing len - 1, to avoid writing a \0 to stdout - Using asprintf to build the url instead of a VLA --- fiche.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/fiche.c b/fiche.c index 5ff0b06..7fad059 100644 --- a/fiche.c +++ b/fiche.c @@ -26,6 +26,8 @@ Use netcat to push text - example: ------------------------------------------------------------------------------- */ +#define _GNU_SOURCE + #include "fiche.h" #include @@ -46,7 +48,7 @@ Use netcat to push text - example: #include #include #include -#include +#include /****************************************************************************** @@ -655,11 +657,12 @@ static void *handle_connection(void *args) { // Create an url (additional byte for slash and one for new line) const size_t len = strlen(c->settings->domain) + strlen(slug) + 3; - char url[len]; - snprintf(url, len, "%s%s%s%s", c->settings->domain, "/", slug, "\n"); + char *url; + asprintf(&url, "%s%s%s%s", c->settings->domain, "/", slug, "\n"); // Send the response - write(c->socket, url, len); + write(c->socket, url, len - 1); + free(url); } print_status("Received %d bytes, saved to: %s.", r, slug);