HttpRequest: use data from the input buffer first

The HttpRequest protocol loop is designed using an input buffer storing
data from the socket. At each loop, we try to parse some of the data,
and then read more from the socket.

However, in some cases (in particular with chunks, which we parse only
one at a time in a loop iteration), we may not use all the data from the
buffer. Eventually, we will be left with an "empty" socket (nothing to
read from there) but the request not completed because there is still
data in the input buffer.

In that case, we would hang waiting for a read on the socket, instead of
processing data from the input buffer.

Change the code to read from the socket only if a loop iteration did not
manage to read anything from the input buffer. This means the input
buffer is too small for the next thing to process (it contains less than
one line of data, for example), and in that case we can safely read from
the socket without being blocked.

This should fix several cases where the network code was stuck doing
nothing, including https://my.justenergy.com/ reported in #13010.
This commit is contained in:
Adrien Destugues
2016-10-31 22:00:40 +01:00
parent af7d48fe5b
commit a9665fc66a
+4 -1
View File
@@ -570,6 +570,7 @@ BHttpRequest::_MakeRequest()
ssize_t bytesRead = 0;
ssize_t bytesReceived = 0;
ssize_t bytesTotal = 0;
size_t previousBufferSize = 0;
off_t bytesUnpacked = 0;
char* inputTempBuffer = new(std::nothrow) char[kHttpBufferSize];
ssize_t inputTempSize = kHttpBufferSize;
@@ -579,7 +580,7 @@ BHttpRequest::_MakeRequest()
ObjectDeleter<BDataIO> decompressingStreamDeleter;
while (!fQuit && !(receiveEnd && parseEnd)) {
if (!receiveEnd) {
if ((!receiveEnd) && (fInputBuffer.Size() == previousBufferSize)) {
fSocket->WaitForReadable();
BStackOrHeapArray<char, 4096> chunk(kHttpBufferSize);
bytesRead = fSocket->Read(chunk, kHttpBufferSize);
@@ -594,6 +595,8 @@ BHttpRequest::_MakeRequest()
} else
bytesRead = 0;
previousBufferSize = fInputBuffer.Size();
if (fRequestStatus < kRequestStatusReceived) {
_ParseStatus();