Skip to content

Commit

Permalink
Update HttpRequestMessageFactory to correctly set the Content-Length …
Browse files Browse the repository at this point in the history
…header
  • Loading branch information
dscpinheiro committed Feb 3, 2025
1 parent f551727 commit 32dfbb1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
10 changes: 10 additions & 0 deletions generator/.DevConfigs/fd7bf4a4-a773-4855-9bfd-7177595cb738.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"core": {
"changeLogMessages": [
"Update `HttpRequestMessageFactory` to correctly set the `Content-Length` header for .NET Standard / .NET 8"
],
"backwardIncompatibilitiesToIgnore": [],
"type": "patch",
"updateMinimum": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public void WriteToRequestBody(HttpContent requestContent, Stream contentStream,
if ((isChunkedUploadWrapperStreamWithLength || isTrailingHeadersWrapperStreamWithLength || isCompressionWrapperStreamWithLength)
|| (chunkedUploadWrapperStream == null && trailingHeadersWrapperStream == null && compressionWrapperStream == null))
{
_request.Content.Headers.ContentLength = contentStream.Length;
_request.Content.Headers.ContentLength = contentStream.Length - contentStream.Position;
}

WriteContentHeaders(contentHeaders);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Amazon.S3;

using Amazon.DNXCore.IntegrationTests;
using Amazon.S3.Model;
using System.Net;
using System.Threading;
using System.IO;
using Amazon.S3.Util;

namespace Amazon.DNXCore.IntegrationTests.S3
{
/// <summary>
/// Summary description for PutObjectTest
/// </summary>

public class PutObjectTest : TestBase<AmazonS3Client>
{
public static readonly long MEG_SIZE = (int)Math.Pow(2, 20);
Expand All @@ -34,7 +31,7 @@ public PutObjectTest()
File.WriteAllText(filePath, "This is some sample text.!!");
bucketName = UtilityMethods.CreateBucketAsync(Client, "PutObjectTest", true).Result;
}

protected override void Dispose(bool disposing)
{
UtilityMethods.DeleteBucketWithObjectsAsync(Client, bucketName).Wait();
Expand All @@ -43,7 +40,7 @@ protected override void Dispose(bool disposing)
{
File.Delete(filePath);
}

base.Dispose(disposing);
}

Expand Down Expand Up @@ -130,8 +127,8 @@ public async Task SimplePathPutObjectTest(bool useChunkEncoding)

Console.WriteLine("S3 generated ETag: {0}", response.ETag);
Assert.True(response.ETag.Length > 0);
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -189,6 +186,48 @@ public async Task PutObjectWithoutContentEncoding(bool useChunkEncoding)
Assert.Null(headers.ContentEncoding);
}

/// <summary>
/// Reported in https://github.com/aws/aws-sdk-net/issues/3629
/// </summary>
[Fact]
public async Task TestResetStreamPosition()
{
var memoryStream = new MemoryStream();
long offset;

using (var writer = new StreamWriter(memoryStream, Encoding.UTF8, 1024, leaveOpen: true))
{
writer.AutoFlush = true;
await writer.WriteAsync("Hello");
offset = memoryStream.Position;
await writer.WriteAsync("World");
await writer.FlushAsync();
}

memoryStream.Seek(offset, SeekOrigin.Begin);

var putRequest = new PutObjectRequest
{
CannedACL = S3CannedACL.NoACL,
BucketName = bucketName,
Key = "test-file.txt",
AutoResetStreamPosition = false,
AutoCloseStream = !memoryStream.CanSeek,
InputStream = memoryStream.CanSeek ? memoryStream : AmazonS3Util.MakeStreamSeekable(memoryStream),
UseChunkEncoding = false,
};

var putResponse = await Client.PutObjectAsync(putRequest);
Assert.True(putResponse.HttpStatusCode == HttpStatusCode.OK);

var getResponse = await Client.GetObjectAsync(bucketName, "test-file.txt");
using (var reader = new StreamReader(getResponse.ResponseStream))
{
var content = await reader.ReadToEndAsync();
Assert.Equal("World", content);
}
}

private async Task<HeadersCollection> TestPutAndGet(PutObjectRequest request)
{
await Client.PutObjectAsync(request);
Expand Down

0 comments on commit 32dfbb1

Please sign in to comment.