From 0dce4de5a13af020b8d0f66ff56d2f13ce3d43fd Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Fri, 4 Sep 2020 21:51:12 +0000 Subject: [PATCH] throw runtime error if response is shorter than expected or gets closed (#577) * throw runtime error if response is shorter than expected or gets closed * trhow Azure::Core::Http::TransportException --- sdk/core/azure-core/src/http/curl/curl.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/sdk/core/azure-core/src/http/curl/curl.cpp b/sdk/core/azure-core/src/http/curl/curl.cpp index 738a07d73..9f64a91a9 100644 --- a/sdk/core/azure-core/src/http/curl/curl.cpp +++ b/sdk/core/azure-core/src/http/curl/curl.cpp @@ -525,6 +525,23 @@ int64_t CurlSession::Read(Azure::Core::Context const& context, uint8_t* buffer, totalRead = ReadFromSocket(buffer, static_cast(readRequestLength)); this->m_sessionTotalRead += totalRead; + // Reading 0 bytes means closed connection. + // For known content length and chunked response, this means there is nothing else to read from + // server or lost connection before getting full response. + // For unknown response size, it means the end of response and it's fine. + if (totalRead == 0 && (this->m_contentLength > 0 || this->m_isChunkedResponseType)) + { + auto expectedToRead = this->m_isChunkedResponseType ? this->m_chunkSize : this->m_contentLength; + if (this->m_sessionTotalRead < expectedToRead) + { + throw Azure::Core::Http::TransportException( + "Connection closed before getting full response or response is less than expected. " + "Expected response length = " + + std::to_string(expectedToRead) + + ". Read until now = " + std::to_string(this->m_sessionTotalRead)); + } + } + return totalRead; }