From a9665fc66a4a91a7d342a115fd82820ebc231f7d Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Mon, 31 Oct 2016 22:00:40 +0100 Subject: [PATCH] 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. --- src/kits/network/libnetapi/HttpRequest.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/kits/network/libnetapi/HttpRequest.cpp b/src/kits/network/libnetapi/HttpRequest.cpp index 57d1dea6ca..d91654a9e6 100644 --- a/src/kits/network/libnetapi/HttpRequest.cpp +++ b/src/kits/network/libnetapi/HttpRequest.cpp @@ -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 decompressingStreamDeleter; while (!fQuit && !(receiveEnd && parseEnd)) { - if (!receiveEnd) { + if ((!receiveEnd) && (fInputBuffer.Size() == previousBufferSize)) { fSocket->WaitForReadable(); BStackOrHeapArray chunk(kHttpBufferSize); bytesRead = fSocket->Read(chunk, kHttpBufferSize); @@ -594,6 +595,8 @@ BHttpRequest::_MakeRequest() } else bytesRead = 0; + previousBufferSize = fInputBuffer.Size(); + if (fRequestStatus < kRequestStatusReceived) { _ParseStatus();