remove CL if TE present
authorGeoff Kizer <geoffrek>
Mon, 19 Feb 2018 19:32:24 +0000 (11:32 -0800)
committerGeoff Kizer <geoffrek>
Mon, 19 Feb 2018 19:32:24 +0000 (11:32 -0800)
Commit migrated from https://github.com/dotnet/corefx/commit/f9c486d14ec01e98b9e73b4eb23f729783678e3c

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs

index 681c513..2ec5027 100644 (file)
@@ -278,16 +278,19 @@ namespace System.Net.Http
             }
 
             // Add headers to define content transfer, if not present
-            bool transferEncodingChunkedSet = request.HasHeaders && request.Headers.TransferEncodingChunked.GetValueOrDefault();
-            if (request.Content == null)
+            if (request.HasHeaders && request.Headers.TransferEncodingChunked.GetValueOrDefault())
             {
-                if (transferEncodingChunkedSet)
+                if (request.Content == null)
                 {
                     return new HttpRequestException(SR.net_http_client_execution_error,
                         new InvalidOperationException(SR.net_http_chunked_not_allowed_with_empty_content));
                 }
+
+                // Since the user explicitly set TransferEncodingChunked to true, we need to remove
+                // the Content-Length header if present, as sending both is invalid.
+                request.Content.Headers.ContentLength = null;
             }
-            else if (!transferEncodingChunkedSet && request.Content.Headers.ContentLength == null)
+            else if (request.Content != null && request.Content.Headers.ContentLength == null)
             {
                 // We have content, but neither Transfer-Encoding nor Content-Length is set.
                 request.Headers.TransferEncodingChunked = true;