Skip to content

Commit 1ae68f1

Browse files
[release/7.0] Fix RequestBody logging when zero byte read is used (#47811)
* Fix RequestBody logging when zero byte read is used * consistency --------- Co-authored-by: Brennan <[email protected]>
1 parent b1a5a18 commit 1ae68f1

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/Middleware/HttpLogging/src/RequestBufferingStream.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public override async ValueTask<int> ReadAsync(Memory<byte> destination, Cancell
2727
{
2828
var res = await _innerStream.ReadAsync(destination, cancellationToken);
2929

30+
// Zero-byte reads (where the passed in buffer has 0 length) can occur when using PipeReader, we don't want to accidentally complete the RequestBody logging in this case
31+
if (destination.IsEmpty)
32+
{
33+
return res;
34+
}
35+
3036
WriteToBuffer(destination.Slice(0, res).Span);
3137

3238
return res;
@@ -36,6 +42,12 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
3642
{
3743
var res = await _innerStream.ReadAsync(buffer.AsMemory(offset, count), cancellationToken);
3844

45+
// Zero-byte reads (where the passed in buffer has 0 length) can occur when using PipeReader, we don't want to accidentally complete the RequestBody logging in this case
46+
if (count == 0)
47+
{
48+
return res;
49+
}
50+
3951
WriteToBuffer(buffer.AsSpan(offset, res));
4052

4153
return res;
@@ -45,6 +57,12 @@ public override int Read(byte[] buffer, int offset, int count)
4557
{
4658
var res = _innerStream.Read(buffer, offset, count);
4759

60+
// Zero-byte reads (where the passed in buffer has 0 length) can occur when using PipeReader, we don't want to accidentally complete the RequestBody logging in this case
61+
if (count == 0)
62+
{
63+
return res;
64+
}
65+
4866
WriteToBuffer(buffer.AsSpan(offset, res));
4967

5068
return res;

src/Middleware/HttpLogging/test/HttpLoggingMiddlewareTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,33 @@ public async Task PartialReadBodyStillLogs()
434434
Assert.Contains(TestSink.Writes, w => w.Message.Equals("RequestBody: " + expected));
435435
}
436436

437+
[Fact]
438+
public async Task ZeroByteReadStillLogsRequestBody()
439+
{
440+
var input = string.Concat(new string('a', 60000), new string('b', 3000));
441+
var options = CreateOptionsAccessor();
442+
options.CurrentValue.LoggingFields = HttpLoggingFields.RequestBody;
443+
444+
var middleware = new HttpLoggingMiddleware(
445+
async c =>
446+
{
447+
var arr = new byte[4096];
448+
_ = await c.Request.Body.ReadAsync(new byte[0]);
449+
var res = await c.Request.Body.ReadAsync(arr);
450+
},
451+
options,
452+
LoggerFactory.CreateLogger<HttpLoggingMiddleware>());
453+
454+
var httpContext = new DefaultHttpContext();
455+
httpContext.Request.ContentType = "text/plain";
456+
httpContext.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(input));
457+
458+
await middleware.Invoke(httpContext);
459+
var expected = input.Substring(0, 4096);
460+
461+
Assert.Contains(TestSink.Writes, w => w.Message.Equals("RequestBody: " + expected));
462+
}
463+
437464
[Theory]
438465
[InlineData("text/plain")]
439466
[InlineData("text/html")]

0 commit comments

Comments
 (0)