Skip to content
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

Merged
merged 48 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
77923d6
Add Request Decompression middleware
david-acker Feb 17, 2022
d888b86
Fix API baselines
david-acker Feb 17, 2022
ca2b675
Minor fixes
david-acker Feb 18, 2022
d3d074a
Eagerly resolve decompression provider and stream
david-acker Feb 18, 2022
4a4e902
Pass requests to endpoint instead of returning 415
david-acker Feb 18, 2022
8ad1352
Revert request body to original value
david-acker Feb 18, 2022
f8d2604
Seal decompression providers
david-acker Feb 18, 2022
12c4519
Use constructor injection for logger
david-acker Feb 19, 2022
e000add
Remove unused property
david-acker Feb 19, 2022
e2a9963
Seal more classes
david-acker Feb 19, 2022
74622dd
Only create RequestDecompressionBody if a decompresison provider exists
david-acker Feb 19, 2022
b4ddfd3
Remove Content-Encoding header if decompressed
david-acker Feb 19, 2022
1d43fef
Fix extensions namespaces
david-acker Feb 19, 2022
fa98756
Remove RequestDecompressionBody
david-acker Feb 19, 2022
e055698
Fix class name
david-acker Feb 19, 2022
03e31c1
Directly instantiate default providers
david-acker Feb 19, 2022
dca2b17
Replace Single with cast
david-acker Feb 19, 2022
202cd2d
Add tests
david-acker Feb 20, 2022
087cf21
Store providers in dictionary
david-acker Feb 20, 2022
8857a63
Fix API baselines
david-acker Feb 20, 2022
c453739
Add MaxRequestBodySize to RequestDecompressionOptions
david-acker Feb 21, 2022
2d674b1
Clean up
david-acker Feb 22, 2022
76fe411
Add, clean up tests
david-acker Feb 22, 2022
7b297ca
Add IRequestSizeLimitMetadata
david-acker Feb 28, 2022
ef9f9cc
Use updated middleware design
david-acker Feb 28, 2022
be883d9
Merge branch 'main' into request-decompression
david-acker Feb 28, 2022
d0e3ea5
Address PR feedback
david-acker Mar 1, 2022
1302dac
Address PR feedback
david-acker Mar 2, 2022
7acf307
Update DefaultRequestDecompressionProvider.cs
pranavkm Mar 2, 2022
f6304d0
Update DefaultRequestDecompressionProvider.cs
pranavkm Mar 2, 2022
71c3cb4
Update RequestDecompressionMiddleware.cs
pranavkm Mar 2, 2022
c96647a
Merge branch 'main' into request-decompression
pranavkm Mar 2, 2022
245ac78
Update RequestDecompressionMiddleware.cs
pranavkm Mar 2, 2022
9a2268e
Use default implementations for BeginRead, EndRead
david-acker Mar 3, 2022
e5376e5
Merge branch 'main' into request-decompression
sebastienros Apr 5, 2022
3ffb5f9
Merge branch 'main' into request-decompression
sebastienros May 9, 2022
38eb172
Make MaxRequestBodySize nullable
david-acker May 16, 2022
ea3a334
Implement IRequestSizeLimitMetadata
david-acker May 16, 2022
c293ade
Add security concern to remarks
david-acker May 16, 2022
7beceb9
Add test for invalid compressed data
david-acker May 16, 2022
cd32124
Set IHttpMaxRequestBodySizeFeature using IRequestSizeLimitMetadata
david-acker May 17, 2022
b33b594
Merge branch 'main' into request-decompression
david-acker May 19, 2022
519310c
Add benchmarks
david-acker May 20, 2022
e31ef58
Remove unneeded project references
david-acker May 20, 2022
18632ef
Pass all write operations to inner stream
david-acker May 20, 2022
3f39a4d
Replace async/await with AsTask
david-acker May 20, 2022
b670356
Fix variable name
david-acker May 25, 2022
5cb34b6
Merge branch 'main' into request-decompression
sebastienros Jun 3, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 60 additions & 32 deletions AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.Compon
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.Components.SdkAnalyzers.Tests", "src\Tools\SDK-Analyzers\Components\test\Microsoft.AspNetCore.Components.SdkAnalyzers.Tests.csproj", "{DC349A25-0DBF-4468-99E1-B95C22D3A7EF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RequestDecompression", "RequestDecompression", "{5465F96F-33D5-454E-9C40-494E58AEEE5D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RequestDecompression.Tests", "src\Middleware\RequestDecompression\test\Microsoft.AspNetCore.RequestDecompression.Tests.csproj", "{97996D39-7722-4AFC-A41A-AD61CA7A413D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RequestDecompressionSample", "src\Middleware\RequestDecompression\sample\RequestDecompressionSample.csproj", "{37144E52-611B-40E8-807C-2821F5A814CB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RequestDecompression", "src\Middleware\RequestDecompression\src\Microsoft.AspNetCore.RequestDecompression.csproj", "{559FE354-7E08-4310-B4F3-AE30F34DEED5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -3414,38 +3422,6 @@ Global
{8DE6625B-C9E4-4949-A75C-89E3FF556724}.Release|x64.Build.0 = Release|Any CPU
{8DE6625B-C9E4-4949-A75C-89E3FF556724}.Release|x86.ActiveCfg = Release|Any CPU
{8DE6625B-C9E4-4949-A75C-89E3FF556724}.Release|x86.Build.0 = Release|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Debug|arm64.ActiveCfg = Debug|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Debug|arm64.Build.0 = Debug|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Debug|x64.ActiveCfg = Debug|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Debug|x64.Build.0 = Debug|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Debug|x86.ActiveCfg = Debug|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Debug|x86.Build.0 = Debug|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Release|Any CPU.Build.0 = Release|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Release|arm64.ActiveCfg = Release|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Release|arm64.Build.0 = Release|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Release|x64.ActiveCfg = Release|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Release|x64.Build.0 = Release|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Release|x86.ActiveCfg = Release|Any CPU
{BE5D6903-34B9-4C29-85A2-811A7EA06DAF}.Release|x86.Build.0 = Release|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Debug|arm64.ActiveCfg = Debug|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Debug|arm64.Build.0 = Debug|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Debug|x64.ActiveCfg = Debug|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Debug|x64.Build.0 = Debug|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Debug|x86.ActiveCfg = Debug|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Debug|x86.Build.0 = Debug|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Release|Any CPU.Build.0 = Release|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Release|arm64.ActiveCfg = Release|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Release|arm64.Build.0 = Release|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Release|x64.ActiveCfg = Release|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Release|x64.Build.0 = Release|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Release|x86.ActiveCfg = Release|Any CPU
{F3F89B56-66A9-4EBC-8658-80785827237E}.Release|x86.Build.0 = Release|Any CPU
{B81C7FA1-870F-4F21-A928-A5BE18754E6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B81C7FA1-870F-4F21-A928-A5BE18754E6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B81C7FA1-870F-4F21-A928-A5BE18754E6E}.Debug|arm64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -9966,6 +9942,54 @@ Global
{DC349A25-0DBF-4468-99E1-B95C22D3A7EF}.Release|x64.Build.0 = Release|Any CPU
{DC349A25-0DBF-4468-99E1-B95C22D3A7EF}.Release|x86.ActiveCfg = Release|Any CPU
{DC349A25-0DBF-4468-99E1-B95C22D3A7EF}.Release|x86.Build.0 = Release|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Debug|arm64.ActiveCfg = Debug|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Debug|arm64.Build.0 = Debug|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Debug|x64.ActiveCfg = Debug|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Debug|x64.Build.0 = Debug|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Debug|x86.ActiveCfg = Debug|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Debug|x86.Build.0 = Debug|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Release|Any CPU.Build.0 = Release|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Release|arm64.ActiveCfg = Release|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Release|arm64.Build.0 = Release|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Release|x64.ActiveCfg = Release|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Release|x64.Build.0 = Release|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Release|x86.ActiveCfg = Release|Any CPU
{97996D39-7722-4AFC-A41A-AD61CA7A413D}.Release|x86.Build.0 = Release|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Debug|arm64.ActiveCfg = Debug|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Debug|arm64.Build.0 = Debug|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Debug|x64.ActiveCfg = Debug|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Debug|x64.Build.0 = Debug|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Debug|x86.ActiveCfg = Debug|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Debug|x86.Build.0 = Debug|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Release|Any CPU.Build.0 = Release|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Release|arm64.ActiveCfg = Release|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Release|arm64.Build.0 = Release|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Release|x64.ActiveCfg = Release|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Release|x64.Build.0 = Release|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Release|x86.ActiveCfg = Release|Any CPU
{37144E52-611B-40E8-807C-2821F5A814CB}.Release|x86.Build.0 = Release|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Debug|arm64.ActiveCfg = Debug|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Debug|arm64.Build.0 = Debug|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Debug|x64.ActiveCfg = Debug|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Debug|x64.Build.0 = Debug|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Debug|x86.ActiveCfg = Debug|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Debug|x86.Build.0 = Debug|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Release|Any CPU.Build.0 = Release|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Release|arm64.ActiveCfg = Release|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Release|arm64.Build.0 = Release|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Release|x64.ActiveCfg = Release|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Release|x64.Build.0 = Release|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Release|x86.ActiveCfg = Release|Any CPU
{559FE354-7E08-4310-B4F3-AE30F34DEED5}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -10786,6 +10810,10 @@ Global
{CC45FA2D-128B-485D-BA6D-DFD9735CB3C3} = {6C06163A-80E9-49C1-817C-B391852BA563}
{825BCF97-67A9-4834-B3A8-C3DC97A90E41} = {CC45FA2D-128B-485D-BA6D-DFD9735CB3C3}
{DC349A25-0DBF-4468-99E1-B95C22D3A7EF} = {CC45FA2D-128B-485D-BA6D-DFD9735CB3C3}
{5465F96F-33D5-454E-9C40-494E58AEEE5D} = {E5963C9F-20A6-4385-B364-814D2581FADF}
{97996D39-7722-4AFC-A41A-AD61CA7A413D} = {5465F96F-33D5-454E-9C40-494E58AEEE5D}
{37144E52-611B-40E8-807C-2821F5A814CB} = {5465F96F-33D5-454E-9C40-494E58AEEE5D}
{559FE354-7E08-4310-B4F3-AE30F34DEED5} = {5465F96F-33D5-454E-9C40-494E58AEEE5D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E8720B3-DBDD-498C-B383-2CC32A054E8F}
Expand Down
1 change: 1 addition & 0 deletions eng/ProjectReferences.props
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Localization.Routing" ProjectPath="$(RepoRoot)src\Middleware\Localization.Routing\src\Microsoft.AspNetCore.Localization.Routing.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Localization" ProjectPath="$(RepoRoot)src\Middleware\Localization\src\Microsoft.AspNetCore.Localization.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.MiddlewareAnalysis" ProjectPath="$(RepoRoot)src\Middleware\MiddlewareAnalysis\src\Microsoft.AspNetCore.MiddlewareAnalysis.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.RequestDecompression" ProjectPath="$(RepoRoot)src\Middleware\RequestDecompression\src\Microsoft.AspNetCore.RequestDecompression.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.ResponseCaching.Abstractions" ProjectPath="$(RepoRoot)src\Middleware\ResponseCaching.Abstractions\src\Microsoft.AspNetCore.ResponseCaching.Abstractions.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.ResponseCaching" ProjectPath="$(RepoRoot)src\Middleware\ResponseCaching\src\Microsoft.AspNetCore.ResponseCaching.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.ResponseCompression" ProjectPath="$(RepoRoot)src\Middleware\ResponseCompression\src\Microsoft.AspNetCore.ResponseCompression.csproj" />
Expand Down
1 change: 1 addition & 0 deletions eng/SharedFramework.Local.props
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<AspNetCoreAppReference Include="Microsoft.AspNetCore.HttpsPolicy" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Localization.Routing" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Localization" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.RequestDecompression" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.ResponseCaching.Abstractions" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.ResponseCaching" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.ResponseCompression" />
Expand Down
5 changes: 4 additions & 1 deletion src/Middleware/Middleware.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
"src\\Middleware\\MiddlewareAnalysis\\samples\\MiddlewareAnalysisSample\\MiddlewareAnalysisSample.csproj",
"src\\Middleware\\MiddlewareAnalysis\\src\\Microsoft.AspNetCore.MiddlewareAnalysis.csproj",
"src\\Middleware\\MiddlewareAnalysis\\test\\Microsoft.AspNetCore.MiddlewareAnalysis.Tests.csproj",
"src\\Middleware\\RequestDecompression\\sample\\RequestDecompressionSample.csproj",
"src\\Middleware\\RequestDecompression\\src\\Microsoft.AspNetCore.RequestDecompression.csproj",
"src\\Middleware\\RequestDecompression\\test\\Microsoft.AspNetCore.RequestDecompression.Tests.csproj",
"src\\Middleware\\ResponseCaching.Abstractions\\src\\Microsoft.AspNetCore.ResponseCaching.Abstractions.csproj",
"src\\Middleware\\ResponseCaching\\samples\\ResponseCachingSample\\ResponseCachingSample.csproj",
"src\\Middleware\\ResponseCaching\\src\\Microsoft.AspNetCore.ResponseCaching.csproj",
Expand Down Expand Up @@ -115,4 +118,4 @@
"src\\Servers\\Kestrel\\Transport.Sockets\\src\\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.RequestDecompression;

namespace RequestDecompressionSample;

public class CustomDecompressionProvider : IDecompressionProvider
{
public string EncodingName => "custom";

public Stream CreateStream(Stream outputStream)
{
// Create a custom decompression stream wrapper here.
return outputStream;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:6164/",
"sslPort": 0
}
},
"profiles": {
"RequestDecompressionSample": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.RequestDecompression" />
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
<Reference Include="Microsoft.Extensions.Logging.Console" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions src/Middleware/RequestDecompression/sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.RequestDecompression;

namespace RequestDecompressionSample;

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddRequestDecompression(options =>
{
options.Providers.Add<GzipDecompressionProvider>();
options.Providers.Add<CustomDecompressionProvider>();
});
}

public void Configure(IApplicationBuilder app)
{
app.UseRequestDecompression();

app.Map("/test", testApp =>
{
testApp.Run(async context =>
{
using var reader = new StreamReader(context.Request.Body);
var decompressedBody = await reader.ReadToEndAsync(context.RequestAborted);

await context.Response.WriteAsync(decompressedBody, context.RequestAborted);
});
});
}

public static Task Main(string[] args)
{
var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.ConfigureLogging(factory =>
{
factory.AddConsole()
.SetMinimumLevel(LogLevel.Debug);
})
.UseStartup<Startup>();
}).Build();

return host.RunAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO.Compression;

namespace Microsoft.AspNetCore.RequestDecompression;

/// <summary>
/// Brotli decompression provider.
/// </summary>
public class BrotliDecompressionProvider : IDecompressionProvider
david-acker marked this conversation as resolved.
Show resolved Hide resolved
{
/// <inheritdoc />
public string EncodingName => "br";

/// <inheritdoc />
public Stream CreateStream(Stream outputStream)
{
return new BrotliStream(outputStream, CompressionMode.Decompress);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the .NET Foundation under one or more agreements.
david-acker marked this conversation as resolved.
Show resolved Hide resolved
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.ObjectModel;

namespace Microsoft.AspNetCore.RequestDecompression;

/// <summary>
/// A collection of <see cref="IDecompressionProvider"/>'s that also allows them to instantiated from an <see cref="IServiceProvider"/>.
/// </summary>
public class DecompressionProviderCollection : Collection<IDecompressionProvider>
{
/// <summary>
/// Adds a type representing an <see cref="IDecompressionProvider"/>.
/// </summary>
/// <remarks>
/// Provider instances will be created using an <see cref="IServiceProvider"/>.
/// </remarks>
public void Add<TDecompressionProvider>() where TDecompressionProvider : IDecompressionProvider
{
Add(typeof(TDecompressionProvider));
}

/// <summary>
/// Adds a type representing a <see cref="IDecompressionProvider"/>.
/// </summary>
/// <param name="providerType">Type representing an <see cref="IDecompressionProvider"/>.</param>
/// <remarks>
/// Provider instance will be created using an <see cref="IServiceProvider"/>.
/// </remarks>
public void Add(Type providerType)
{
if (providerType == null)
{
throw new ArgumentNullException(nameof(providerType));
}

if (!typeof(IDecompressionProvider).IsAssignableFrom(providerType))
{
throw new ArgumentException($"The provider must implement {nameof(IDecompressionProvider)}.", nameof(providerType));
}

var factory = new DecompressionProviderFactory(providerType);
Add(factory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.RequestDecompression;

/// <summary>
/// This is a placeholder for the <see cref="DecompressionProviderCollection"/> that allows
/// the creation of the given type via an <see cref="IServiceProvider"/>.
/// </summary>
internal class DecompressionProviderFactory : IDecompressionProvider
david-acker marked this conversation as resolved.
Show resolved Hide resolved
{
public DecompressionProviderFactory(Type providerType)
{
ProviderType = providerType;
}

private Type ProviderType { get; }

public IDecompressionProvider CreateInstance(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}

return (IDecompressionProvider)ActivatorUtilities.CreateInstance(serviceProvider, ProviderType, Type.EmptyTypes);
}

string IDecompressionProvider.EncodingName
{
get { throw new NotSupportedException(); }
}

Stream IDecompressionProvider.CreateStream(Stream outputStream)
{
throw new NotSupportedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO.Compression;

namespace Microsoft.AspNetCore.RequestDecompression;

/// <summary>
/// DEFLATE decompression provider.
/// </summary>
public class DeflateDecompressionProvider : IDecompressionProvider
david-acker marked this conversation as resolved.
Show resolved Hide resolved
{
/// <inheritdoc />
public string EncodingName => "deflate";

/// <inheritdoc />
public Stream CreateStream(Stream outputStream)
{
return new DeflateStream(outputStream, CompressionMode.Decompress);
david-acker marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading