Skip to content

Commit

Permalink
(http server) read body request when the Content-Length is specified …
Browse files Browse the repository at this point in the history
…+ set timeout to read the request to 30 seconds max by default, and make it configurable as a constructor parameter
  • Loading branch information
bsergean committed Sep 12, 2020
1 parent b04e5c5 commit 128bc0a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions ixwebsocket/IXHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,12 @@ namespace ix
}

std::tuple<bool, std::string, HttpRequestPtr> Http::parseRequest(
std::unique_ptr<Socket>& socket)
std::unique_ptr<Socket>& socket, int timeoutSecs)
{
HttpRequestPtr httpRequest;

std::atomic<bool> requestInitCancellation(false);

int timeoutSecs = 5; // FIXME

auto isCancellationRequested =
makeCancellationRequestWithTimeout(timeoutSecs, requestInitCancellation);

Expand Down
2 changes: 1 addition & 1 deletion ixwebsocket/IXHttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace ix
{
public:
static std::tuple<bool, std::string, HttpRequestPtr> parseRequest(
std::unique_ptr<Socket>& socket);
std::unique_ptr<Socket>& socket, int timeoutSecs);
static bool sendResponse(HttpResponsePtr response, std::unique_ptr<Socket>& socket);

static std::pair<std::string, int> parseStatusLine(const std::string& line);
Expand Down
13 changes: 10 additions & 3 deletions ixwebsocket/IXHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion ixwebsocket/IXHttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -42,6 +43,9 @@ namespace ix
OnConnectionCallback _onConnectionCallback;
std::atomic<int> _connectedClientsCount;

const static int kDefaultTimeoutSecs;
int _timeoutSecs;

// Methods
virtual void handleConnection(std::unique_ptr<Socket>,
std::shared_ptr<ConnectionState> connectionState) final;
Expand Down
2 changes: 1 addition & 1 deletion ixwebsocket/IXWebSocketVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

#pragma once

#define IX_WEBSOCKET_VERSION "10.3.5"
#define IX_WEBSOCKET_VERSION "10.4.0"

0 comments on commit 128bc0a

Please sign in to comment.