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

Commit eb8426a

Browse files
committed
Feedback
1 parent 2c1b708 commit eb8426a

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingContext.cs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -447,44 +447,46 @@ internal async Task<bool> TryServeFromCacheAsync()
447447
if (ConditionalRequestSatisfied(cachedResponseHeaders))
448448
{
449449
_httpContext.Response.StatusCode = StatusCodes.Status304NotModified;
450-
return true;
451-
}
452-
453-
var response = _httpContext.Response;
454-
// Copy the cached status code and response headers
455-
response.StatusCode = cachedResponse.StatusCode;
456-
foreach (var header in cachedResponse.Headers)
457-
{
458-
response.Headers.Add(header);
459-
}
460-
461-
response.Headers[HeaderNames.Age] = age.TotalSeconds.ToString("F0", CultureInfo.InvariantCulture);
462-
463-
if (_responseType == ResponseType.HeadersOnly)
464-
{
465450
responseServed = true;
466451
}
467-
else if (_responseType == ResponseType.FullReponse)
452+
else
468453
{
469-
// Copy the cached response body
470-
var body = cachedResponse.Body;
471-
472-
// Add a content-length if required
473-
if (response.ContentLength == null && string.IsNullOrEmpty(response.Headers[HeaderNames.TransferEncoding]))
454+
var response = _httpContext.Response;
455+
// Copy the cached status code and response headers
456+
response.StatusCode = cachedResponse.StatusCode;
457+
foreach (var header in cachedResponse.Headers)
474458
{
475-
response.ContentLength = body.Length;
459+
response.Headers.Add(header);
476460
}
477461

478-
if (body.Length > 0)
462+
response.Headers[HeaderNames.Age] = age.TotalSeconds.ToString("F0", CultureInfo.InvariantCulture);
463+
464+
if (_responseType == ResponseType.HeadersOnly)
479465
{
480-
await response.Body.WriteAsync(body, 0, body.Length);
466+
responseServed = true;
481467
}
468+
else if (_responseType == ResponseType.FullReponse)
469+
{
470+
// Copy the cached response body
471+
var body = cachedResponse.Body;
482472

483-
responseServed = true;
484-
}
485-
else
486-
{
487-
throw new InvalidOperationException($"{nameof(_responseType)} not specified or is unrecognized.");
473+
// Add a content-length if required
474+
if (response.ContentLength == null && string.IsNullOrEmpty(response.Headers[HeaderNames.TransferEncoding]))
475+
{
476+
response.ContentLength = body.Length;
477+
}
478+
479+
if (body.Length > 0)
480+
{
481+
await response.Body.WriteAsync(body, 0, body.Length);
482+
}
483+
484+
responseServed = true;
485+
}
486+
else
487+
{
488+
throw new InvalidOperationException($"{nameof(_responseType)} not specified or is unrecognized.");
489+
}
488490
}
489491
}
490492
else
@@ -496,24 +498,26 @@ internal async Task<bool> TryServeFromCacheAsync()
496498
if (!responseServed && RequestCacheControl.OnlyIfCached)
497499
{
498500
_httpContext.Response.StatusCode = StatusCodes.Status504GatewayTimeout;
499-
return true;
501+
responseServed = true;
500502
}
501503

502504
return responseServed;
503505
}
504506

505507
internal bool ConditionalRequestSatisfied(ResponseHeaders cachedResponseHeaders)
506508
{
507-
if (RequestHeaders.IfNoneMatch != null)
509+
var ifNoneMatchHeader = RequestHeaders.IfNoneMatch;
510+
511+
if (ifNoneMatchHeader != null)
508512
{
509-
if (RequestHeaders.IfNoneMatch.Count == 1 && RequestHeaders.IfNoneMatch[0].Equals(EntityTagHeaderValue.Any))
513+
if (ifNoneMatchHeader.Count == 1 && ifNoneMatchHeader[0].Equals(EntityTagHeaderValue.Any))
510514
{
511515
return true;
512516
}
513517

514518
if (cachedResponseHeaders.ETag != null)
515519
{
516-
foreach (var tag in RequestHeaders.IfNoneMatch)
520+
foreach (var tag in ifNoneMatchHeader)
517521
{
518522
// TODO: use strong comparison
519523
if (cachedResponseHeaders.ETag.Equals(tag))

0 commit comments

Comments
 (0)