Skip to content

Commit b04e5c5

Browse files
committed
http server: use socket->readBytes which reads in bulk instead of N calls to socket->readByte
1 parent 1e8c421 commit b04e5c5

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

ixwebsocket/IXHttp.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace ix
130130
return std::make_tuple(false, "Error parsing HTTP headers", httpRequest);
131131
}
132132

133-
std::string body = "";
133+
std::string body;
134134
if (headers.find("Content-Length") != headers.end())
135135
{
136136
int contentLength = 0;
@@ -144,13 +144,19 @@ namespace ix
144144
false, "Error parsing HTTP Header 'Content-Length'", httpRequest);
145145
}
146146

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

150-
for (int i = 0; i < contentLength; i++)
153+
auto res = socket->readBytes(contentLength, nullptr, isCancellationRequested);
154+
if (!res.first)
151155
{
152-
if (socket->readByte(&c, isCancellationRequested)) body += c;
156+
return std::make_tuple(
157+
false, std::string("Error reading request: ") + res.second, httpRequest);
153158
}
159+
body = res.second;
154160
}
155161

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

0 commit comments

Comments
 (0)