Просмотр исходного кода

Fix MultipartReaderStream synchronous read when using buffer offset (#59360)

Brennan 1 год назад
Родитель
Сommit
620d45d2c4

+ 1 - 1
src/Http/WebUtilities/src/MultipartReaderStream.cs

@@ -174,7 +174,7 @@ internal sealed class MultipartReaderStream : Stream
             if (index != 0)
             {
                 // Sync, it's already buffered
-                var slice = buffer.AsSpan(0, Math.Min(buffer.Length, index));
+                var slice = buffer.AsSpan(offset, Math.Min(count, index));
 
                 var readAmount = _innerStream.Read(slice);
                 return UpdatePosition(readAmount);

+ 24 - 0
src/Http/WebUtilities/test/MultipartReaderTests.cs

@@ -389,4 +389,28 @@ public class MultipartReaderTests
         var section = await reader.ReadNextSectionAsync();
         Assert.NotNull(section);
     }
+
+    [Fact]
+    public async Task SyncReadWithOffsetWorks()
+    {
+        var stream = MakeStream(OnePartBody);
+        var reader = new MultipartReader(Boundary, stream);
+        var buffer = new byte[5];
+
+        var section = await reader.ReadNextSectionAsync();
+        Assert.NotNull(section);
+        Assert.Single(section.Headers);
+        Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
+
+        var read = section.Body.Read(buffer, 2, buffer.Length - 2);
+        Assert.Equal("\0\0tex", GetString(buffer, read + 2));
+
+        read = section.Body.Read(buffer, 1, buffer.Length - 1);
+        Assert.Equal("\0t de", GetString(buffer, read + 1));
+
+        read = section.Body.Read(buffer, 0, buffer.Length);
+        Assert.Equal("fault", GetString(buffer, read));
+
+        Assert.Null(await reader.ReadNextSectionAsync());
+    }
 }