Skip to content

Commit 32dfbb1

Browse files
committed
Update HttpRequestMessageFactory to correctly set the Content-Length header
1 parent f551727 commit 32dfbb1

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"core": {
3+
"changeLogMessages": [
4+
"Update `HttpRequestMessageFactory` to correctly set the `Content-Length` header for .NET Standard / .NET 8"
5+
],
6+
"backwardIncompatibilitiesToIgnore": [],
7+
"type": "patch",
8+
"updateMinimum": true
9+
}
10+
}

sdk/src/Core/Amazon.Runtime/Pipeline/HttpHandler/_netstandard/HttpRequestMessageFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ public void WriteToRequestBody(HttpContent requestContent, Stream contentStream,
625625
if ((isChunkedUploadWrapperStreamWithLength || isTrailingHeadersWrapperStreamWithLength || isCompressionWrapperStreamWithLength)
626626
|| (chunkedUploadWrapperStream == null && trailingHeadersWrapperStream == null && compressionWrapperStream == null))
627627
{
628-
_request.Content.Headers.ContentLength = contentStream.Length;
628+
_request.Content.Headers.ContentLength = contentStream.Length - contentStream.Position;
629629
}
630630

631631
WriteContentHeaders(contentHeaders);

sdk/test/NetStandard/IntegrationTests/IntegrationTests/S3/PutObjectTests.cs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Text;
53
using System.Threading.Tasks;
64
using Xunit;
75
using Amazon.S3;
8-
9-
using Amazon.DNXCore.IntegrationTests;
106
using Amazon.S3.Model;
117
using System.Net;
128
using System.Threading;
139
using System.IO;
10+
using Amazon.S3.Util;
1411

1512
namespace Amazon.DNXCore.IntegrationTests.S3
1613
{
1714
/// <summary>
1815
/// Summary description for PutObjectTest
1916
/// </summary>
20-
17+
2118
public class PutObjectTest : TestBase<AmazonS3Client>
2219
{
2320
public static readonly long MEG_SIZE = (int)Math.Pow(2, 20);
@@ -34,7 +31,7 @@ public PutObjectTest()
3431
File.WriteAllText(filePath, "This is some sample text.!!");
3532
bucketName = UtilityMethods.CreateBucketAsync(Client, "PutObjectTest", true).Result;
3633
}
37-
34+
3835
protected override void Dispose(bool disposing)
3936
{
4037
UtilityMethods.DeleteBucketWithObjectsAsync(Client, bucketName).Wait();
@@ -43,7 +40,7 @@ protected override void Dispose(bool disposing)
4340
{
4441
File.Delete(filePath);
4542
}
46-
43+
4744
base.Dispose(disposing);
4845
}
4946

@@ -130,8 +127,8 @@ public async Task SimplePathPutObjectTest(bool useChunkEncoding)
130127

131128
Console.WriteLine("S3 generated ETag: {0}", response.ETag);
132129
Assert.True(response.ETag.Length > 0);
133-
}
134-
130+
}
131+
135132
[Theory]
136133
[InlineData(true)]
137134
[InlineData(false)]
@@ -189,6 +186,48 @@ public async Task PutObjectWithoutContentEncoding(bool useChunkEncoding)
189186
Assert.Null(headers.ContentEncoding);
190187
}
191188

189+
/// <summary>
190+
/// Reported in https://github.com/aws/aws-sdk-net/issues/3629
191+
/// </summary>
192+
[Fact]
193+
public async Task TestResetStreamPosition()
194+
{
195+
var memoryStream = new MemoryStream();
196+
long offset;
197+
198+
using (var writer = new StreamWriter(memoryStream, Encoding.UTF8, 1024, leaveOpen: true))
199+
{
200+
writer.AutoFlush = true;
201+
await writer.WriteAsync("Hello");
202+
offset = memoryStream.Position;
203+
await writer.WriteAsync("World");
204+
await writer.FlushAsync();
205+
}
206+
207+
memoryStream.Seek(offset, SeekOrigin.Begin);
208+
209+
var putRequest = new PutObjectRequest
210+
{
211+
CannedACL = S3CannedACL.NoACL,
212+
BucketName = bucketName,
213+
Key = "test-file.txt",
214+
AutoResetStreamPosition = false,
215+
AutoCloseStream = !memoryStream.CanSeek,
216+
InputStream = memoryStream.CanSeek ? memoryStream : AmazonS3Util.MakeStreamSeekable(memoryStream),
217+
UseChunkEncoding = false,
218+
};
219+
220+
var putResponse = await Client.PutObjectAsync(putRequest);
221+
Assert.True(putResponse.HttpStatusCode == HttpStatusCode.OK);
222+
223+
var getResponse = await Client.GetObjectAsync(bucketName, "test-file.txt");
224+
using (var reader = new StreamReader(getResponse.ResponseStream))
225+
{
226+
var content = await reader.ReadToEndAsync();
227+
Assert.Equal("World", content);
228+
}
229+
}
230+
192231
private async Task<HeadersCollection> TestPutAndGet(PutObjectRequest request)
193232
{
194233
await Client.PutObjectAsync(request);

0 commit comments

Comments
 (0)