@@ -447,44 +447,46 @@ internal async Task<bool> TryServeFromCacheAsync()
447
447
if ( ConditionalRequestSatisfied ( cachedResponseHeaders ) )
448
448
{
449
449
_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
- {
465
450
responseServed = true ;
466
451
}
467
- else if ( _responseType == ResponseType . FullReponse )
452
+ else
468
453
{
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 )
474
458
{
475
- response . ContentLength = body . Length ;
459
+ response . Headers . Add ( header ) ;
476
460
}
477
461
478
- if ( body . Length > 0 )
462
+ response . Headers [ HeaderNames . Age ] = age . TotalSeconds . ToString ( "F0" , CultureInfo . InvariantCulture ) ;
463
+
464
+ if ( _responseType == ResponseType . HeadersOnly )
479
465
{
480
- await response . Body . WriteAsync ( body , 0 , body . Length ) ;
466
+ responseServed = true ;
481
467
}
468
+ else if ( _responseType == ResponseType . FullReponse )
469
+ {
470
+ // Copy the cached response body
471
+ var body = cachedResponse . Body ;
482
472
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
+ }
488
490
}
489
491
}
490
492
else
@@ -496,24 +498,26 @@ internal async Task<bool> TryServeFromCacheAsync()
496
498
if ( ! responseServed && RequestCacheControl . OnlyIfCached )
497
499
{
498
500
_httpContext . Response . StatusCode = StatusCodes . Status504GatewayTimeout ;
499
- return true ;
501
+ responseServed = true ;
500
502
}
501
503
502
504
return responseServed ;
503
505
}
504
506
505
507
internal bool ConditionalRequestSatisfied ( ResponseHeaders cachedResponseHeaders )
506
508
{
507
- if ( RequestHeaders . IfNoneMatch != null )
509
+ var ifNoneMatchHeader = RequestHeaders . IfNoneMatch ;
510
+
511
+ if ( ifNoneMatchHeader != null )
508
512
{
509
- if ( RequestHeaders . IfNoneMatch . Count == 1 && RequestHeaders . IfNoneMatch [ 0 ] . Equals ( EntityTagHeaderValue . Any ) )
513
+ if ( ifNoneMatchHeader . Count == 1 && ifNoneMatchHeader [ 0 ] . Equals ( EntityTagHeaderValue . Any ) )
510
514
{
511
515
return true ;
512
516
}
513
517
514
518
if ( cachedResponseHeaders . ETag != null )
515
519
{
516
- foreach ( var tag in RequestHeaders . IfNoneMatch )
520
+ foreach ( var tag in ifNoneMatchHeader )
517
521
{
518
522
// TODO: use strong comparison
519
523
if ( cachedResponseHeaders . ETag . Equals ( tag ) )
0 commit comments