The Labs \ Source Viewer \ SSCLI \ System.Net.Cache \ RequestCacheValidator

  1. //------------------------------------------------------------------------------
  2. // <copyright file="RequestCacheValidator.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*++
  16. Abstract:
  17.     The file specifies the contract for plugged cache validation logic.
  18. Author:
  19.     Alexei Vopilov    21-Dec-2002
  20. Revision History:
  21.     Aug 25 2003 - Moved into a separate file and implemented Whidbey M3 changes
  22.     Jan 25 2004 - Changed the visibility of the class from public to internal.
  23. --*/
  24. namespace System.Net.Cache
  25. {
  26.     using System;
  27.     using System.Diagnostics;
  28.     using System.Text;
  29.     using System.IO;
  30.     using System.Collections.Specialized;
  31.     using System.Threading;
  32.    
  33.    
  34.     //
  35.     // We need Undefined value because sometime a cache entry does not provide a clue when it should expire
  36.     // not flags!
  37.     internal enum CacheFreshnessStatus
  38.     {
  39.         Undefined = 0,
  40.         Fresh = 1,
  41.         Stale = 2
  42.     }
  43.    
  44.     //
  45.     // These are valus that can be returned from validation methods.
  46.     // Most validation methods can only return a subset of below values.
  47.     //
  48.     // not flags!
  49.     internal enum CacheValidationStatus
  50.     {
  51.         DoNotUseCache = 0,
  52.         //Cache is not used for this request and response is not cached.
  53.         Fail = 1,
  54.         //Fail this request (allows a protocol to generate own exception)
  55.         DoNotTakeFromCache = 2,
  56.         //Don't used caches value for this request
  57.         RetryResponseFromCache = 3,
  58.         //Retry cache lookup using changed cache key
  59.         RetryResponseFromServer = 4,
  60.         //Retry this request as the result of invalid response received
  61.         ReturnCachedResponse = 5,
  62.         //Return cached response to the application
  63.         CombineCachedAndServerResponse = 6,
  64.         //Combine cached and live responses for this request
  65.         CacheResponse = 7,
  66.         //Replace cache entry with received live response
  67.         UpdateResponseInformation = 8,
  68.         //Update Metadata of cache entry using live response headers
  69.         RemoveFromCache = 9,
  70.         //Remove cache entry referenced to by a cache key.
  71.         DoNotUpdateCache = 10,
  72.         //Do nothing on cache update.
  73.         Continue = 11
  74.         //Proceed to the next protocol stage.
  75.     }
  76.    
  77.     /// <summary>
  78.     /// <para>
  79.     /// This class reserves a pattern for all WebRequest related cache validators.
  80.     /// All exposed protected methods are virtual.
  81.     /// If a derived class method does not call the base method implementation,
  82.     /// then the base class context may not be updated so it's recommended suppressing the base
  83.     /// methods for all subsequent calls on this class.
  84.     /// </para>
  85.     /// </summary>
  86.     internal abstract class RequestCacheValidator
  87.     {
  88.        
  89.         internal WebRequest _Request;
  90.         internal WebResponse _Response;
  91.         internal Stream _CacheStream;
  92.        
  93.         private RequestCachePolicy _Policy;
  94.         private Uri _Uri;
  95.         private string _CacheKey;
  96.         private RequestCacheEntry _CacheEntry;
  97.         private int _ResponseCount;
  98.         private CacheValidationStatus _ValidationStatus;
  99.         private CacheFreshnessStatus _CacheFreshnessStatus;
  100.         private long _CacheStreamOffset;
  101.         private long _CacheStreamLength;
  102.        
  103.         private bool _StrictCacheErrors;
  104.         private TimeSpan _UnspecifiedMaxAge;
  105.        
  106. /*-------------- public members -------------*/       
  107.        
  108.         internal abstract RequestCacheValidator CreateValidator();
  109.        
  110. /*
  111.         //                                   
  112.         protected RequestCacheValidator(): this(false, TimeSpan.FromDays(1))
  113.         {
  114.         }
  115.         */       
  116.        
  117.         protected RequestCacheValidator(bool strictCacheErrors, TimeSpan unspecifiedMaxAge)
  118.         {
  119.             _StrictCacheErrors = strictCacheErrors;
  120.             _UnspecifiedMaxAge = unspecifiedMaxAge;
  121.             _ValidationStatus = CacheValidationStatus.DoNotUseCache;
  122.             _CacheFreshnessStatus = CacheFreshnessStatus.Undefined;
  123.         }
  124.        
  125.         //public
  126.         internal bool StrictCacheErrors {
  127.             get { return _StrictCacheErrors; }
  128.         }
  129.         //
  130.         // This would help cache validation when the entry does
  131.         // not have any expiration mechanism defined.
  132.         //public
  133.         internal TimeSpan UnspecifiedMaxAge {
  134.             get { return _UnspecifiedMaxAge; }
  135.         }
  136.        
  137. /*------------- get-only protected properties -------------*/       
  138.         protected internal Uri Uri {
  139.             get { return _Uri; }
  140.         }
  141.         protected internal WebRequest Request {
  142.             get { return _Request; }
  143.         }
  144.         protected internal WebResponse Response {
  145.             get { return _Response; }
  146.         }
  147.         protected internal RequestCachePolicy Policy {
  148.             get { return _Policy; }
  149.         }
  150.         protected internal int ResponseCount {
  151.             get { return _ResponseCount; }
  152.         }
  153.         protected internal CacheValidationStatus ValidationStatus {
  154.             get { return _ValidationStatus; }
  155.         }
  156.         protected internal CacheFreshnessStatus CacheFreshnessStatus {
  157.             get { return _CacheFreshnessStatus; }
  158.         }
  159.         protected internal RequestCacheEntry CacheEntry {
  160.             get { return _CacheEntry; }
  161.         }
  162.        
  163. /*------------- protected methods and settable protected properties ------------*/       
  164.         protected internal Stream CacheStream {
  165.             get { return _CacheStream; }
  166.             set { _CacheStream = value; }
  167.         }
  168.         //
  169.         protected internal long CacheStreamOffset {
  170.             get { return _CacheStreamOffset; }
  171.             set { _CacheStreamOffset = value; }
  172.         }
  173.         //
  174.         protected internal long CacheStreamLength {
  175.             get { return _CacheStreamLength; }
  176.             set { _CacheStreamLength = value; }
  177.         }
  178.         //
  179.         protected internal string CacheKey {
  180.             get { return _CacheKey; }
  181.         }
  182. /*
  183.             //                                   
  184.             set
  185.             {
  186.                 // Security: Setting a cache key would allow reading an arbitrary cache location
  187.                 //new RequestCachePermission(RequestCacheActions.CacheReadWrite, value).Demand();
  188.                 _CacheKey = value;
  189.             }
  190.             */       
  191.         //
  192. /*-------------- protected virtual methods -------------*/       
  193.         //
  194.         protected internal abstract CacheValidationStatus ValidateRequest();
  195.         //
  196.         protected internal abstract CacheFreshnessStatus ValidateFreshness();
  197.         //
  198.         protected internal abstract CacheValidationStatus ValidateCache();
  199.         //
  200.         protected internal abstract CacheValidationStatus ValidateResponse();
  201.         //
  202.         protected internal abstract CacheValidationStatus RevalidateCache();
  203.         //
  204.         protected internal abstract CacheValidationStatus UpdateCache();
  205.         //
  206.         protected internal virtual void FailRequest(WebExceptionStatus webStatus)
  207.         {
  208.             if (Logging.On)
  209.                 Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_failing_request_with_exception, webStatus.ToString()));
  210.             if (webStatus == WebExceptionStatus.CacheEntryNotFound)
  211.                 throw ExceptionHelper.CacheEntryNotFoundException;
  212.             else if (webStatus == WebExceptionStatus.RequestProhibitedByCachePolicy)
  213.                 throw ExceptionHelper.RequestProhibitedByCachePolicyException;
  214.            
  215.             throw new WebException(NetRes.GetWebStatusString("net_requestaborted", webStatus), webStatus);
  216.         }
  217.        
  218. /*-------------- internal members -------------*/       
  219.         //
  220.         internal void FetchRequest(Uri uri, WebRequest request)
  221.         {
  222.             _Request = request;
  223.             _Policy = request.CachePolicy;
  224.             _Response = null;
  225.             _ResponseCount = 0;
  226.             _ValidationStatus = CacheValidationStatus.DoNotUseCache;
  227.             _CacheFreshnessStatus = CacheFreshnessStatus.Undefined;
  228.             _CacheStream = null;
  229.             _CacheStreamOffset = 0l;
  230.             _CacheStreamLength = 0l;
  231.            
  232.             if (!uri.Equals(_Uri)) {
  233.                 // it's changed from previous call
  234.                 _CacheKey = uri.GetParts(UriComponents.AbsoluteUri, UriFormat.Unescaped);
  235.             }
  236.             _Uri = uri;
  237.         }
  238.         //
  239.         internal void FetchCacheEntry(RequestCacheEntry fetchEntry)
  240.         {
  241.             _CacheEntry = fetchEntry;
  242.         }
  243.        
  244.         internal void FetchResponse(WebResponse fetchResponse)
  245.         {
  246.             ++_ResponseCount;
  247.             _Response = fetchResponse;
  248.         }
  249.        
  250.         internal void SetFreshnessStatus(CacheFreshnessStatus status)
  251.         {
  252.             _CacheFreshnessStatus = status;
  253.         }
  254.        
  255.         internal void SetValidationStatus(CacheValidationStatus status)
  256.         {
  257.             _ValidationStatus = status;
  258.         }
  259.     }
  260.    
  261. }

Developer Fusion