From c77cae46355fd4c6503da8cd67a2ad2067df002f Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 25 Sep 2022 15:48:00 +0200 Subject: [PATCH] Require 16kb/sec HTTP upload throughput or drop the uploader --- src/HttpContext.h | 10 +++++++++- src/HttpResponse.h | 3 +++ src/HttpResponseData.h | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/HttpContext.h b/src/HttpContext.h index 8fcd997..a759255 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -43,6 +43,9 @@ private: /* Maximum delay allowed until an HTTP connection is terminated due to outstanding request or rejected data (slow loris protection) */ static const int HTTP_IDLE_TIMEOUT_S = 10; + /* Minimum allowed receive throughput per second (clients uploading less than 16kB/sec get dropped) */ + static const int HTTP_RECEIVE_THROUGHPUT_BYTES = 16 * 1024; + us_socket_context_t *getSocketContext() { return (us_socket_context_t *) this; } @@ -205,7 +208,12 @@ private: us_socket_timeout(SSL, (struct us_socket_t *) user, 0); } else { /* We still have some more data coming in later, so reset timeout */ - us_socket_timeout(SSL, (struct us_socket_t *) user, HTTP_IDLE_TIMEOUT_S); + /* Only reset timeout if we got enough bytes (16kb/sec) since last time we reset here */ + httpResponseData->received_bytes_per_timeout += (unsigned int) data.length(); + if (httpResponseData->received_bytes_per_timeout >= HTTP_RECEIVE_THROUGHPUT_BYTES * HTTP_IDLE_TIMEOUT_S) { + us_socket_timeout(SSL, (struct us_socket_t *) user, HTTP_IDLE_TIMEOUT_S); + httpResponseData->received_bytes_per_timeout = 0; + } } /* We might respond in the handler, so do not change timeout after this */ diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 05fb56b..83f15c9 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -490,6 +490,9 @@ public: void onData(MoveOnlyFunction &&handler) { HttpResponseData *data = getHttpResponseData(); data->inStream = std::move(handler); + + /* Always reset this counter here */ + data->received_bytes_per_timeout = 0; } }; diff --git a/src/HttpResponseData.h b/src/HttpResponseData.h index 2c8feec..1e7604c 100644 --- a/src/HttpResponseData.h +++ b/src/HttpResponseData.h @@ -79,6 +79,9 @@ private: /* Outgoing offset */ uintmax_t offset = 0; + /* Let's track number of bytes since last timeout reset in data handler */ + unsigned int received_bytes_per_timeout = 0; + /* Current state (content-length sent, status sent, write called, etc */ int state = 0;