http server: use socket->readBytes which reads in bulk instead of N calls to socket->readByte

This commit is contained in:
Benjamin Sergeant 2020-09-12 14:09:25 -07:00
parent 1e8c421d66
commit b04e5c5529

View File

@ -130,7 +130,7 @@ namespace ix
return std::make_tuple(false, "Error parsing HTTP headers", httpRequest); return std::make_tuple(false, "Error parsing HTTP headers", httpRequest);
} }
std::string body = ""; std::string body;
if (headers.find("Content-Length") != headers.end()) if (headers.find("Content-Length") != headers.end())
{ {
int contentLength = 0; int contentLength = 0;
@ -144,13 +144,19 @@ namespace ix
false, "Error parsing HTTP Header 'Content-Length'", httpRequest); false, "Error parsing HTTP Header 'Content-Length'", httpRequest);
} }
char c; if (contentLength < 0)
body.reserve(contentLength);
for (int i = 0; i < contentLength; i++)
{ {
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<HttpRequest>(uri, method, httpVersion, body, headers); httpRequest = std::make_shared<HttpRequest>(uri, method, httpVersion, body, headers);