-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support request and response compression (#27)
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.IO.Compression; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace FhirPseudonymizer | ||
{ | ||
public class RequestCompression | ||
{ | ||
private readonly RequestDelegate next; | ||
private const string ContentEncodingHeader = "Content-Encoding"; | ||
private const string ContentEncodingGzip = "gzip"; | ||
private const string ContentEncodingBrotli = "br"; | ||
private const string ContentEncodingDeflate = "deflate"; | ||
|
||
public RequestCompression(RequestDelegate next) | ||
{ | ||
this.next = next ?? throw new ArgumentNullException(nameof(next)); | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
if (context.Request.Headers.Keys.Contains(ContentEncodingHeader)) | ||
{ | ||
switch (context.Request.Headers[ContentEncodingHeader]) | ||
{ | ||
case ContentEncodingGzip: | ||
context.Request.Body = new GZipStream(context.Request.Body, CompressionMode.Decompress, true); | ||
break; | ||
case ContentEncodingBrotli: | ||
context.Request.Body = new BrotliStream(context.Request.Body, CompressionMode.Decompress, true); | ||
break; | ||
case ContentEncodingDeflate: | ||
context.Request.Body = new DeflateStream(context.Request.Body, CompressionMode.Decompress, true); | ||
break; | ||
} | ||
} | ||
|
||
await next(context); | ||
} | ||
} | ||
} |
This file contains 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