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

View File

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