From 0bcc72208e1c3a8cc05360f73b27dafc71df3cc0 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Wed, 23 Nov 2016 22:25:08 +0000 Subject: [PATCH] Add initial (non-functioning) keepalive support in S3Client (cherry picked from commit 3c62a5be57e0a7b7100b44c41cc8c69c20c92fa9) --- lib/httpserver/S3Client.cpp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/httpserver/S3Client.cpp b/lib/httpserver/S3Client.cpp index 07bb7920b..24d6f37b4 100644 --- a/lib/httpserver/S3Client.cpp +++ b/lib/httpserver/S3Client.cpp @@ -178,7 +178,8 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, } request.AddHeader("Authorization", auth_code); - + HTTPResponse response; + if (mpSimulator) { if (pStreamToSend) @@ -187,11 +188,13 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, } request.SetForReading(); - CollectInBufferStream response_buffer; - HTTPResponse response(&response_buffer); - mpSimulator->Handle(request, response); - return response; + + // TODO FIXME: HTTPServer::Connection does some post-processing on every + // response to determine whether Connection: keep-alive is possible. + // We should do that here too, but currently our HTTP implementation + // doesn't support chunked encoding, so it's disabled there, so we don't + // do it here either. } else { @@ -203,7 +206,7 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, mapClientSocket->Open(Socket::TypeINET, mHostName, mPort); } - return SendRequest(request, pStreamToSend, + response = SendRequest(request, pStreamToSend, pStreamContentType); } catch (ConnectionException &ce) @@ -214,7 +217,7 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, // try to reconnect, just once mapClientSocket->Open(Socket::TypeINET, mHostName, mPort); - return SendRequest(request, pStreamToSend, + response = SendRequest(request, pStreamToSend, pStreamContentType); } else @@ -224,6 +227,17 @@ HTTPResponse S3Client::FinishAndSendRequest(HTTPRequest::Method Method, } } } + + // It's not valid to have a keep-alive response if the length isn't known. + // S3Simulator should really check this, but depending on how it's called above, + // it might be possible to bypass that check, so this is a double-check. + ASSERT(response.GetContentLength() >= 0 || !response.IsKeepAlive()); + + BOX_TRACE("S3Client: " << mHostName << " < " << response.GetResponseCode() << + ": " << response.GetContentLength() << " bytes") + response.SetForReading(); + + return response; } // -------------------------------------------------------------------------- @@ -254,6 +268,8 @@ HTTPResponse S3Client::SendRequest(HTTPRequest& rRequest, } else { + // No stream, so it's always safe to enable keep-alive + rRequest.SetClientKeepAliveRequested(true); rRequest.Send(*mapClientSocket, mNetworkTimeout); response.Receive(*mapClientSocket, mNetworkTimeout); }