Skip to content

Commit

Permalink
http server: use socket->readBytes which reads in bulk instead of N c…
Browse files Browse the repository at this point in the history
…alls to socket->readByte
  • Loading branch information
bsergean committed Sep 12, 2020
1 parent 1e8c421 commit b04e5c5
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions ixwebsocket/IXHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -144,13 +144,19 @@ namespace ix
false, "Error parsing HTTP Header 'Content-Length'", httpRequest);
}

char c;
body.reserve(contentLength);
if (contentLength < 0)
{
return std::make_tuple(
false, "Error: 'Content-Length' should be a positive integer", httpRequest);
}

for (int i = 0; i < contentLength; i++)
auto res = socket->readBytes(contentLength, nullptr, isCancellationRequested);
if (!res.first)
{
if (socket->readByte(&c, isCancellationRequested)) body += c;
return std::make_tuple(
false, std::string("Error reading request: ") + res.second, httpRequest);
}
body = res.second;
}

httpRequest = std::make_shared<HttpRequest>(uri, method, httpVersion, body, headers);
Expand Down

0 comments on commit b04e5c5

Please sign in to comment.