Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HttpRequestMessageFactory to correctly set the Content-Length header #3631

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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