Repository navigation
Expand file tree
/
Copy pathResponseCompressionSetup.cs
More file actions
117 lines (110 loc) · 3.62 KB
/
Copy pathResponseCompressionSetup.cs
File metadata and controls
117 lines (110 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System.IO.Compression;
using Microsoft.AspNetCore.ResponseCompression;
namespace NpgsqlRestClient;
/// <summary>
/// Response compression as the client configures it (the ResponseCompression configuration section).
/// </summary>
public static class ResponseCompressionSetup
{
/// <summary>True when this build can compress with Zstandard (.NET 11 and later).</summary>
public static bool ZstandardSupported =>
#if NET11_0_OR_GREATER
true;
#else
false;
#endif
public static readonly string[] DefaultMimeTypes = [
"text/plain",
"text/css",
"application/javascript",
"text/javascript",
"text/html",
"application/xml",
"text/xml",
"application/json",
"text/json",
"image/svg+xml",
"font/woff",
"font/woff2",
"application/font-woff",
"application/font-woff2"];
/// <summary>
/// The Zstandard quality that stands for a compression level: the algorithm's default for Optimal, its
/// fastest regular level for Fastest, and a high level short of the "ultra" ones (20 and up, which take far
/// more memory on both sides) for SmallestSize.
/// </summary>
public static int ZstandardQualityFor(CompressionLevel level) => level switch
{
CompressionLevel.Fastest => 1,
CompressionLevel.SmallestSize => 19,
CompressionLevel.NoCompression => 1,
_ => 3
};
/// <summary>
/// Registers response compression. Providers are tried in the order Zstandard, Brotli, Gzip among the
/// encodings the client accepts with equal preference. Returns false, registering nothing, when no
/// algorithm is on.
/// </summary>
public static bool Add(
IServiceCollection services,
bool enableForHttps,
bool useZstandard,
bool useBrotli,
bool useGzip,
CompressionLevel level,
int? zstandardQuality = null,
IEnumerable<string>? mimeTypes = null,
IEnumerable<string>? excludedMimeTypes = null)
{
useZstandard = useZstandard && ZstandardSupported;
if (useZstandard is false && useBrotli is false && useGzip is false)
{
return false;
}
services.AddResponseCompression(options =>
{
options.EnableForHttps = enableForHttps;
#if NET11_0_OR_GREATER
if (useZstandard is true)
{
options.Providers.Add<ZstandardCompressionProvider>();
}
#endif
if (useBrotli is true)
{
options.Providers.Add<BrotliCompressionProvider>();
}
if (useGzip is true)
{
options.Providers.Add<GzipCompressionProvider>();
}
options.MimeTypes = mimeTypes?.ToArray() ?? DefaultMimeTypes;
options.ExcludedMimeTypes = excludedMimeTypes?.ToArray() ?? [];
});
#if NET11_0_OR_GREATER
if (useZstandard is true)
{
var quality = zstandardQuality ?? ZstandardQualityFor(level);
services.Configure<ZstandardCompressionProviderOptions>(options =>
{
options.CompressionOptions.Quality = quality;
});
}
#endif
if (useBrotli is true)
{
services.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = level;
});
}
if (useGzip is true)
{
services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = level;
});
}
return true;
}
}