From a460718ac59ea50647e34eeeef92124fe1becaeb Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 25 Jul 2017 16:21:00 -0400 Subject: [PATCH] Fix several test failures Commit migrated from https://github.com/dotnet/corefx/commit/d659a500dd349eb570e074b95de265b9254dda7e --- .../System/Net/Http/Managed/HttpConnection.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs index 7ad24b1c5b4..9df9ac5c92b 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs @@ -280,13 +280,20 @@ namespace System.Net.Http if (request.Method == HttpMethod.Head || (int)response.StatusCode == 204 || (int)response.StatusCode == 304) { responseStream = EmptyReadStream.Instance; + ReturnConnectionToPool(); } else if (response.Content.Headers.ContentLength != null) { long contentLength = response.Content.Headers.ContentLength.GetValueOrDefault(); - responseStream = contentLength == 0 ? - (HttpContentReadStream)EmptyReadStream.Instance : - new ContentLengthReadStream(this, contentLength); + if (contentLength == 0) + { + responseStream = EmptyReadStream.Instance; + ReturnConnectionToPool(); + } + else + { + responseStream = new ContentLengthReadStream(this, contentLength); + } } else if (response.Headers.TransferEncodingChunked == true) { @@ -303,7 +310,6 @@ namespace System.Net.Http catch (Exception inner) { Dispose(); - if (inner is InvalidOperationException || inner is IOException) { throw new HttpRequestException(SR.net_http_client_execution_error, inner); @@ -644,7 +650,7 @@ namespace System.Net.Http if (remaining == _readLength - _readOffset) { - ThrowInvalidHttpResponse(); + throw new IOException(SR.net_http_invalid_response); } } } @@ -680,7 +686,7 @@ namespace System.Net.Http } // Couldn't find the expect CrLf. - ThrowInvalidHttpResponse(); + throw new IOException(SR.net_http_invalid_response); } private Task FillAsync(CancellationToken cancellationToken)