@@ -74,50 +74,52 @@ public async Task Invoke(HttpContext httpContext)
74
74
var context = new ResponseCachingContext ( httpContext , _logger ) ;
75
75
76
76
// Should we attempt any caching logic?
77
- if ( _policyProvider . AllowResponseCaching ( context ) )
77
+ if ( _policyProvider . AttemptResponseCaching ( context ) )
78
78
{
79
79
// Can this request be served from cache?
80
80
if ( _policyProvider . AllowCacheLookup ( context ) && await TryServeFromCacheAsync ( context ) )
81
81
{
82
82
return ;
83
83
}
84
84
85
- context . AllowResponseCapture = _policyProvider . AllowCacheStorage ( context ) ;
85
+ // Should we store the response to this request?
86
+ if ( _policyProvider . AllowCacheStorage ( context ) )
87
+ {
88
+ // Hook up to listen to the response stream
89
+ ShimResponseStream ( context ) ;
86
90
87
- // Hook up to listen to the response stream
88
- ShimResponseStream ( context ) ;
91
+ try
92
+ {
93
+ // Subscribe to OnStarting event
94
+ httpContext . Response . OnStarting ( _onStartingCallback , context ) ;
89
95
90
- try
91
- {
92
- // Subscribe to OnStarting event
93
- httpContext . Response . OnStarting ( _onStartingCallback , context ) ;
96
+ await _next ( httpContext ) ;
94
97
95
- await _next ( httpContext ) ;
98
+ // If there was no response body, check the response headers now. We can cache things like redirects.
99
+ await OnResponseStartingAsync ( context ) ;
96
100
97
- // If there was no response body, check the response headers now. We can cache things like redirects.
98
- await OnResponseStartingAsync ( context ) ;
101
+ // Finalize the cache entry
102
+ await FinalizeCacheBodyAsync ( context ) ;
103
+ }
104
+ finally
105
+ {
106
+ UnshimResponseStream ( context ) ;
107
+ }
99
108
100
- // Finalize the cache entry
101
- await FinalizeCacheBodyAsync ( context ) ;
102
- }
103
- finally
104
- {
105
- UnshimResponseStream ( context ) ;
109
+ return ;
106
110
}
107
111
}
108
- else
109
- {
110
- // Add IResponseCachingFeature which may be required when the response is generated
111
- AddResponseCachingFeature ( httpContext ) ;
112
112
113
- try
114
- {
115
- await _next ( httpContext ) ;
116
- }
117
- finally
118
- {
119
- RemoveResponseCachingFeature ( httpContext ) ;
120
- }
113
+ // Response should not be captured but add IResponseCachingFeature which may be required when the response is generated
114
+ AddResponseCachingFeature ( httpContext ) ;
115
+
116
+ try
117
+ {
118
+ await _next ( httpContext ) ;
119
+ }
120
+ finally
121
+ {
122
+ RemoveResponseCachingFeature ( httpContext ) ;
121
123
}
122
124
}
123
125
@@ -327,7 +329,7 @@ internal async Task FinalizeCacheBodyAsync(ResponseCachingContext context)
327
329
328
330
internal Task OnResponseStartingAsync ( ResponseCachingContext context )
329
331
{
330
- if ( context . AllowResponseCapture && ! context . ResponseStarted )
332
+ if ( ! context . ResponseStarted )
331
333
{
332
334
context . ResponseStarted = true ;
333
335
context . ResponseTime = _options . SystemClock . UtcNow ;
@@ -351,19 +353,16 @@ internal static void AddResponseCachingFeature(HttpContext context)
351
353
352
354
internal void ShimResponseStream ( ResponseCachingContext context )
353
355
{
354
- if ( context . AllowResponseCapture )
356
+ // Shim response stream
357
+ context . OriginalResponseStream = context . HttpContext . Response . Body ;
358
+ context . ResponseCachingStream = new ResponseCachingStream ( context . OriginalResponseStream , _options . MaximumBodySize , StreamUtilities . BodySegmentSize ) ;
359
+ context . HttpContext . Response . Body = context . ResponseCachingStream ;
360
+
361
+ // Shim IHttpSendFileFeature
362
+ context . OriginalSendFileFeature = context . HttpContext . Features . Get < IHttpSendFileFeature > ( ) ;
363
+ if ( context . OriginalSendFileFeature != null )
355
364
{
356
- // Shim response stream
357
- context . OriginalResponseStream = context . HttpContext . Response . Body ;
358
- context . ResponseCachingStream = new ResponseCachingStream ( context . OriginalResponseStream , _options . MaximumBodySize , StreamUtilities . BodySegmentSize ) ;
359
- context . HttpContext . Response . Body = context . ResponseCachingStream ;
360
-
361
- // Shim IHttpSendFileFeature
362
- context . OriginalSendFileFeature = context . HttpContext . Features . Get < IHttpSendFileFeature > ( ) ;
363
- if ( context . OriginalSendFileFeature != null )
364
- {
365
- context . HttpContext . Features . Set < IHttpSendFileFeature > ( new SendFileFeatureWrapper ( context . OriginalSendFileFeature , context . ResponseCachingStream ) ) ;
366
- }
365
+ context . HttpContext . Features . Set < IHttpSendFileFeature > ( new SendFileFeatureWrapper ( context . OriginalSendFileFeature , context . ResponseCachingStream ) ) ;
367
366
}
368
367
369
368
// Add IResponseCachingFeature
@@ -375,14 +374,11 @@ internal static void RemoveResponseCachingFeature(HttpContext context) =>
375
374
376
375
internal static void UnshimResponseStream ( ResponseCachingContext context )
377
376
{
378
- if ( context . AllowResponseCapture )
379
- {
380
- // Unshim response stream
381
- context . HttpContext . Response . Body = context . OriginalResponseStream ;
377
+ // Unshim response stream
378
+ context . HttpContext . Response . Body = context . OriginalResponseStream ;
382
379
383
- // Unshim IHttpSendFileFeature
384
- context . HttpContext . Features . Set ( context . OriginalSendFileFeature ) ;
385
- }
380
+ // Unshim IHttpSendFileFeature
381
+ context . HttpContext . Features . Set ( context . OriginalSendFileFeature ) ;
386
382
387
383
// Remove IResponseCachingFeature
388
384
RemoveResponseCachingFeature ( context . HttpContext ) ;
0 commit comments