From 3528905be60b3752b3a99f5c1ce32bb7f74e6be8 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Wed, 16 Jul 2014 17:34:31 +0200 Subject: [PATCH] Parse multiple HTTP at once Instead of relying on the global protocol loop to call _ParseHeaders once for each header, extract as much as possible from the current buffer. This saves memory, avoids useless operations on the socket and various processing steps, and fixes #10245. Also improve the handling of 0-size requests to make sure they terminate properly. --- src/kits/network/libnetapi/HttpRequest.cpp | 29 +++++++++++----------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/kits/network/libnetapi/HttpRequest.cpp b/src/kits/network/libnetapi/HttpRequest.cpp index cc4d6785d5..ed28b90f15 100644 --- a/src/kits/network/libnetapi/HttpRequest.cpp +++ b/src/kits/network/libnetapi/HttpRequest.cpp @@ -653,7 +653,7 @@ BHttpRequest::_MakeRequest() if (index != B_ERROR) bytesTotal = atoi(fHeaders.HeaderAt(index).Value()); else - bytesTotal = 0; + bytesTotal = -1; } } @@ -731,7 +731,7 @@ BHttpRequest::_MakeRequest() } } - if (bytesRead > 0) { + if (bytesRead >= 0) { bytesReceived += bytesRead; if (fListener != NULL) { @@ -749,15 +749,15 @@ BHttpRequest::_MakeRequest() size); bytesUnpacked += size; } - } else { + } else if (bytesRead > 0) { fListener->DataReceived(this, inputTempBuffer, bytesReceived - bytesRead, bytesRead); } fListener->DownloadProgress(this, bytesReceived, - bytesTotal); + std::max((ssize_t)0, bytesTotal)); } - if (bytesTotal > 0 && bytesReceived >= bytesTotal) { + if (bytesTotal >= 0 && bytesReceived >= bytesTotal) { receiveEnd = true; if (decompress) { @@ -854,17 +854,18 @@ void BHttpRequest::_ParseHeaders() { BString currentHeader; - if (_GetLine(currentHeader) == B_ERROR) - return; + while (_GetLine(currentHeader) != B_ERROR) + { + // An empty line means the end of the header section + if (currentHeader.Length() == 0) { + fRequestStatus = kRequestHeadersReceived; + return; + } - // An empty line means the end of the header section - if (currentHeader.Length() == 0) { - fRequestStatus = kRequestHeadersReceived; - return; + _EmitDebug(B_URL_PROTOCOL_DEBUG_HEADER_IN, "%s", + currentHeader.String()); + fHeaders.AddHeader(currentHeader.String()); } - - _EmitDebug(B_URL_PROTOCOL_DEBUG_HEADER_IN, "%s", currentHeader.String()); - fHeaders.AddHeader(currentHeader.String()); }