From b04e5c55299265bee7631ce756804d6f583d6e5d Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Sat, 12 Sep 2020 14:09:25 -0700 Subject: [PATCH] http server: use socket->readBytes which reads in bulk instead of N calls to socket->readByte --- ixwebsocket/IXHttp.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ixwebsocket/IXHttp.cpp b/ixwebsocket/IXHttp.cpp index 5648c284..4dfabbf7 100644 --- a/ixwebsocket/IXHttp.cpp +++ b/ixwebsocket/IXHttp.cpp @@ -130,7 +130,7 @@ namespace ix return std::make_tuple(false, "Error parsing HTTP headers", httpRequest); } - std::string body = ""; + std::string body; if (headers.find("Content-Length") != headers.end()) { int contentLength = 0; @@ -144,13 +144,19 @@ namespace ix false, "Error parsing HTTP Header 'Content-Length'", httpRequest); } - char c; - body.reserve(contentLength); - - for (int i = 0; i < contentLength; i++) + if (contentLength < 0) { - if (socket->readByte(&c, isCancellationRequested)) body += c; + return std::make_tuple( + false, "Error: 'Content-Length' should be a positive integer", httpRequest); } + + auto res = socket->readBytes(contentLength, nullptr, isCancellationRequested); + if (!res.first) + { + return std::make_tuple( + false, std::string("Error reading request: ") + res.second, httpRequest); + } + body = res.second; } httpRequest = std::make_shared(uri, method, httpVersion, body, headers);