Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit b8b6c9e

Browse files
committed
Fix etag parsing
1 parent 87e6fa8 commit b8b6c9e

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Globalization;
67
using System.Threading.Tasks;
78
using Microsoft.AspNetCore.Http;
89
using Microsoft.AspNetCore.Http.Features;
9-
using Microsoft.AspNetCore.Http.Headers;
1010
using Microsoft.AspNetCore.ResponseCaching.Internal;
1111
using Microsoft.Extensions.Internal;
1212
using Microsoft.Extensions.Logging;
@@ -366,14 +366,14 @@ internal static bool ContentIsNotModified(ResponseCachingContext context)
366366
}
367367

368368
EntityTagHeaderValue eTag;
369+
IList<EntityTagHeaderValue> ifNoneMatchEtags;
369370
if (!StringValues.IsNullOrEmpty(cachedResponseHeaders[HeaderNames.ETag])
370-
&& EntityTagHeaderValue.TryParse(cachedResponseHeaders[HeaderNames.ETag], out eTag))
371+
&& EntityTagHeaderValue.TryParse(cachedResponseHeaders[HeaderNames.ETag], out eTag)
372+
&& EntityTagHeaderValue.TryParseList(ifNoneMatchHeader, out ifNoneMatchEtags))
371373
{
372-
foreach (var tag in ifNoneMatchHeader)
374+
foreach (var requestETag in ifNoneMatchEtags)
373375
{
374-
EntityTagHeaderValue requestETag;
375-
if (EntityTagHeaderValue.TryParse(tag, out requestETag) &&
376-
eTag.Compare(requestETag, useStrongComparison: false))
376+
if (eTag.Compare(requestETag, useStrongComparison: false))
377377
{
378378
context.Logger.LogNotModifiedIfNoneMatchMatched(requestETag);
379379
return true;

test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingMiddlewareTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Http;
8-
using Microsoft.AspNetCore.Http.Headers;
98
using Microsoft.AspNetCore.ResponseCaching.Internal;
109
using Microsoft.Extensions.Logging.Testing;
1110
using Microsoft.Extensions.Primitives;
@@ -306,14 +305,30 @@ public void ContentIsNotModified_IfNoneMatch_ExplicitWithoutMatch_False()
306305
var sink = new TestSink();
307306
var context = TestUtils.CreateTestContext(sink);
308307
context.CachedResponseHeaders = new HeaderDictionary();
309-
context.HttpContext.Response.Headers[HeaderNames.ETag] = new EntityTagHeaderValue("\"E2\"").ToString();
308+
context.CachedResponseHeaders[HeaderNames.ETag] = new EntityTagHeaderValue("\"E2\"").ToString();
310309

311310
context.HttpContext.Request.Headers[HeaderNames.IfNoneMatch] = new List<EntityTagHeaderValue>(new[] { new EntityTagHeaderValue("\"E1\"") }).ToString();
312311

313312
Assert.False(ResponseCachingMiddleware.ContentIsNotModified(context));
314313
Assert.Empty(sink.Writes);
315314
}
316315

316+
[Fact]
317+
public void ContentIsNotModified_IfNoneMatch_MatchesAtLeastOneValue_True()
318+
{
319+
var sink = new TestSink();
320+
var context = TestUtils.CreateTestContext(sink);
321+
context.CachedResponseHeaders = new HeaderDictionary();
322+
context.CachedResponseHeaders[HeaderNames.ETag] = new EntityTagHeaderValue("\"E2\"").ToString();
323+
324+
context.HttpContext.Request.Headers[HeaderNames.IfNoneMatch] = new string[] { "\"E0\", \"E1\"", "\"E1\", \"E2\"" };
325+
326+
Assert.True(ResponseCachingMiddleware.ContentIsNotModified(context));
327+
TestUtils.AssertLoggedMessages(
328+
sink.Writes,
329+
LoggedMessage.NotModifiedIfNoneMatchMatched);
330+
}
331+
317332
[Fact]
318333
public async Task FinalizeCacheHeaders_DoNotUpdateShouldCacheResponse_IfResponseIsNotCacheable()
319334
{

0 commit comments

Comments
 (0)