Update base key

- Add Scheme, Host, Port and PathBase
This commit is contained in:
John Luo 2017-11-06 14:12:28 -08:00
parent 9cea093888
commit 19e943e6b9
3 changed files with 21 additions and 12 deletions

View File

@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
return new string[] { CreateStorageVaryByKey(context) }; return new string[] { CreateStorageVaryByKey(context) };
} }
// GET<delimiter>/PATH // GET<delimiter>SCHEME<delimiter>HOST:PORT/PATHBASE/PATH
public string CreateBaseKey(ResponseCachingContext context) public string CreateBaseKey(ResponseCachingContext context)
{ {
if (context == null) if (context == null)
@ -54,15 +54,22 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
{ {
builder builder
.AppendUpperInvariant(request.Method) .AppendUpperInvariant(request.Method)
.Append(KeyDelimiter); .Append(KeyDelimiter)
.AppendUpperInvariant(request.Scheme)
.Append(KeyDelimiter)
.AppendUpperInvariant(request.Host.Value);
if (_options.UseCaseSensitivePaths) if (_options.UseCaseSensitivePaths)
{ {
builder.Append(request.Path.Value); builder
.Append(request.PathBase.Value)
.Append(request.Path.Value);
} }
else else
{ {
builder.AppendUpperInvariant(request.Path.Value); builder
.AppendUpperInvariant(request.PathBase.Value)
.AppendUpperInvariant(request.Path.Value);
} }
return builder.ToString(); return builder.ToString();

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Text; using System.Text;
namespace Microsoft.AspNetCore.ResponseCaching.Internal namespace Microsoft.AspNetCore.ResponseCaching.Internal
@ -10,10 +9,13 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
{ {
internal static StringBuilder AppendUpperInvariant(this StringBuilder builder, string value) internal static StringBuilder AppendUpperInvariant(this StringBuilder builder, string value)
{ {
builder.EnsureCapacity(builder.Length + value.Length); if (string.IsNullOrEmpty(value))
for (var i = 0; i < value.Length; i++)
{ {
builder.Append(char.ToUpperInvariant(value[i])); builder.EnsureCapacity(builder.Length + value.Length);
for (var i = 0; i < value.Length; i++)
{
builder.Append(char.ToUpperInvariant(value[i]));
}
} }
return builder; return builder;

View File

@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
private static readonly char KeyDelimiter = '\x1e'; private static readonly char KeyDelimiter = '\x1e';
[Fact] [Fact]
public void ResponseCachingKeyProvider_CreateStorageBaseKey_IncludesOnlyNormalizedMethodAndPath() public void ResponseCachingKeyProvider_CreateStorageBaseKey_IncludesOnlyNormalizedMethodSchemeHostPortAndPath()
{ {
var cacheKeyProvider = TestUtils.CreateTestKeyProvider(); var cacheKeyProvider = TestUtils.CreateTestKeyProvider();
var context = TestUtils.CreateTestContext(); var context = TestUtils.CreateTestContext();
@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.HttpContext.Request.PathBase = "/pathBase"; context.HttpContext.Request.PathBase = "/pathBase";
context.HttpContext.Request.QueryString = new QueryString("?query.Key=a&query.Value=b"); context.HttpContext.Request.QueryString = new QueryString("?query.Key=a&query.Value=b");
Assert.Equal($"HEAD{KeyDelimiter}/PATH/SUBPATH", cacheKeyProvider.CreateBaseKey(context)); Assert.Equal($"HEAD{KeyDelimiter}HTTPS{KeyDelimiter}EXAMPLE.COM:80/PATHBASE/PATH/SUBPATH", cacheKeyProvider.CreateBaseKey(context));
} }
[Fact] [Fact]
@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.HttpContext.Request.Method = HttpMethods.Get; context.HttpContext.Request.Method = HttpMethods.Get;
context.HttpContext.Request.Path = "/Path"; context.HttpContext.Request.Path = "/Path";
Assert.Equal($"{HttpMethods.Get}{KeyDelimiter}/PATH", cacheKeyProvider.CreateBaseKey(context)); Assert.Equal($"{HttpMethods.Get}{KeyDelimiter}{KeyDelimiter}/PATH", cacheKeyProvider.CreateBaseKey(context));
} }
[Fact] [Fact]
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
context.HttpContext.Request.Method = HttpMethods.Get; context.HttpContext.Request.Method = HttpMethods.Get;
context.HttpContext.Request.Path = "/Path"; context.HttpContext.Request.Path = "/Path";
Assert.Equal($"{HttpMethods.Get}{KeyDelimiter}/Path", cacheKeyProvider.CreateBaseKey(context)); Assert.Equal($"{HttpMethods.Get}{KeyDelimiter}{KeyDelimiter}/Path", cacheKeyProvider.CreateBaseKey(context));
} }
[Fact] [Fact]