Skip to content

Commit

Permalink
feat: support request and response compression (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
chgl authored Aug 21, 2021
1 parent e856e13 commit 6cc7049
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/FhirPseudonymizer/CompressionMiddleware.cs
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);
}
}
}
12 changes: 12 additions & 0 deletions src/FhirPseudonymizer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
Expand Down Expand Up @@ -38,6 +39,14 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);
services.AddResponseCompression(options =>
{
options.MimeTypes =
ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/fhir+json" });
});

var apiKey = Configuration.GetValue<string>("ApiKey");
services.AddAuthentication(ApiKeyDefaults.AuthenticationScheme)
.AddApiKeyInHeaderOrQueryParams(options =>
Expand Down Expand Up @@ -189,6 +198,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseDeveloperExceptionPage();
}

app.UseMiddleware<RequestCompression>();
app.UseResponseCompression();

app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v2/swagger.json", "FhirPseudonymizer v2"));

Expand Down

0 comments on commit 6cc7049

Please sign in to comment.