Use weak comparison for If-None-Match header (#7237)

This commit is contained in:
Jass Bagga 2018-01-12 12:40:51 -08:00 committed by GitHub
parent 91fb3eb41e
commit 66c13ae5e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 15 deletions

View File

@ -195,6 +195,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
if (etag != null) if (etag != null)
{ {
ifMatchState = GetEtagMatchState( ifMatchState = GetEtagMatchState(
useStrongComparison: true,
etagHeader: ifMatch, etagHeader: ifMatch,
etag: etag, etag: etag,
matchFoundState: PreconditionState.ShouldProcess, matchFoundState: PreconditionState.ShouldProcess,
@ -211,6 +212,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
if (etag != null) if (etag != null)
{ {
ifNoneMatchState = GetEtagMatchState( ifNoneMatchState = GetEtagMatchState(
useStrongComparison: false,
etagHeader: ifNoneMatch, etagHeader: ifNoneMatch,
etag: etag, etag: etag,
matchFoundState: PreconditionState.NotModified, matchFoundState: PreconditionState.NotModified,
@ -245,6 +247,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
} }
private static PreconditionState GetEtagMatchState( private static PreconditionState GetEtagMatchState(
bool useStrongComparison,
IList<EntityTagHeaderValue> etagHeader, IList<EntityTagHeaderValue> etagHeader,
EntityTagHeaderValue etag, EntityTagHeaderValue etag,
PreconditionState matchFoundState, PreconditionState matchFoundState,
@ -255,7 +258,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
var state = matchNotFoundState; var state = matchNotFoundState;
foreach (var entityTag in etagHeader) foreach (var entityTag in etagHeader)
{ {
if (entityTag.Equals(EntityTagHeaderValue.Any) || entityTag.Compare(etag, useStrongComparison: true)) if (entityTag.Equals(EntityTagHeaderValue.Any) || entityTag.Compare(etag, useStrongComparison))
{ {
state = matchFoundState; state = matchFoundState;
break; break;

View File

@ -252,10 +252,12 @@ namespace Microsoft.AspNetCore.Mvc
} }
[Theory] [Theory]
[InlineData("\"Etag\"", "\"NotEtag\"", "\"Etag\"")] [InlineData("\"Etag\"", "\"NotEtag\"", true, "\"Etag\"")]
[InlineData("\"Etag\"", null, null)] [InlineData("\"Etag\"", "\"NotEtag\"", false, "\"Etag\"")]
[InlineData(null, "\"NotEtag\"", "\"Etag\"")] [InlineData("\"Etag\"", null, false, null)]
public void GetPreconditionState_ShouldProcess(string ifMatch, string ifNoneMatch, string ifRange) [InlineData(null, "\"NotEtag\"", true, "\"Etag\"")]
[InlineData(null, "\"NotEtag\"", false, "\"Etag\"")]
public void GetPreconditionState_ShouldProcess(string ifMatch, string ifNoneMatch, bool isWeak, string ifRange)
{ {
// Arrange // Arrange
var actionContext = new ActionContext(); var actionContext = new ActionContext();
@ -272,7 +274,7 @@ namespace Microsoft.AspNetCore.Mvc
httpRequestHeaders.IfNoneMatch = ifNoneMatch == null ? null : new[] httpRequestHeaders.IfNoneMatch = ifNoneMatch == null ? null : new[]
{ {
new EntityTagHeaderValue(ifNoneMatch), new EntityTagHeaderValue(ifNoneMatch, isWeak),
}; };
httpRequestHeaders.IfRange = ifRange == null ? null : new RangeConditionHeaderValue(ifRange); httpRequestHeaders.IfRange = ifRange == null ? null : new RangeConditionHeaderValue(ifRange);
httpRequestHeaders.IfUnmodifiedSince = lastModified; httpRequestHeaders.IfUnmodifiedSince = lastModified;
@ -291,10 +293,12 @@ namespace Microsoft.AspNetCore.Mvc
} }
[Theory] [Theory]
[InlineData("\"NotEtag\"", null)] [InlineData("\"NotEtag\"", null, true)]
[InlineData("\"Etag\"", "\"Etag\"")] [InlineData("\"NotEtag\"", null, false)]
[InlineData(null, null)] [InlineData("\"Etag\"", "\"Etag\"", true)]
public void GetPreconditionState_ShouldNotProcess_PreconditionFailed(string ifMatch, string ifNoneMatch) [InlineData("\"Etag\"", "\"Etag\"", false)]
[InlineData(null, null, false)]
public void GetPreconditionState_ShouldNotProcess_PreconditionFailed(string ifMatch, string ifNoneMatch, bool isWeak)
{ {
// Arrange // Arrange
var actionContext = new ActionContext(); var actionContext = new ActionContext();
@ -310,7 +314,7 @@ namespace Microsoft.AspNetCore.Mvc
httpRequestHeaders.IfNoneMatch = ifNoneMatch == null ? null : new[] httpRequestHeaders.IfNoneMatch = ifNoneMatch == null ? null : new[]
{ {
new EntityTagHeaderValue(ifNoneMatch), new EntityTagHeaderValue(ifNoneMatch, isWeak),
}; };
httpRequestHeaders.IfUnmodifiedSince = DateTimeOffset.MinValue; httpRequestHeaders.IfUnmodifiedSince = DateTimeOffset.MinValue;
httpRequestHeaders.IfModifiedSince = DateTimeOffset.MinValue.AddDays(2); httpRequestHeaders.IfModifiedSince = DateTimeOffset.MinValue.AddDays(2);
@ -328,9 +332,10 @@ namespace Microsoft.AspNetCore.Mvc
} }
[Theory] [Theory]
[InlineData(null, "\"Etag\"")] [InlineData(null, "\"Etag\"", true)]
[InlineData(null, null)] [InlineData(null, "\"Etag\"", false)]
public void GetPreconditionState_ShouldNotProcess_NotModified(string ifMatch, string ifNoneMatch) [InlineData(null, null, false)]
public void GetPreconditionState_ShouldNotProcess_NotModified(string ifMatch, string ifNoneMatch, bool isWeak)
{ {
// Arrange // Arrange
var actionContext = new ActionContext(); var actionContext = new ActionContext();
@ -347,7 +352,7 @@ namespace Microsoft.AspNetCore.Mvc
httpRequestHeaders.IfNoneMatch = ifNoneMatch == null ? null : new[] httpRequestHeaders.IfNoneMatch = ifNoneMatch == null ? null : new[]
{ {
new EntityTagHeaderValue(ifNoneMatch), new EntityTagHeaderValue(ifNoneMatch, isWeak),
}; };
httpRequestHeaders.IfModifiedSince = lastModified; httpRequestHeaders.IfModifiedSince = lastModified;
actionContext.HttpContext = httpContext; actionContext.HttpContext = httpContext;