Created
February 19, 2022 03:01
-
-
Save pranavkm/8932a862a701061f43b6e64cc0b18931 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IRequestDecompressionProvider | |
{ | |
bool TryDecompressRequest(HttpContext httpContext); | |
} | |
public sealed class GzipDecompressionProvider : IRequestDecompressionProvider | |
{ | |
bool TryDecompressRequest(HttpContext context) | |
{ | |
// e.g. Content-Encoding: br, deflate, gzip | |
var encodings = context.Request.Headers.ContentEncoding; | |
if (encodings.Length == 1 && encodings[0] == "gz") | |
{ | |
stream = SizeLimitedStream(httpContext.GetRequiredFeature<IHttpSizeLimitFeature>(), GzipStream(context.Request)); | |
context.Body = stream; | |
context.Request.Headers.ContentEncoding = null; | |
return true; | |
} | |
stream = null; | |
return false; | |
} | |
} | |
public class RequestDecompressionOptions | |
{ | |
IList<IRequestDecompressionProvider> DecompressionProviders { get; } = new List<IRequestDecompressionProvider> | |
{ | |
new GzipDecompressionProvider(), | |
new BrotliCompressionProvider(), | |
new DeflateCompressionProvider(), | |
}; | |
} | |
class RequestDecompressionMiddleware | |
{ | |
Task InvokeAsync(HttpContext context) | |
{ | |
foreach (var provider in _providers) | |
{ | |
if (TryDecompressRequest(context)) | |
{ | |
break; | |
} | |
} | |
return _next(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment