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

Commit 6e9ebc7

Browse files
committed
Cleanup
1 parent aba398d commit 6e9ebc7

File tree

7 files changed

+60
-66
lines changed

7 files changed

+60
-66
lines changed

src/Microsoft.AspNetCore.ResponseCaching/Internal/HttpHeaderParsingHelpers.cs renamed to src/Microsoft.AspNetCore.ResponseCaching/Internal/HttpHeaderHelpers.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Microsoft.AspNetCore.ResponseCaching.Internal
99
{
10-
internal static class HttpHeaderParsingHelpers
10+
internal static class HttpHeaderHelpers
1111
{
1212
private static readonly string[] DateFormats = new string[] {
1313
// "r", // RFC 1123, required output format but too strict for input
@@ -32,21 +32,21 @@ internal static class HttpHeaderParsingHelpers
3232

3333
// Try the various date formats in the order listed above.
3434
// We should accept a wide verity of common formats, but only output RFC 1123 style dates.
35-
internal static bool TryParseHeaderDate(string input, out DateTimeOffset result) => DateTimeOffset.TryParseExact(input, DateFormats, DateTimeFormatInfo.InvariantInfo,
35+
internal static bool TryParseDate(string input, out DateTimeOffset result) => DateTimeOffset.TryParseExact(input, DateFormats, DateTimeFormatInfo.InvariantInfo,
3636
DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal, out result);
3737

3838
// Try to get the value of a specific header from a list of headers
3939
// e.g. "header1=10, header2=30"
40-
internal static bool TryParseHeaderTimeSpan(StringValues headers, string headerName, out TimeSpan? value)
40+
internal static bool TryParseTimeSpan(StringValues headerValues, string targetValue, out TimeSpan? value)
4141
{
42-
foreach (var header in headers)
42+
foreach (var headerValue in headerValues)
4343
{
44-
var index = header.IndexOf(headerName, StringComparison.OrdinalIgnoreCase);
44+
var index = headerValue.IndexOf(targetValue, StringComparison.OrdinalIgnoreCase);
4545
if (index != -1)
4646
{
47-
index += headerName.Length;
47+
index += targetValue.Length;
4848
int seconds;
49-
if (!TryParseHeaderInt(index, header, out seconds))
49+
if (!TryParseInt(index, headerValue, out seconds))
5050
{
5151
break;
5252
}
@@ -58,11 +58,11 @@ internal static bool TryParseHeaderTimeSpan(StringValues headers, string headerN
5858
return false;
5959
}
6060

61-
internal static bool HeaderContains(StringValues headers, string headerName)
61+
internal static bool Contains(StringValues headerValues, string targetValue)
6262
{
63-
foreach (var header in headers)
63+
foreach (var headerValue in headerValues)
6464
{
65-
var index = header.IndexOf(headerName, StringComparison.OrdinalIgnoreCase);
65+
var index = headerValue.IndexOf(targetValue, StringComparison.OrdinalIgnoreCase);
6666
if (index != -1)
6767
{
6868
return true;
@@ -72,29 +72,28 @@ internal static bool HeaderContains(StringValues headers, string headerName)
7272
return false;
7373
}
7474

75-
private static bool TryParseHeaderInt(int startIndex, string header, out int value)
75+
private static bool TryParseInt(int startIndex, string headerValue, out int value)
7676
{
7777
var found = false;
78-
while (startIndex != header.Length)
78+
while (startIndex != headerValue.Length)
7979
{
80-
var c = header[startIndex];
80+
var c = headerValue[startIndex];
8181
if (c == '=')
8282
{
8383
found = true;
8484
}
8585
else if (c != ' ')
8686
{
87-
--startIndex;
8887
break;
8988
}
9089
++startIndex;
9190
}
92-
if (found && startIndex != header.Length)
91+
if (found)
9392
{
94-
var endIndex = startIndex + 1;
95-
while (endIndex < header.Length)
93+
var endIndex = startIndex;
94+
while (endIndex < headerValue.Length)
9695
{
97-
var c = header[endIndex];
96+
var c = headerValue[endIndex];
9897
if ((c >= '0') && (c <= '9'))
9998
{
10099
endIndex++;
@@ -104,10 +103,10 @@ private static bool TryParseHeaderInt(int startIndex, string header, out int val
104103
break;
105104
}
106105
}
107-
var length = endIndex - (startIndex + 1);
106+
var length = endIndex - startIndex;
108107
if (length > 0)
109108
{
110-
value = int.Parse(header.Substring(startIndex + 1, length), NumberStyles.None, NumberFormatInfo.InvariantInfo);
109+
value = int.Parse(headerValue.Substring(startIndex, length), NumberStyles.None, NumberFormatInfo.InvariantInfo);
111110
return true;
112111
}
113112
}

src/Microsoft.AspNetCore.ResponseCaching/Internal/MemoryResponseCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public MemoryResponseCache(IMemoryCache cache)
2424
public Task<IResponseCacheEntry> GetAsync(string key)
2525
{
2626
var entry = _cache.Get(key);
27-
27+
2828
var memoryCachedResponse = entry as MemoryCachedResponse;
2929
if (memoryCachedResponse != null)
3030
{

src/Microsoft.AspNetCore.ResponseCaching/Internal/ResponseCachingContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ internal DateTimeOffset? ResponseDate
6565
{
6666
_parsedResponseDate = true;
6767
DateTimeOffset date;
68-
if (HttpHeaderParsingHelpers.TryParseHeaderDate(HttpContext.Response.Headers[HeaderNames.Date], out date))
68+
if (HttpHeaderHelpers.TryParseDate(HttpContext.Response.Headers[HeaderNames.Date], out date))
6969
{
7070
_responseDate = date;
7171
}
@@ -92,7 +92,7 @@ internal DateTimeOffset? ResponseExpires
9292
{
9393
_parsedResponseExpires = true;
9494
DateTimeOffset expires;
95-
if (HttpHeaderParsingHelpers.TryParseHeaderDate(HttpContext.Response.Headers[HeaderNames.Expires], out expires))
95+
if (HttpHeaderHelpers.TryParseDate(HttpContext.Response.Headers[HeaderNames.Expires], out expires))
9696
{
9797
_responseExpires = expires;
9898
}
@@ -112,7 +112,7 @@ internal TimeSpan? ResponseSharedMaxAge
112112
if (!_parsedResponseSharedMaxAge)
113113
{
114114
_parsedResponseSharedMaxAge = true;
115-
HttpHeaderParsingHelpers.TryParseHeaderTimeSpan(HttpContext.Response.Headers[HeaderNames.CacheControl], CacheControlValues.SharedMaxAgeString, out _responseSharedMaxAge);
115+
HttpHeaderHelpers.TryParseTimeSpan(HttpContext.Response.Headers[HeaderNames.CacheControl], CacheControlValues.SharedMaxAgeString, out _responseSharedMaxAge);
116116
}
117117
return _responseSharedMaxAge;
118118
}
@@ -125,7 +125,7 @@ internal TimeSpan? ResponseMaxAge
125125
if (!_parsedResponseMaxAge)
126126
{
127127
_parsedResponseMaxAge = true;
128-
HttpHeaderParsingHelpers.TryParseHeaderTimeSpan(HttpContext.Response.Headers[HeaderNames.CacheControl], CacheControlValues.MaxAgeString, out _responseMaxAge);
128+
HttpHeaderHelpers.TryParseTimeSpan(HttpContext.Response.Headers[HeaderNames.CacheControl], CacheControlValues.MaxAgeString, out _responseMaxAge);
129129
}
130130
return _responseMaxAge;
131131
}

src/Microsoft.AspNetCore.ResponseCaching/Internal/ResponseCachingPolicyProvider.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public virtual bool IsRequestCacheable(ResponseCachingContext context)
3030
// Verify request cache-control parameters
3131
if (!StringValues.IsNullOrEmpty(request.Headers[HeaderNames.CacheControl]))
3232
{
33-
if (HttpHeaderParsingHelpers.HeaderContains(request.Headers[HeaderNames.CacheControl], CacheControlValues.NoCacheString))
33+
if (HttpHeaderHelpers.Contains(request.Headers[HeaderNames.CacheControl], CacheControlValues.NoCacheString))
3434
{
3535
context.Logger.LogRequestWithNoCacheNotCacheable();
3636
return false;
@@ -40,7 +40,7 @@ public virtual bool IsRequestCacheable(ResponseCachingContext context)
4040
{
4141
// Support for legacy HTTP 1.0 cache directive
4242
var pragmaHeaderValues = request.Headers[HeaderNames.Pragma];
43-
if (HttpHeaderParsingHelpers.HeaderContains(request.Headers[HeaderNames.Pragma], CacheControlValues.NoCacheString))
43+
if (HttpHeaderHelpers.Contains(request.Headers[HeaderNames.Pragma], CacheControlValues.NoCacheString))
4444
{
4545
context.Logger.LogRequestWithPragmaNoCacheNotCacheable();
4646
return false;
@@ -55,27 +55,22 @@ public virtual bool IsResponseCacheable(ResponseCachingContext context)
5555
var responseCacheControlHeader = context.HttpContext.Response.Headers[HeaderNames.CacheControl];
5656

5757
// Only cache pages explicitly marked with public
58-
if (!HttpHeaderParsingHelpers.HeaderContains(responseCacheControlHeader, CacheControlValues.PublicString))
58+
if (!HttpHeaderHelpers.Contains(responseCacheControlHeader, CacheControlValues.PublicString))
5959
{
6060
context.Logger.LogResponseWithoutPublicNotCacheable();
6161
return false;
6262
}
6363

6464
// Check no-store
65-
if (HttpHeaderParsingHelpers.HeaderContains(context.HttpContext.Request.Headers[HeaderNames.CacheControl], CacheControlValues.NoStoreString))
66-
{
67-
context.Logger.LogResponseWithNoStoreNotCacheable();
68-
return false;
69-
}
70-
71-
if (HttpHeaderParsingHelpers.HeaderContains(responseCacheControlHeader, CacheControlValues.NoStoreString))
65+
if (HttpHeaderHelpers.Contains(context.HttpContext.Request.Headers[HeaderNames.CacheControl], CacheControlValues.NoStoreString)
66+
|| HttpHeaderHelpers.Contains(responseCacheControlHeader, CacheControlValues.NoStoreString))
7267
{
7368
context.Logger.LogResponseWithNoStoreNotCacheable();
7469
return false;
7570
}
7671

7772
// Check no-cache
78-
if (HttpHeaderParsingHelpers.HeaderContains(responseCacheControlHeader, CacheControlValues.NoCacheString))
73+
if (HttpHeaderHelpers.Contains(responseCacheControlHeader, CacheControlValues.NoCacheString))
7974
{
8075
context.Logger.LogResponseWithNoCacheNotCacheable();
8176
return false;
@@ -99,7 +94,7 @@ public virtual bool IsResponseCacheable(ResponseCachingContext context)
9994
}
10095

10196
// Check private
102-
if (HttpHeaderParsingHelpers.HeaderContains(responseCacheControlHeader, CacheControlValues.PrivateString))
97+
if (HttpHeaderHelpers.Contains(responseCacheControlHeader, CacheControlValues.PrivateString))
10398
{
10499
context.Logger.LogResponseWithPrivateNotCacheable();
105100
return false;
@@ -159,20 +154,20 @@ public virtual bool IsResponseCacheable(ResponseCachingContext context)
159154
public virtual bool IsCachedEntryFresh(ResponseCachingContext context)
160155
{
161156
var age = context.CachedEntryAge.Value;
162-
var cachedControlHeaders = context.CachedResponseHeaders[HeaderNames.CacheControl];
157+
var cachedCacheControlHeaders = context.CachedResponseHeaders[HeaderNames.CacheControl];
163158
var requestCacheControlHeaders = context.HttpContext.Request.Headers[HeaderNames.CacheControl];
164159

165160
// Add min-fresh requirements
166161
TimeSpan? minFresh;
167-
if (HttpHeaderParsingHelpers.TryParseHeaderTimeSpan(requestCacheControlHeaders, CacheControlValues.MinFreshString, out minFresh))
162+
if (HttpHeaderHelpers.TryParseTimeSpan(requestCacheControlHeaders, CacheControlValues.MinFreshString, out minFresh))
168163
{
169164
age += minFresh.Value;
170165
context.Logger.LogExpirationMinFreshAdded(minFresh.Value);
171166
}
172167

173168
// Validate shared max age, this overrides any max age settings for shared caches
174169
TimeSpan? cachedSharedMaxAge;
175-
HttpHeaderParsingHelpers.TryParseHeaderTimeSpan(cachedControlHeaders, CacheControlValues.SharedMaxAgeString, out cachedSharedMaxAge);
170+
HttpHeaderHelpers.TryParseTimeSpan(cachedCacheControlHeaders, CacheControlValues.SharedMaxAgeString, out cachedSharedMaxAge);
176171

177172
if (age >= cachedSharedMaxAge)
178173
{
@@ -183,24 +178,24 @@ public virtual bool IsCachedEntryFresh(ResponseCachingContext context)
183178
else if (!cachedSharedMaxAge.HasValue)
184179
{
185180
TimeSpan? requestMaxAge;
186-
HttpHeaderParsingHelpers.TryParseHeaderTimeSpan(requestCacheControlHeaders, CacheControlValues.MaxAgeString, out requestMaxAge);
181+
HttpHeaderHelpers.TryParseTimeSpan(requestCacheControlHeaders, CacheControlValues.MaxAgeString, out requestMaxAge);
187182

188183
TimeSpan? cachedMaxAge;
189-
HttpHeaderParsingHelpers.TryParseHeaderTimeSpan(cachedControlHeaders, CacheControlValues.MaxAgeString, out cachedMaxAge);
184+
HttpHeaderHelpers.TryParseTimeSpan(cachedCacheControlHeaders, CacheControlValues.MaxAgeString, out cachedMaxAge);
190185

191186
var lowestMaxAge = cachedMaxAge < requestMaxAge ? cachedMaxAge : requestMaxAge ?? cachedMaxAge;
192187
// Validate max age
193188
if (age >= lowestMaxAge)
194189
{
195190
// Must revalidate
196-
if (HttpHeaderParsingHelpers.HeaderContains(cachedControlHeaders, CacheControlValues.MustRevalidateString))
191+
if (HttpHeaderHelpers.Contains(cachedCacheControlHeaders, CacheControlValues.MustRevalidateString))
197192
{
198193
context.Logger.LogExpirationMustRevalidate(age, lowestMaxAge.Value);
199194
return false;
200195
}
201196

202197
TimeSpan? requestMaxStale;
203-
HttpHeaderParsingHelpers.TryParseHeaderTimeSpan(requestCacheControlHeaders, CacheControlValues.MaxStaleString, out requestMaxStale);
198+
HttpHeaderHelpers.TryParseTimeSpan(requestCacheControlHeaders, CacheControlValues.MaxStaleString, out requestMaxStale);
204199

205200
// Request allows stale values
206201
if (requestMaxStale.HasValue && age - lowestMaxAge < requestMaxStale)
@@ -216,7 +211,7 @@ public virtual bool IsCachedEntryFresh(ResponseCachingContext context)
216211
{
217212
// Validate expiration
218213
DateTimeOffset expires;
219-
if (HttpHeaderParsingHelpers.TryParseHeaderDate(context.CachedResponseHeaders[HeaderNames.Expires], out expires) &&
214+
if (HttpHeaderHelpers.TryParseDate(context.CachedResponseHeaders[HeaderNames.Expires], out expires) &&
220215
context.ResponseTime.Value >= expires)
221216
{
222217
context.Logger.LogExpirationExpiresExceeded(context.ResponseTime.Value, expires);

src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingMiddleware.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ internal async Task<bool> TryServeFromCacheAsync(ResponseCachingContext context)
198198
}
199199
}
200200

201-
if (HttpHeaderParsingHelpers.HeaderContains(context.HttpContext.Request.Headers[HeaderNames.CacheControl], CacheControlValues.OnlyIfCachedString))
201+
if (HttpHeaderHelpers.Contains(context.HttpContext.Request.Headers[HeaderNames.CacheControl], CacheControlValues.OnlyIfCachedString))
202202
{
203203
_logger.LogGatewayTimeoutServed();
204204
context.HttpContext.Response.StatusCode = StatusCodes.Status504GatewayTimeout;
@@ -365,20 +365,18 @@ internal static bool ContentIsNotModified(ResponseCachingContext context)
365365
return true;
366366
}
367367

368-
if (!StringValues.IsNullOrEmpty(cachedResponseHeaders[HeaderNames.ETag]))
368+
EntityTagHeaderValue eTag;
369+
if (!StringValues.IsNullOrEmpty(cachedResponseHeaders[HeaderNames.ETag])
370+
&& EntityTagHeaderValue.TryParse(cachedResponseHeaders[HeaderNames.ETag], out eTag))
369371
{
370-
EntityTagHeaderValue eTag;
371-
if (EntityTagHeaderValue.TryParse(cachedResponseHeaders[HeaderNames.ETag], out eTag))
372+
foreach (var tag in ifNoneMatchHeader)
372373
{
373-
foreach (var tag in ifNoneMatchHeader)
374+
EntityTagHeaderValue requestETag;
375+
if (EntityTagHeaderValue.TryParse(tag, out requestETag) &&
376+
eTag.Compare(requestETag, useStrongComparison: false))
374377
{
375-
EntityTagHeaderValue requestETag;
376-
if (EntityTagHeaderValue.TryParse(tag, out requestETag) &&
377-
eTag.Compare(requestETag, useStrongComparison: false))
378-
{
379-
context.Logger.LogNotModifiedIfNoneMatchMatched(requestETag);
380-
return true;
381-
}
378+
context.Logger.LogNotModifiedIfNoneMatchMatched(requestETag);
379+
return true;
382380
}
383381
}
384382
}
@@ -389,14 +387,16 @@ internal static bool ContentIsNotModified(ResponseCachingContext context)
389387
if (!StringValues.IsNullOrEmpty(ifUnmodifiedSince))
390388
{
391389
DateTimeOffset modified;
392-
if (!HttpHeaderParsingHelpers.TryParseHeaderDate(cachedResponseHeaders[HeaderNames.LastModified], out modified) &&
393-
!HttpHeaderParsingHelpers.TryParseHeaderDate(cachedResponseHeaders[HeaderNames.Date], out modified))
390+
if (!HttpHeaderHelpers.TryParseDate(cachedResponseHeaders[HeaderNames.LastModified], out modified))
394391
{
395-
return false;
392+
if (!HttpHeaderHelpers.TryParseDate(cachedResponseHeaders[HeaderNames.Date], out modified))
393+
{
394+
return false;
395+
}
396396
}
397397

398398
DateTimeOffset unmodifiedSince;
399-
if (HttpHeaderParsingHelpers.TryParseHeaderDate(ifUnmodifiedSince, out unmodifiedSince) &&
399+
if (HttpHeaderHelpers.TryParseDate(ifUnmodifiedSince, out unmodifiedSince) &&
400400
modified <= unmodifiedSince)
401401
{
402402
context.Logger.LogNotModifiedIfUnmodifiedSinceSatisfied(modified, unmodifiedSince);

0 commit comments

Comments
 (0)