-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Add Request Decompression middleware #40279
Conversation
src/Middleware/RequestDecompression/src/DeflateDecompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/Microsoft.AspNetCore.RequestDecompression.csproj
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/PublicAPI.Unshipped.txt
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/DecompressionProviderCollection.cs
Outdated
Show resolved
Hide resolved
I wish my next middleware PR is as polished as this one. |
src/Middleware/RequestDecompression/sample/Properties/launchsettings.json
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionBody.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/BrotliDecompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/DecompressionProviderFactory.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/DeflateDecompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/GzipDecompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/IRequestDecompressionProvider.cs
Show resolved
Hide resolved
Random thought: what if we designed the feature like so: https://gist.github.com/pranavkm/8932a862a701061f43b6e64cc0b18931. It removes some of the newly introduced types at the expense of slightly more code in each of the compression providers. |
src/Middleware/RequestDecompression/src/RequestDecompressionBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionServiceExtensions.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionBody.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionBody.cs
Outdated
Show resolved
Hide resolved
src/Middleware/RequestDecompression/src/RequestDecompressionBody.cs
Outdated
Show resolved
Hide resolved
Thanks @david-acker! I should have paid more attention to the actual code on my last review. |
@halter73 No worries! Thanks for the help with the |
Can we make sure we have basic benchmarks setup so we can measure the overhead here? |
I've added some benchmarks which cover both compressed and uncompressed requests as well as the scenarios where the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole thing looks really clean, nice job.
Congrats @david-acker! Happy to see this merged. |
Co-authored-by: Pranav K <prkrishn@hotmail.com> Co-authored-by: Sébastien Ros <sebastienros@gmail.com>
@david-acker, this change will be considered for inclusion in the blog post for the release it'll ship in. Nice work! Please ensure that the original comment in this thread contains a clear explanation of what the change does, why it's important (what problem does it solve?), and, if relevant, include things like code samples and/or performance numbers. This content may not be exactly what goes into the blog post, but it will help the team putting together the announcement. Thanks! |
Adds middleware for decompressing HTTP requests.
Problem
In the past, the developer of the server would need to manually handle requests with compressed content by checking if a request was compressed and then decompressing the request body stream as appropriate.
Solution
This middleware uses the
Content-Encoding
header automatically identify and decompress requests with compressed content, so that the developer of the server does not need to handle this themselves.The request decompression middleware is added using the
UseRequestDecompression
extension method forIApplicationBuilder
and theAddRequestDecompression
extension method forIServiceCollection
.Supported Content Encodings
Support for Brotli (
br
), Deflate (deflate
), and GZip (gzip
) are included out of the box.Support for other content encodings can be added by registering a custom decompression provider class, which implements the
IDecompressionProvider
interface, along with aContent-Encoding
header value inRequestDecompressionOptions
.Request Size Limits
The request body size limit enforced by the server (
MaxRequestBodySize
) and/or the endpoint (RequestSizeLimitAttribute
) will also be enforced on the decompressed request body.If the amount of data read from the decompressed request body exceeds the size limit, an
InvalidOperationException
is thrown to prevent any more data from being read. This protects against the threat of uncontrolled resource consumption from zip bombsEdge Cases
Content-Encoding
or has multiple encodings, an appropriate message will be logged, and the request will be passed on without being decompressed.Fixes #40080