From d72483d390e866c3ad407621a5b4c646202a9f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marie=20P=C3=ADchov=C3=A1?= <11718369+ManickaP@users.noreply.github.com> Date: Mon, 3 Jun 2024 13:52:32 +0200 Subject: [PATCH] =?UTF-8?q?Revert=20"Content-Length=20header=20not=20alway?= =?UTF-8?q?s=20returned=20when=20enumerating=20HttpConten=E2=80=A6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit d9d0e381f2dcbf5279af66ca406a42fb08cc6528. --- .../src/System/Net/Http/ByteArrayContent.cs | 2 -- .../src/System/Net/Http/ReadOnlyMemoryContent.cs | 5 +---- .../src/System/Net/Http/StreamContent.cs | 10 ---------- .../tests/FunctionalTests/ByteArrayContentTest.cs | 6 ------ .../FunctionalTests/HttpRequestMessageTest.cs | 2 -- .../FunctionalTests/HttpResponseMessageTest.cs | 3 --- .../tests/FunctionalTests/MultipartContentTest.cs | 8 +------- .../MultipartFormDataContentTest.cs | 14 +++++++------- .../tests/FunctionalTests/StreamContentTest.cs | 15 --------------- .../JavaScript/HttpRequestMessageTest.cs | 2 -- 10 files changed, 9 insertions(+), 58 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/ByteArrayContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/ByteArrayContent.cs index 97cc1b66b452fe..d13e851bb47721 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/ByteArrayContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/ByteArrayContent.cs @@ -20,7 +20,6 @@ public ByteArrayContent(byte[] content) _content = content; _count = content.Length; - Headers.ContentLength = _count; } public ByteArrayContent(byte[] content, int offset, int count) @@ -36,7 +35,6 @@ public ByteArrayContent(byte[] content, int offset, int count) _content = content; _offset = offset; _count = count; - Headers.ContentLength = _count; } protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken) => diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs index 3d7b1fa371f353..c4709c86226b44 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs @@ -11,11 +11,8 @@ public sealed class ReadOnlyMemoryContent : HttpContent { private readonly ReadOnlyMemory _content; - public ReadOnlyMemoryContent(ReadOnlyMemory content) - { + public ReadOnlyMemoryContent(ReadOnlyMemory content) => _content = content; - Headers.ContentLength = content.Length; - } protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken) { diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/StreamContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/StreamContent.cs index 262a2cb7862e1f..fe81801690098f 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/StreamContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/StreamContent.cs @@ -23,11 +23,6 @@ public StreamContent(Stream content) // Indicate that we should use default buffer size by setting size to 0. InitializeContent(content, 0); - - if (TryComputeLength(out long contentLength)) - { - Headers.ContentLength = contentLength; - } } public StreamContent(Stream content, int bufferSize) @@ -36,11 +31,6 @@ public StreamContent(Stream content, int bufferSize) ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bufferSize); InitializeContent(content, bufferSize); - - if (TryComputeLength(out long contentLength)) - { - Headers.ContentLength = contentLength; - } } [MemberNotNull(nameof(_content))] diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/ByteArrayContentTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/ByteArrayContentTest.cs index 70995db936a7b7..f8b74e0f86968e 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/ByteArrayContentTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/ByteArrayContentTest.cs @@ -66,9 +66,6 @@ public void ContentLength_UseWholeSourceArray_LengthMatchesArrayLength() var contentData = new byte[10]; var content = new ByteArrayContent(contentData); - Assert.Contains("Content-Length", content.Headers.ToString()); - Assert.Contains(content.Headers, h => h.Key == "Content-Length"); - Assert.Contains(content.Headers.NonValidated, h => h.Key == "Content-Length"); Assert.Equal(contentData.Length, content.Headers.ContentLength); } @@ -78,9 +75,6 @@ public void ContentLength_UsePartialSourceArray_LengthMatchesArrayLength() var contentData = new byte[10]; var content = new ByteArrayContent(contentData, 5, 3); - Assert.Contains("Content-Length", content.Headers.ToString()); - Assert.Contains(content.Headers, h => h.Key == "Content-Length"); - Assert.Contains(content.Headers.NonValidated, h => h.Key == "Content-Length"); Assert.Equal(3, content.Headers.ContentLength); } diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpRequestMessageTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpRequestMessageTest.cs index 0cde6b2060605c..d62566f1dd1b4a 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpRequestMessageTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpRequestMessageTest.cs @@ -210,7 +210,6 @@ public void ToString_DefaultAndNonDefaultInstance_DumpAllFields() Assert.Equal( "Method: PUT, RequestUri: 'http://a.com/', Version: 1.0, Content: " + typeof(StringContent).ToString() + ", Headers:" + Environment.NewLine + $"{{{Environment.NewLine}" + - " Content-Length: 7" + Environment.NewLine + " Content-Type: text/plain; charset=utf-8" + Environment.NewLine + "}", rm.ToString()); @@ -229,7 +228,6 @@ public void ToString_DefaultAndNonDefaultInstance_DumpAllFields() " Accept: text/xml; q=0.1" + Environment.NewLine + " Accept-Language: en-US,en;q=0.5" + Environment.NewLine + " Custom-Request-Header: value1" + Environment.NewLine + - " Content-Length: 7" + Environment.NewLine + " Content-Type: text/plain; charset=utf-8" + Environment.NewLine + " Custom-Content-Header: value2" + Environment.NewLine + "}", rm.ToString()); diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpResponseMessageTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpResponseMessageTest.cs index f1e865c661d9d7..3146cdafda7a1c 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpResponseMessageTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpResponseMessageTest.cs @@ -312,7 +312,6 @@ public void ToString_DefaultAndNonDefaultInstance_DumpAllFields() Assert.Equal( "StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.0, Content: " + typeof(StringContent).ToString() + ", Headers:" + Environment.NewLine + "{" + Environment.NewLine + - " Content-Length: 7" + Environment.NewLine + " Content-Type: text/plain; charset=utf-8" + Environment.NewLine + "}", rm.ToString()); @@ -327,7 +326,6 @@ public void ToString_DefaultAndNonDefaultInstance_DumpAllFields() " Accept-Ranges: bytes" + Environment.NewLine + " Accept-Ranges: pages" + Environment.NewLine + " Custom-Response-Header: value1" + Environment.NewLine + - " Content-Length: 7" + Environment.NewLine + " Content-Type: text/plain; charset=utf-8" + Environment.NewLine + " Custom-Content-Header: value2" + Environment.NewLine + "}", rm.ToString()); @@ -341,7 +339,6 @@ public void ToString_DefaultAndNonDefaultInstance_DumpAllFields() " Accept-Ranges: bytes" + Environment.NewLine + " Accept-Ranges: pages" + Environment.NewLine + " Custom-Response-Header: value1" + Environment.NewLine + - " Content-Length: 7" + Environment.NewLine + " Content-Type: text/plain; charset=utf-8" + Environment.NewLine + " Custom-Content-Header: value2" + Environment.NewLine + "}, Trailing Headers:" + Environment.NewLine + diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs index 188fd1bab69d47..7df54b8ddd9550 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs @@ -160,7 +160,6 @@ public async Task ReadAsStringAsync_OneSubContentWithHeaders_MatchesExpected(Mul Assert.Equal( "--theBoundary\r\n" + - "Content-Length: 26\r\n" + "someHeaderName: andSomeHeaderValue\r\n" + "someOtherHeaderName: withNotOne, ButTwoValues\r\n" + "oneMoreHeader: withNotOne, AndNotTwo, butThreeValues\r\n" + @@ -183,11 +182,9 @@ public async Task ReadAsStringAsync_TwoSubContents_MatchesExpected(MultipartCont Assert.Equal( "--theBoundary\r\n" + - "Content-Length: 26\r\n" + "\r\n" + "This is a ByteArrayContent\r\n" + "--theBoundary\r\n" + - "Content-Length: 23\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "This is a StringContent\r\n" + @@ -480,31 +477,28 @@ public async Task ReadAsStreamAsync_CustomEncodingSelector_CustomEncodingIsUsed( byte[] expected = Concat( "--fooBoundary\r\n"u8.ToArray(), - "Content-Length: 4\r\n"u8.ToArray(), "Content-Type: text/plain; charset=utf-8\r\n"u8.ToArray(), "latin1: "u8.ToArray(), Encoding.Latin1.GetBytes("\U0001F600"), "\r\n\r\n"u8.ToArray(), "bar1"u8.ToArray(), "\r\n--fooBoundary\r\n"u8.ToArray(), - "Content-Length: 4\r\n"u8.ToArray(), "utf8: "u8.ToArray(), "\U0001F600"u8.ToArray(), "\r\n\r\n"u8.ToArray(), "bar2"u8.ToArray(), "\r\n--fooBoundary\r\n"u8.ToArray(), - "Content-Length: 4\r\n"u8.ToArray(), "ascii: "u8.ToArray(), Encoding.ASCII.GetBytes("\U0001F600"), "\r\n\r\n"u8.ToArray(), "bar3"u8.ToArray(), "\r\n--fooBoundary\r\n"u8.ToArray(), - "Content-Length: 4\r\n"u8.ToArray(), "default: "u8.ToArray(), Encoding.Latin1.GetBytes("\U0001F600"), "\r\n\r\n"u8.ToArray(), "bar4"u8.ToArray(), "\r\n--fooBoundary--\r\n"u8.ToArray()); + Assert.Equal(expected, ms.ToArray()); static byte[] Concat(params byte[][] arrays) => arrays.SelectMany(b => b).ToArray(); diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartFormDataContentTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartFormDataContentTest.cs index c906f41c49900a..90a75c3b7078b2 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartFormDataContentTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartFormDataContentTest.cs @@ -92,7 +92,7 @@ public async Task Serialize_StringContent_Success() string result = new StreamReader(output).ReadToEnd(); Assert.Equal( - "--test_boundary\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n" + "--test_boundary\r\nContent-Type: text/plain; charset=utf-8\r\n" + "Content-Disposition: form-data\r\n\r\nHello World\r\n--test_boundary--\r\n", result); } @@ -110,7 +110,7 @@ public async Task Serialize_NamedStringContent_Success() string result = new StreamReader(output).ReadToEnd(); Assert.Equal( - "--test_boundary\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n" + "--test_boundary\r\nContent-Type: text/plain; charset=utf-8\r\n" + "Content-Disposition: form-data; name=test_name\r\n\r\nHello World\r\n--test_boundary--\r\n", result); } @@ -128,7 +128,7 @@ public async Task Serialize_FileNameStringContent_Success() string result = new StreamReader(output).ReadToEnd(); Assert.Equal( - "--test_boundary\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n" + "--test_boundary\r\nContent-Type: text/plain; charset=utf-8\r\n" + "Content-Disposition: form-data; name=test_name; " + "filename=test_file_name; filename*=utf-8\'\'test_file_name\r\n\r\n" + "Hello World\r\n--test_boundary--\r\n", @@ -148,7 +148,7 @@ public async Task Serialize_QuotedName_Success() string result = new StreamReader(output).ReadToEnd(); Assert.Equal( - "--test_boundary\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n" + "--test_boundary\r\nContent-Type: text/plain; charset=utf-8\r\n" + "Content-Disposition: form-data; name=\"test name\"\r\n\r\nHello World\r\n--test_boundary--\r\n", result); } @@ -166,7 +166,7 @@ public async Task Serialize_InvalidName_Encoded() string result = new StreamReader(output).ReadToEnd(); Assert.Equal( - "--test_boundary\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n" + "--test_boundary\r\nContent-Type: text/plain; charset=utf-8\r\n" + "Content-Disposition: form-data; name=\"=?utf-8?B?dGVzdOOCrw0KIG5hbcOp?=\"" + "\r\n\r\nHello World\r\n--test_boundary--\r\n", result); @@ -185,7 +185,7 @@ public async Task Serialize_InvalidQuotedName_Encoded() string result = new StreamReader(output).ReadToEnd(); Assert.Equal( - "--test_boundary\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n" + "--test_boundary\r\nContent-Type: text/plain; charset=utf-8\r\n" + "Content-Disposition: form-data; name=\"=?utf-8?B?dGVzdOOCrw0KIG5hbcOp?=\"" + "\r\n\r\nHello World\r\n--test_boundary--\r\n", result); @@ -204,7 +204,7 @@ public async Task Serialize_InvalidNamedFileName_Encoded() string result = new StreamReader(output).ReadToEnd(); Assert.Equal( - "--test_boundary\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n" + "--test_boundary\r\nContent-Type: text/plain; charset=utf-8\r\n" + "Content-Disposition: form-data; name=\"=?utf-8?B?dGVzdOOCrw0KIG5hbcOp?=\";" + " filename=\"=?utf-8?B?ZmlsZeOCrw0KIG5hbcOp?=\"; filename*=utf-8\'\'file%E3%82%AF%0D%0A%20nam%C3%A9" + "\r\n\r\nHello World\r\n--test_boundary--\r\n", diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/StreamContentTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/StreamContentTest.cs index d1761b021fdded..9d432aea93eef1 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/StreamContentTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/StreamContentTest.cs @@ -32,21 +32,6 @@ public void ContentLength_SetStreamSupportingSeeking_StreamLengthMatchesHeaderVa var source = new MockStream(new byte[10], true, true); // Supports seeking. var content = new StreamContent(source); - Assert.Contains("Content-Length", content.Headers.ToString()); - Assert.Contains(content.Headers, h => h.Key == "Content-Length"); - Assert.Contains(content.Headers.NonValidated, h => h.Key == "Content-Length"); - Assert.Equal(source.Length, content.Headers.ContentLength); - } - - [Fact] - public void ContentLength_SetStreamSupportingSeekingWithBuffering_StreamLengthMatchesHeaderValue() - { - var source = new MockStream(new byte[10], true, true); // Supports seeking. - var content = new StreamContent(source, 5); - - Assert.Contains("Content-Length", content.Headers.ToString()); - Assert.Contains(content.Headers, h => h.Key == "Content-Length"); - Assert.Contains(content.Headers.NonValidated, h => h.Key == "Content-Length"); Assert.Equal(source.Length, content.Headers.ContentLength); } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/HttpRequestMessageTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/HttpRequestMessageTest.cs index 1776de1565a7e9..815dfeb0dcd2df 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/HttpRequestMessageTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/HttpRequestMessageTest.cs @@ -336,7 +336,6 @@ public void ToString_NonDefaultInstanceWithNoCustomHeaders_DumpAllFields(string Assert.Equal( $"Method: PUT, RequestUri: '{uriData}', Version: 1.0, Content: " + typeof(StringContent).ToString() + ", Headers:" + Environment.NewLine + $"{{{Environment.NewLine}" + - " Content-Length: 7" + Environment.NewLine + " Content-Type: text/plain; charset=utf-8" + Environment.NewLine + "}", rm.ToString()); } @@ -362,7 +361,6 @@ public void ToString_NonDefaultInstanceWithCustomHeaders_DumpAllFields(string ur " Accept: text/plain; q=0.2" + Environment.NewLine + " Accept: text/xml; q=0.1" + Environment.NewLine + " Custom-Request-Header: value1" + Environment.NewLine + - " Content-Length: 7" + Environment.NewLine + " Content-Type: text/plain; charset=utf-8" + Environment.NewLine + " Custom-Content-Header: value2" + Environment.NewLine + "}", rm.ToString());