Fallback to empty cache control when none is explicitly cached

This commit is contained in:
John Luo 2016-09-12 17:24:39 -07:00
parent 65b89668bb
commit e236e64055
2 changed files with 18 additions and 6 deletions

View File

@ -4,7 +4,6 @@
using System; using System;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Headers; using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.ResponseCaching.Internal;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
@ -12,6 +11,8 @@ namespace Microsoft.AspNetCore.ResponseCaching
{ {
public class CacheabilityValidator : ICacheabilityValidator public class CacheabilityValidator : ICacheabilityValidator
{ {
private static readonly CacheControlHeaderValue EmptyCacheControl = new CacheControlHeaderValue();
public virtual bool RequestIsCacheable(HttpContext httpContext) public virtual bool RequestIsCacheable(HttpContext httpContext)
{ {
var state = httpContext.GetResponseCachingState(); var state = httpContext.GetResponseCachingState();
@ -157,6 +158,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
{ {
var state = httpContext.GetResponseCachingState(); var state = httpContext.GetResponseCachingState();
var age = state.CachedEntryAge; var age = state.CachedEntryAge;
var cachedControlHeaders = cachedResponseHeaders.CacheControl ?? EmptyCacheControl;
// Add min-fresh requirements // Add min-fresh requirements
if (state.RequestCacheControl.MinFresh != null) if (state.RequestCacheControl.MinFresh != null)
@ -165,18 +167,18 @@ namespace Microsoft.AspNetCore.ResponseCaching
} }
// Validate shared max age, this overrides any max age settings for shared caches // Validate shared max age, this overrides any max age settings for shared caches
if (age > cachedResponseHeaders.CacheControl.SharedMaxAge) if (age > cachedControlHeaders.SharedMaxAge)
{ {
// shared max age implies must revalidate // shared max age implies must revalidate
return false; return false;
} }
else if (cachedResponseHeaders.CacheControl.SharedMaxAge == null) else if (cachedControlHeaders.SharedMaxAge == null)
{ {
// Validate max age // Validate max age
if (age > cachedResponseHeaders.CacheControl.MaxAge || age > state.RequestCacheControl.MaxAge) if (age > cachedControlHeaders.MaxAge || age > state.RequestCacheControl.MaxAge)
{ {
// Must revalidate // Must revalidate
if (cachedResponseHeaders.CacheControl.MustRevalidate) if (cachedControlHeaders.MustRevalidate)
{ {
return false; return false;
} }
@ -190,7 +192,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
return false; return false;
} }
else if (cachedResponseHeaders.CacheControl.MaxAge == null && state.RequestCacheControl.MaxAge == null) else if (cachedControlHeaders.MaxAge == null && state.RequestCacheControl.MaxAge == null)
{ {
// Validate expiration // Validate expiration
if (state.ResponseTime > cachedResponseHeaders.Expires) if (state.ResponseTime > cachedResponseHeaders.Expires)

View File

@ -386,6 +386,16 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
Assert.False(new CacheabilityValidator().ResponseIsCacheable(httpContext)); Assert.False(new CacheabilityValidator().ResponseIsCacheable(httpContext));
} }
[Fact]
public void EntryIsFresh_NoCachedCacheControl_FallsbackToEmptyCacheControl()
{
var utcNow = DateTimeOffset.UtcNow;
var httpContext = CreateDefaultContext();
httpContext.GetResponseCachingState().ResponseTime = DateTimeOffset.MaxValue;
Assert.True(new CacheabilityValidator().CachedEntryIsFresh(httpContext, new ResponseHeaders(new HeaderDictionary())));
}
[Fact] [Fact]
public void EntryIsFresh_NoExpiryRequirements_IsFresh() public void EntryIsFresh_NoExpiryRequirements_IsFresh()
{ {