1- using System . IO ;
2- using Microsoft . AspNet . OutputCache . Resource ;
3-
4- namespace Microsoft . AspNet . OutputCache {
1+ namespace Microsoft . AspNet . OutputCache {
52 using System . Collections . Generic ;
63 using System . Linq ;
74 using System ;
@@ -13,8 +10,10 @@ namespace Microsoft.AspNet.OutputCache {
1310 using System . Text ;
1411 using System . Globalization ;
1512 using System . Diagnostics ;
13+ using System . IO ;
14+ using Microsoft . AspNet . OutputCache . Resource ;
1615
17- internal class OutputCacheHelper {
16+ class OutputCacheHelper {
1817 private const int MaxPostKeyLength = 15000 ;
1918 private const string NullVarybyValue = "+n+" ;
2019 internal const string TagOutputcache = "OutputCache" ;
@@ -23,7 +22,6 @@ internal class OutputCacheHelper {
2322 private const string Identity = "identity" ;
2423 private const string Asterisk = "*" ;
2524 private const string OutputcacheKeyprefixDependencies = "Microsoft.AspNet.OutputCache.Dependencies" ;
26- private readonly CacheItemRemovedCallback _dependencyRemovedCallback = null ;
2725 private InMemoryOutputCacheProvider _inMemoryOutputCacheProvider ;
2826
2927 private InMemoryOutputCacheProvider InMemoryOutputCacheProvider => _inMemoryOutputCacheProvider ??
@@ -37,7 +35,7 @@ private async Task RemoveFromProvider(string key, string providerName) {
3735 // it's in the internal cache.
3836 if ( providerName != null ) {
3937 OutputCacheProviderCollection providers = OutputCache . Providers ;
40- provider = ( OutputCacheProviderAsync ) providers ? [ providerName ] ;
38+ provider = providers ? [ providerName ] as OutputCacheProviderAsync ;
4139 }
4240 else {
4341 provider = InMemoryOutputCacheProvider ;
@@ -70,7 +68,6 @@ private bool HasDependencyChanged(string depKey, string[] fileDeps, string kerne
7068 var dep = new CacheDependency ( fileDeps ) ;
7169 int idStartIndex = OutputcacheKeyprefixDependencies . Length ;
7270 int idLength = depKey . Length - idStartIndex ;
73- CacheItemRemovedCallback callback = _dependencyRemovedCallback ;
7471 // have the file dependencies changed?
7572 if ( string . Compare ( dep . GetUniqueID ( ) , 0 , depKey , idStartIndex , idLength , StringComparison . Ordinal ) == 0 ) {
7673 // file dependencies have not changed--cache them with callback to remove OutputCacheEntry if they change
@@ -84,7 +81,7 @@ private bool HasDependencyChanged(string depKey, string[] fileDeps, string kerne
8481 Dependencies = dep ,
8582 CacheItemPriority = CacheItemPriority . Normal ,
8683 DependencyCacheTimeSpan = Cache . NoSlidingExpiration ,
87- DependencyRemovedCallback = callback
84+ DependencyRemovedCallback = null
8885 } ;
8986 InMemoryOutputCacheProvider . Add ( depKey , dcew , DateTimeOffset . MaxValue ) ;
9087 return false ;
@@ -155,7 +152,7 @@ private static CachedRawResponse Convert(OutputCacheEntry oce) {
155152 } ;
156153 }
157154
158- internal async Task < object > GetAsync ( string key ) {
155+ public async Task < object > GetAsync ( string key ) {
159156 OutputCacheProviderAsync provider = GetProvider ( HttpContext . Current ) ;
160157 object result = await provider . GetAsync ( key ) ;
161158 var oce = result as OutputCacheEntry ;
@@ -170,7 +167,7 @@ internal async Task<object> GetAsync(string key) {
170167 return result ;
171168 }
172169
173- internal async Task RemoveAsync ( string key , HttpContext context ) {
170+ public async Task RemoveAsync ( string key , HttpContext context ) {
174171 // we don't know if it's in the internal cache or
175172 // one of the providers. If a context is given,
176173 // then we can narrow down to at most one provider.
@@ -189,7 +186,7 @@ internal async Task RemoveAsync(string key, HttpContext context) {
189186 }
190187 }
191188
192- internal async Task InsertResponseAsync ( string cachedVaryKey ,
189+ public async Task InsertResponseAsync ( string cachedVaryKey ,
193190 CachedVary cachedVary ,
194191 string rawResponseKey ,
195192 CachedRawResponse rawResponse ,
@@ -248,7 +245,7 @@ internal async Task InsertResponseAsync(string cachedVaryKey,
248245 Dependencies = dependencies ,
249246 CacheItemPriority = CacheItemPriority . Normal ,
250247 DependencyCacheTimeSpan = Cache . NoSlidingExpiration ,
251- DependencyRemovedCallback = _dependencyRemovedCallback
248+ DependencyRemovedCallback = null
252249 } ;
253250 object d = await provider . AddAsync ( depKey , dcew , absExp ) ;
254251 if ( d != null ) {
@@ -258,7 +255,7 @@ internal async Task InsertResponseAsync(string cachedVaryKey,
258255 }
259256 }
260257
261- internal static bool IsCacheableEncoding ( string coding , string [ ] contentEncodings ) {
258+ public static bool IsCacheableEncoding ( string coding , string [ ] contentEncodings ) {
262259 // return true if we are not varying by content encoding.
263260 if ( contentEncodings == null ) {
264261 return true ;
@@ -268,7 +265,7 @@ internal static bool IsCacheableEncoding(string coding, string[] contentEncoding
268265 // return true if the Content-Encoding header is listed
269266 }
270267
271- internal static bool ContainsNonShareableCookies ( HttpResponse response ) {
268+ public static bool ContainsNonShareableCookies ( HttpResponse response ) {
272269 HttpCookieCollection cookies = response . Cookies ;
273270 for ( int i = 0 ; i < cookies . Count ; i ++ ) {
274271 HttpCookie httpCookie = cookies [ i ] ;
@@ -279,7 +276,7 @@ internal static bool ContainsNonShareableCookies(HttpResponse response) {
279276 return false ;
280277 }
281278
282- internal static void UseSnapshot ( HttpRawResponse rawResponse , bool sendBody , HttpResponse response ) {
279+ public static void UseSnapshot ( HttpRawResponse rawResponse , bool sendBody , HttpResponse response ) {
283280 if ( response . HeadersWritten )
284281 throw new HttpException ( SR . Cannot_use_snapshot_after_headers_sent ) ;
285282 response . Clear ( ) ;
@@ -296,7 +293,7 @@ internal static void UseSnapshot(HttpRawResponse rawResponse, bool sendBody, Htt
296293 response . SuppressContent = ! sendBody ;
297294 }
298295
299- internal static HttpRawResponse GetSnapshot ( HttpResponse response ) {
296+ public static HttpRawResponse GetSnapshot ( HttpResponse response ) {
300297 var headers = new NameValueCollection ( ) ;
301298 const bool hasSubstBlocks = false ;
302299 if ( response . HeadersWritten )
@@ -328,7 +325,7 @@ internal static HttpRawResponse GetSnapshot(HttpResponse response) {
328325 } ;
329326 }
330327
331- internal static HttpCachePolicySettings GetCurrentSettings ( HttpResponse response ) {
328+ public static HttpCachePolicySettings GetCurrentSettings ( HttpResponse response ) {
332329 IEnumerable < KeyValuePair < HttpCacheValidateHandler , object > > validationCallbackInfo =
333330 OutputCacheUtility . GetValidationCallbacks ( response ) ;
334331
@@ -353,7 +350,7 @@ internal static HttpCachePolicySettings GetCurrentSettings(HttpResponse response
353350 } ;
354351 }
355352
356- internal static void ResetFromHttpCachePolicySettings ( HttpCachePolicySettings settings ,
353+ public static void ResetFromHttpCachePolicySettings ( HttpCachePolicySettings settings ,
357354 DateTime utcTimestampRequest , HttpResponse response ) {
358355 response . Cache . SetCacheability ( settings . Cacheability ) ;
359356 response . Cache . VaryByContentEncodings . SetContentEncodings ( settings . VaryByContentEncodings ) ;
@@ -385,7 +382,7 @@ internal static void ResetFromHttpCachePolicySettings(HttpCachePolicySettings se
385382 }
386383 }
387384
388- internal static void UpdateCachedHeaders ( HttpResponse response ) {
385+ public static void UpdateCachedHeaders ( HttpResponse response ) {
389386 //To enable Out of Band OutputCache Module support, we will always refresh the UtcTimestampRequest.
390387 if ( response . Cache . UtcTimestampCreated == DateTime . MinValue ) {
391388 response . Cache . UtcTimestampCreated = HttpContext . Current . Timestamp . ToUniversalTime ( ) ;
@@ -395,6 +392,7 @@ internal static void UpdateCachedHeaders(HttpResponse response) {
395392
396393 private static void UpdateFromDependencies ( HttpResponse response ) {
397394 CacheDependency dep = null ;
395+ DateTime utcFileLastModifiedMax ;
398396 // if response.Cache.GetETag() != null && response.Cache.GetETagFromFileDependencies() == true, then this HttpCachePolicy
399397 // was created from HttpCachePolicySettings and we don't need to update _etag.
400398 if ( response . Cache . GetETag ( ) == null && response . Cache . GetETagFromFileDependencies ( ) ) {
@@ -406,7 +404,7 @@ private static void UpdateFromDependencies(HttpResponse response) {
406404 if ( id == null ) {
407405 throw new HttpException ( SR . No_UniqueId_Cache_Dependency ) ;
408406 }
409- DateTime utcFileLastModifiedMax = UpdateLastModifiedTimeFromDependency ( dep , response ) ;
407+ utcFileLastModifiedMax = UpdateLastModifiedTimeFromDependency ( dep , response ) ;
410408 var sb = new StringBuilder ( 256 ) ;
411409 sb . Append ( HttpRuntime . AppDomainId ) ;
412410 sb . Append ( id ) ;
@@ -415,22 +413,17 @@ private static void UpdateFromDependencies(HttpResponse response) {
415413 response . Cache . SetETag ( "\" " +
416414 System . Convert . ToBase64String (
417415 CryptoUtil . ComputeSha256Hash ( Encoding . UTF8 . GetBytes ( sb . ToString ( ) ) ) ) + "\" " ) ;
418-
419-
420416 if ( ! response . Cache . GetLastModifiedFromFileDependencies ( ) )
421417 return ;
422418 }
423-
424- {
419+ if ( dep == null ) {
420+ dep = OutputCacheUtility . CreateCacheDependency ( response ) ;
425421 if ( dep == null ) {
426- dep = OutputCacheUtility . CreateCacheDependency ( response ) ;
427- if ( dep == null ) {
428- return ;
429- }
422+ return ;
430423 }
431- DateTime utcFileLastModifiedMax = UpdateLastModifiedTimeFromDependency ( dep , response ) ;
432- UtcSetLastModified ( utcFileLastModifiedMax , response ) ;
433424 }
425+ utcFileLastModifiedMax = UpdateLastModifiedTimeFromDependency ( dep , response ) ;
426+ UtcSetLastModified ( utcFileLastModifiedMax , response ) ;
434427 }
435428
436429 private static void UtcSetLastModified ( DateTime utcDate , HttpResponse response ) {
@@ -439,7 +432,6 @@ private static void UtcSetLastModified(DateTime utcDate, HttpResponse response)
439432 * Time may differ if the system time changes in the middle of the request.
440433 * Adjust the timestamp to Now if necessary.
441434 */
442-
443435 DateTime utcNow = DateTime . UtcNow ;
444436 if ( utcDate > utcNow ) {
445437 utcDate = utcNow ;
@@ -450,16 +442,12 @@ private static void UtcSetLastModified(DateTime utcDate, HttpResponse response)
450442 * need to store dates with 1 second resolution or comparisons
451443 * will be off.
452444 */
453-
454445 utcDate = new DateTime ( utcDate . Ticks - ( utcDate . Ticks % TimeSpan . TicksPerSecond ) ) ;
455446 if ( response . Cache . GetUtcLastModified ( ) != DateTime . MinValue || utcDate > response . Cache . GetUtcLastModified ( ) ) {
456447 response . Cache . SetLastModified ( utcDate ) ;
457448 }
458-
459-
460449 }
461450
462-
463451 private static DateTime UpdateLastModifiedTimeFromDependency ( CacheDependency dep , HttpResponse response ) {
464452 DateTime utcFileLastModifiedMax = dep . UtcLastModified ;
465453 if ( utcFileLastModifiedMax < response . Cache . GetUtcLastModified ( ) ) {
@@ -474,7 +462,6 @@ private static DateTime UpdateLastModifiedTimeFromDependency(CacheDependency dep
474462 utcFileLastModifiedMax = utcNow ;
475463 }
476464 return utcFileLastModifiedMax ;
477-
478465 }
479466
480467 private static string CreateOutputCachedItemKey (
@@ -616,7 +603,7 @@ private static string CreateOutputCachedItemKey(
616603 * and form posted data.
617604 */
618605
619- internal static string CreateOutputCachedItemKey ( HttpContext context , CachedVary cachedVary ) {
606+ public static string CreateOutputCachedItemKey ( HttpContext context , CachedVary cachedVary ) {
620607 return CreateOutputCachedItemKey ( context . Request . Path , context . Request . HttpMethod , context , cachedVary ) ;
621608 }
622609
@@ -626,7 +613,7 @@ internal static string CreateOutputCachedItemKey(HttpContext context, CachedVary
626613 * returns either i) an acceptable index in contentEncodings, ii) -1 if the identity is acceptable, or iii) -2 if nothing is acceptable
627614 */
628615
629- internal static int GetAcceptableEncoding ( string [ ] contentEncodings , int startIndex , string acceptEncoding ) {
616+ public static int GetAcceptableEncoding ( string [ ] contentEncodings , int startIndex , string acceptEncoding ) {
630617 // The format of Accept-Encoding is ( 1#( codings [ ";" "q" "=" qvalue ] ) | "*" )
631618 if ( string . IsNullOrEmpty ( acceptEncoding ) ) {
632619 return - 1 ; // use "identity"
@@ -692,7 +679,7 @@ internal static int GetAcceptableEncoding(string[] contentEncodings, int startIn
692679
693680 private static double Tolerance { get ; set ; }
694681
695- // GetAsync the weight of the specified coding from the Accept-Encoding header.
682+ // Get the weight of the specified coding from the Accept-Encoding header.
696683 // 1 means use this coding. 0 means don't use this coding. A number between
697684 // 1 and 0 must be compared with other codings. -1 means the coding was not found
698685 private static double GetAcceptableEncodingHelper ( string coding , string acceptEncoding ) {
@@ -775,7 +762,7 @@ private static bool IsIdentityAcceptable(string acceptEncoding) {
775762 return result ;
776763 }
777764
778- internal static bool IsAcceptableEncoding ( string contentEncoding , string acceptEncoding ) {
765+ public static bool IsAcceptableEncoding ( string contentEncoding , string acceptEncoding ) {
779766 if ( string . IsNullOrEmpty ( contentEncoding ) ) {
780767 // if Content-Encoding is not set treat it as the identity
781768 contentEncoding = Identity ;
@@ -789,7 +776,5 @@ internal static bool IsAcceptableEncoding(string contentEncoding, string acceptE
789776 return ! ( Math . Abs ( weight ) < tolerance ) &&
790777 ( ! ( weight <= 0 ) || Math . Abs ( GetAcceptableEncodingHelper ( Asterisk , acceptEncoding ) ) > 0 ) ;
791778 }
792-
793-
794779 }
795780}
0 commit comments