Skip to content

Commit 3bff9ad

Browse files
Fix Accept-Encoding parsing
Fix `Accept-Encoding` parsing when there are multiple values or the value includes a quality. See #3486 (comment).
1 parent 525a8a3 commit 3bff9ad

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/Shared/EmbeddedResourceProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ public async Task<bool> TryRespondWithFileAsync(HttpContext httpContext)
8787

8888
private static bool IsGZipAccepted(HttpRequest httpRequest)
8989
{
90-
var acceptEncoding = httpRequest.Headers.AcceptEncoding;
90+
if (httpRequest.GetTypedHeaders().AcceptEncoding is not { Count: > 0 } acceptEncoding)
91+
{
92+
return false;
93+
}
9194

9295
for (int i = 0; i < acceptEncoding.Count; i++)
9396
{
94-
if (string.Equals(acceptEncoding[i], GZipEncodingValue, StringComparison.OrdinalIgnoreCase))
97+
if (string.Equals(acceptEncoding[i]?.Value.Value, GZipEncodingValue, StringComparison.OrdinalIgnoreCase))
9598
{
9699
return true;
97100
}

test/Swashbuckle.AspNetCore.IntegrationTests/ReDocIntegrationTests.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO.Compression;
22
using System.Net;
3+
using System.Net.Http.Headers;
34
using System.Security.Cryptography;
45
using Microsoft.AspNetCore.Builder;
56
using Swashbuckle.AspNetCore.ReDoc;
@@ -219,8 +220,11 @@ public async Task ReDocMiddleware_Returns_ExpectedAssetContents_Decompressed()
219220
Assert.Equal(response.Content.Headers.ContentLength, actual.Length);
220221
}
221222

222-
[Fact]
223-
public async Task ReDocMiddleware_Returns_ExpectedAssetContents_GZip_Compressed()
223+
[Theory]
224+
[InlineData("gzip")]
225+
[InlineData("gzip;q=1.0, identity; q=0.5, *;q=0")]
226+
[InlineData("gzip, deflate, br, zstd")]
227+
public async Task ReDocMiddleware_Returns_ExpectedAssetContents_GZip_Compressed(string acceptEncoding)
224228
{
225229
// Arrange
226230
var cancellationToken = TestContext.Current.CancellationToken;
@@ -229,7 +233,15 @@ public async Task ReDocMiddleware_Returns_ExpectedAssetContents_GZip_Compressed(
229233
using var client = site.BuildClient();
230234

231235
using var request = new HttpRequestMessage(HttpMethod.Get, "/Api-Docs/redoc.standalone.js");
232-
request.Headers.AcceptEncoding.Add(new("gzip"));
236+
237+
var encodings = acceptEncoding.Split(',')
238+
.Select((p) => p.Trim())
239+
.ToList();
240+
241+
foreach (var encoding in encodings)
242+
{
243+
request.Headers.AcceptEncoding.Add(StringWithQualityHeaderValue.Parse(encoding));
244+
}
233245

234246
// Act
235247
using var response = await client.SendAsync(request, cancellationToken);

0 commit comments

Comments
 (0)