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

Add options to configure case sensitivity of request paths #26

Merged
merged 1 commit into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class ResponseCachingContext

// Internal for testing
internal ResponseCachingContext(
HttpContext httpContext,
HttpContext httpContext,
IResponseCache cache,
ResponseCachingOptions options,
ObjectPool<StringBuilder> builderPool,
Expand Down Expand Up @@ -149,11 +149,11 @@ internal string CreateCacheKey(CachedVaryBy varyBy)
.Append(KeyDelimiter);
}

// Default key
// Default key
builder
.Append(request.Method.ToUpperInvariant())
.Append(KeyDelimiter)
.Append(request.Path.Value.ToUpperInvariant());
.Append(_options.CaseSensitivePaths ? request.Path.Value : request.Path.Value.ToUpperInvariant());

// Vary by headers
if (varyBy?.Headers.Count > 0)
Expand Down Expand Up @@ -337,7 +337,7 @@ internal bool ResponseIsCacheable()
}

// TODO: public MAY override the cacheability checks for private and status codes

// Check private
if (ResponseCacheControl.Private)
{
Expand Down Expand Up @@ -365,7 +365,7 @@ internal bool ResponseIsCacheable()
internal bool EntryIsFresh(ResponseHeaders responseHeaders, TimeSpan age, bool verifyAgainstRequest)
{
var responseCacheControl = responseHeaders.CacheControl ?? EmptyCacheControl;

// Add min-fresh requirements
if (verifyAgainstRequest)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ namespace Microsoft.AspNetCore.Builder
public class ResponseCachingOptions
{
/// <summary>
/// The largest cacheable size for the response body in bytes.
/// The largest cacheable size for the response body in bytes. The default is set to 1 MB.
/// </summary>
public long MaximumCachedBodySize { get; set; } = 1024 * 1024;

/// <summary>
/// <c>true</c> if request paths are case-sensitive; otherwise <c>false</c>. The default is to treat paths as case-insensitive.
/// </summary>
public bool CaseSensitivePaths { get; set; } = false;

/// <summary>
/// For testing purposes only.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,34 @@ public void CreateCacheKey_CacheKeyModifier_AddsPrefix()
}));
}

[Fact]
public void CreateCacheKey_CaseInsensitivePath_NormalizesPath()
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Method = "GET";
httpContext.Request.Path = "/Path";
var context = CreateTestContext(httpContext, new ResponseCachingOptions()
{
CaseSensitivePaths = false
});

Assert.Equal($"GET{KeyDelimiter}/PATH", context.CreateCacheKey());
}

[Fact]
public void CreateCacheKey_CaseSensitivePath_PreservesPathCase()
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Method = "GET";
httpContext.Request.Path = "/Path";
var context = CreateTestContext(httpContext, new ResponseCachingOptions()
{
CaseSensitivePaths = true
});

Assert.Equal($"GET{KeyDelimiter}/Path", context.CreateCacheKey());
}

[Fact]
public void ResponseIsCacheable_NoPublic_NotAllowed()
{
Expand Down Expand Up @@ -832,6 +860,16 @@ private static ResponseCachingContext CreateTestContext(HttpContext httpContext)
{
return CreateTestContext(
httpContext,
new ResponseCachingOptions(),
new NoopCacheKeyModifier(),
new NoopCacheabilityValidator());
}

private static ResponseCachingContext CreateTestContext(HttpContext httpContext, ResponseCachingOptions options)
{
return CreateTestContext(
httpContext,
options,
new NoopCacheKeyModifier(),
new NoopCacheabilityValidator());
}
Expand All @@ -840,6 +878,7 @@ private static ResponseCachingContext CreateTestContext(HttpContext httpContext,
{
return CreateTestContext(
httpContext,
new ResponseCachingOptions(),
cacheKeyModifier,
new NoopCacheabilityValidator());
}
Expand All @@ -848,19 +887,21 @@ private static ResponseCachingContext CreateTestContext(HttpContext httpContext,
{
return CreateTestContext(
httpContext,
new ResponseCachingOptions(),
new NoopCacheKeyModifier(),
cacheabilityValidator);
}

private static ResponseCachingContext CreateTestContext(
HttpContext httpContext,
ResponseCachingOptions options,
IResponseCachingCacheKeyModifier cacheKeyModifier,
IResponseCachingCacheabilityValidator cacheabilityValidator)
{
return new ResponseCachingContext(
httpContext,
new TestResponseCache(),
new ResponseCachingOptions(),
options,
new DefaultObjectPool<StringBuilder>(new StringBuilderPooledObjectPolicy()),
cacheabilityValidator,
cacheKeyModifier);
Expand Down