From 128bc0afa98ab631327b104bfa43e09fbfcd8a68 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Sat, 12 Sep 2020 14:17:06 -0700 Subject: [PATCH] (http server) read body request when the Content-Length is specified + set timeout to read the request to 30 seconds max by default, and make it configurable as a constructor parameter --- docs/CHANGELOG.md | 3 +++ ixwebsocket/IXHttp.cpp | 4 +--- ixwebsocket/IXHttp.h | 2 +- ixwebsocket/IXHttpServer.cpp | 13 ++++++++++--- ixwebsocket/IXHttpServer.h | 6 +++++- ixwebsocket/IXWebSocketVersion.h | 2 +- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 682c61d3..058bf82f 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,9 @@ All changes to this project will be documented in this file. +## [10.4.0] - 2020-09-12 + +(http server) read body request when the Content-Length is specified + set timeout to read the request to 30 seconds max by default, and make it configurable as a constructor parameter ## [10.3.5] - 2020-09-09 diff --git a/ixwebsocket/IXHttp.cpp b/ixwebsocket/IXHttp.cpp index 4dfabbf7..218aa9b2 100644 --- a/ixwebsocket/IXHttp.cpp +++ b/ixwebsocket/IXHttp.cpp @@ -93,14 +93,12 @@ namespace ix } std::tuple Http::parseRequest( - std::unique_ptr& socket) + std::unique_ptr& socket, int timeoutSecs) { HttpRequestPtr httpRequest; std::atomic requestInitCancellation(false); - int timeoutSecs = 5; // FIXME - auto isCancellationRequested = makeCancellationRequestWithTimeout(timeoutSecs, requestInitCancellation); diff --git a/ixwebsocket/IXHttp.h b/ixwebsocket/IXHttp.h index 1d8807a7..062fcc0d 100644 --- a/ixwebsocket/IXHttp.h +++ b/ixwebsocket/IXHttp.h @@ -118,7 +118,7 @@ namespace ix { public: static std::tuple parseRequest( - std::unique_ptr& socket); + std::unique_ptr& socket, int timeoutSecs); static bool sendResponse(HttpResponsePtr response, std::unique_ptr& socket); static std::pair parseStatusLine(const std::string& line); diff --git a/ixwebsocket/IXHttpServer.cpp b/ixwebsocket/IXHttpServer.cpp index d1ccfe4f..ecc9038a 100644 --- a/ixwebsocket/IXHttpServer.cpp +++ b/ixwebsocket/IXHttpServer.cpp @@ -92,10 +92,17 @@ namespace namespace ix { - HttpServer::HttpServer( - int port, const std::string& host, int backlog, size_t maxConnections, int addressFamily) + const int HttpServer::kDefaultTimeoutSecs(30); + + HttpServer::HttpServer(int port, + const std::string& host, + int backlog, + size_t maxConnections, + int addressFamily, + int timeoutSecs) : SocketServer(port, host, backlog, maxConnections, addressFamily) , _connectedClientsCount(0) + , _timeoutSecs(timeoutSecs) { setDefaultConnectionCallback(); } @@ -124,7 +131,7 @@ namespace ix { _connectedClientsCount++; - auto ret = Http::parseRequest(socket); + auto ret = Http::parseRequest(socket, _timeoutSecs); // FIXME: handle errors in parseRequest if (std::get<0>(ret)) diff --git a/ixwebsocket/IXHttpServer.h b/ixwebsocket/IXHttpServer.h index 96b22d3f..08e9a5d0 100644 --- a/ixwebsocket/IXHttpServer.h +++ b/ixwebsocket/IXHttpServer.h @@ -29,7 +29,8 @@ namespace ix const std::string& host = SocketServer::kDefaultHost, int backlog = SocketServer::kDefaultTcpBacklog, size_t maxConnections = SocketServer::kDefaultMaxConnections, - int addressFamily = SocketServer::kDefaultAddressFamily); + int addressFamily = SocketServer::kDefaultAddressFamily, + int timeoutSecs = HttpServer::kDefaultTimeoutSecs); virtual ~HttpServer(); virtual void stop() final; @@ -42,6 +43,9 @@ namespace ix OnConnectionCallback _onConnectionCallback; std::atomic _connectedClientsCount; + const static int kDefaultTimeoutSecs; + int _timeoutSecs; + // Methods virtual void handleConnection(std::unique_ptr, std::shared_ptr connectionState) final; diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index b5ba933b..71da73c3 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "10.3.5" +#define IX_WEBSOCKET_VERSION "10.4.0"