Skip to content

Commit 1d1d5a7

Browse files
Fix MultipartReaderStream synchronous read when using buffer offset (#59422)
Co-authored-by: Brennan <[email protected]>
1 parent c7de78c commit 1d1d5a7

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Http/WebUtilities/src/MultipartReaderStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public override int Read(byte[] buffer, int offset, int count)
174174
if (index != 0)
175175
{
176176
// Sync, it's already buffered
177-
var slice = buffer.AsSpan(0, Math.Min(buffer.Length, index));
177+
var slice = buffer.AsSpan(offset, Math.Min(count, index));
178178

179179
var readAmount = _innerStream.Read(slice);
180180
return UpdatePosition(readAmount);

src/Http/WebUtilities/test/MultipartReaderTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,28 @@ public async Task MultipartReader_StripQuotesFromBoundary()
389389
var section = await reader.ReadNextSectionAsync();
390390
Assert.NotNull(section);
391391
}
392+
393+
[Fact]
394+
public async Task SyncReadWithOffsetWorks()
395+
{
396+
var stream = MakeStream(OnePartBody);
397+
var reader = new MultipartReader(Boundary, stream);
398+
var buffer = new byte[5];
399+
400+
var section = await reader.ReadNextSectionAsync();
401+
Assert.NotNull(section);
402+
Assert.Single(section.Headers);
403+
Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
404+
405+
var read = section.Body.Read(buffer, 2, buffer.Length - 2);
406+
Assert.Equal("\0\0tex", GetString(buffer, read + 2));
407+
408+
read = section.Body.Read(buffer, 1, buffer.Length - 1);
409+
Assert.Equal("\0t de", GetString(buffer, read + 1));
410+
411+
read = section.Body.Read(buffer, 0, buffer.Length);
412+
Assert.Equal("fault", GetString(buffer, read));
413+
414+
Assert.Null(await reader.ReadNextSectionAsync());
415+
}
392416
}

0 commit comments

Comments
 (0)