Skip to content

Request.Body can only be read once when AddRequestDecompression is used #4353

@jamescrosswell

Description

@jamescrosswell

In #4315 we may have introduced a problem when/if other middleware wants to read the request body:

if (!request.Body.CanSeek)
{
// When RequestDecompression is enabled, the RequestDecompressionMiddleware will store a SizeLimitedStream
// in the request body after decompression. Seek operations throw an exception, but we can still read the stream
return DoExtractPayLoad(request);
}

The new middleware that we vendored in is getting an IRequestDecompressionProvider injected from DI via the constructor. That gets registered by the AddRequestDecompression service extension. The stream that this returns doesn't typically support seek (decompression streams are typically forward only). For example GZipStream.CanSeek returns false in all cases.

A naive solution would be to read the decompression stream into a memory stream and store the results back on the RequestBody as a MemoryStream (which does support seek).

In theory, this shouldn't be necessary. We call EnableBuffering:

context.Request.EnableBuffering();

Looking at the implementation for that extension this should handle all of this automatically for us.

So why is EnableBuffering not doing what we expect it to?

Metadata

Metadata

Labels

.NETPull requests that update .net codeBugSomething isn't working

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions