Skip to content

Issue #60416 - Correction of BufferedReadStream 's Position #60529

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

Merged
merged 5 commits into from
Feb 21, 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
4 changes: 2 additions & 2 deletions src/Http/WebUtilities/src/BufferedReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public override long Position
if (innerOffset <= _bufferCount)
{
// Yes, just skip some of the buffered data
_bufferOffset += innerOffset;
_bufferCount -= innerOffset;
_bufferOffset += _bufferCount - innerOffset;
_bufferCount = innerOffset;
}
else
{
Expand Down
38 changes: 37 additions & 1 deletion src/Http/WebUtilities/test/MultipartReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public class MultipartReaderTests
"<!DOCTYPE html><title>Content of a.html.</title>\r\n" +
"\r\n" +
"--9051914041544843365972754266--\r\n";

private const string TwoPartBodyIncompleteBuffer =
"--9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
Expand Down Expand Up @@ -315,6 +314,43 @@ public void MultipartReader_BufferSizeMustBeLargerThanBoundary_Throws()
});
}

[Fact]
public async Task MultipartReader_ReadMultipartBodyWithFilesForDeferredCopy_Success()
{
var stream = MakeStream(ThreePartBody);
var reader = new MultipartReader(Boundary, stream);

await reader.ReadNextSectionAsync(); // skip text field section

var section = await reader.ReadNextSectionAsync();
Assert.NotNull(section);
Assert.Equal(2, section.Headers.Count);
Assert.Equal("form-data; name=\"file1\"; filename=\"a.txt\"", section.Headers["Content-Disposition"][0]);
Assert.Equal("text/plain", section.Headers["Content-Type"][0]);
var stream1 = section.Body;

section = await reader.ReadNextSectionAsync();
Assert.NotNull(section);
Assert.Equal(2, section.Headers.Count);
Assert.Equal("form-data; name=\"file2\"; filename=\"a.html\"", section.Headers["Content-Disposition"][0]);
Assert.Equal("text/html", section.Headers["Content-Type"][0]);
var stream2 = section.Body;

Assert.Null(await reader.ReadNextSectionAsync());

Assert.True(stream1.CanSeek);
Assert.Equal(0, stream1.Seek(0, SeekOrigin.Begin));
var buffer = new MemoryStream();
await stream1.CopyToAsync(buffer);
Assert.Equal("Content of a.txt.\r\n", Encoding.ASCII.GetString(buffer.ToArray()));

Assert.True(stream2.CanSeek);
Assert.Equal(0, stream2.Seek(0, SeekOrigin.Begin));
buffer = new MemoryStream();
await stream2.CopyToAsync(buffer);
Assert.Equal("<!DOCTYPE html><title>Content of a.html.</title>\r\n", Encoding.ASCII.GetString(buffer.ToArray()));
}

[Fact]
public async Task MultipartReader_TwoPartBodyIncompleteBuffer_TwoSectionsReadSuccessfullyThirdSectionThrows()
{
Expand Down
Loading