Fixed #4137 - Don't close CURL connection on shutdown (#4142)

* Fixed #4137 - Don't close connection on shutdown

* clang-format
This commit is contained in:
Larry Osterman 2022-12-05 14:32:50 -07:00 committed by GitHub
parent 09db139a71
commit 5737bfeb6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 12 deletions

View File

@ -557,6 +557,12 @@ CURLcode CurlConnection::SendBuffer(
size_t bufferSize,
Context const& context)
{
// Once you've shutdown the connection, we can't send any more data (although we can continue to
// receive).
if (IsShutdown())
{
return CURLE_SEND_ERROR;
}
for (size_t sentBytesTotal = 0; sentBytesTotal < bufferSize;)
{
// check cancelation for each chunk of data.
@ -1065,16 +1071,6 @@ size_t CurlSession::OnRead(uint8_t* buffer, size_t count, Context const& context
return totalRead;
}
void CurlConnection::Shutdown()
{
#if defined(AZ_PLATFORM_POSIX)
::shutdown(m_curlSocket, SHUT_RDWR);
#elif defined(AZ_PLATFORM_WINDOWS)
::shutdown(m_curlSocket, SD_BOTH);
#endif
CurlNetworkConnection::Shutdown();
}
// Read from socket and return the number of bytes taken from socket
size_t CurlConnection::ReadFromSocket(uint8_t* buffer, size_t bufferSize, Context const& context)
{

View File

@ -230,8 +230,6 @@ namespace Azure { namespace Core {
*/
CURLcode SendBuffer(uint8_t const* buffer, size_t bufferSize, Context const& context)
override;
void Shutdown() override;
};
} // namespace Http
}} // namespace Azure::Core

View File

@ -601,6 +601,29 @@ namespace Azure { namespace Core { namespace Test {
}
}
TEST(CurlConnectionPool, forceConnectionClosed)
{
Azure::Core::Http::Request req(
Azure::Core::Http::HttpMethod::Get, Azure::Core::Url(AzureSdkHttpbinServer::Status(101)));
Azure::Core::Http::CurlTransportOptions options;
auto connection
= CurlConnectionPool::g_curlConnectionPool.ExtractOrCreateCurlConnection(req, options);
{
// Check we can use the connection to retrieve headers in the response, but the connection
// is still flagged as shutdown.
auto session
= std::make_unique<Azure::Core::Http::CurlSession>(req, std::move(connection), options);
auto r = session->Perform(Azure::Core::Context::ApplicationContext);
EXPECT_EQ(CURLE_OK, r);
auto response = session->ExtractResponse();
EXPECT_EQ(response->GetStatusCode(), Azure::Core::Http::HttpStatusCode::SwitchingProtocols);
EXPECT_EQ("close", response->GetHeaders().find("Connection")->second);
}
}
TEST(CurlConnectionPool, connectionClose)
{
/// When getting the header connection: close from an HTTP response, the connection should not

View File

@ -33,6 +33,10 @@ namespace Azure { namespace Core { namespace Test {
inline static std::string Delete() { return Schema() + "://" + Host() + "/delete"; }
inline static std::string Patch() { return Schema() + "://" + Host() + "/patch"; }
inline static std::string Delay() { return Schema() + "://" + Host() + "/delay"; }
inline static std::string Status(int statusCode)
{
return Schema() + "://" + Host() + "/status/" + std::to_string(statusCode);
}
inline static std::string Host() { return std::string(_detail::AzureSdkHttpbinServer); }
inline static std::string Schema() { return std::string(_detail::AzureSdkHttpbinServerSchema); }
};