public static void ApplyCacheRequestDirective(this HttpRequestMessage requestMessage, CacheRequestDirective cacheDirective, ConditionalRequestDirective conditionalDirective) { var cacheControlHeader = new CacheControlHeaderValue(); cacheControlHeader.MaxAge = cacheDirective.MaxAge; cacheControlHeader.MaxStale = cacheDirective.MaxStale.HasValue; if (cacheDirective.MaxStale != TimeSpan.MaxValue) cacheControlHeader.MaxStaleLimit = cacheDirective.MaxStale; cacheControlHeader.MinFresh = cacheDirective.MinFresh; cacheControlHeader.NoCache = cacheDirective.NoCache; if (cacheDirective.NoCache) requestMessage.Headers.Pragma.Add(new NameValueHeaderValue("no-cache")); cacheControlHeader.NoStore = cacheDirective.NoStore; // TODO deal with no-transform cacheControlHeader.OnlyIfCached = cacheDirective.OnlyIfCached; requestMessage.Headers.CacheControl = cacheControlHeader; requestMessage.Headers.IfModifiedSince = conditionalDirective.IfModifiedSince; requestMessage.Headers.IfUnmodifiedSince = conditionalDirective.IfUnmodifiedSince; CopyETags(conditionalDirective.IfMatch, requestMessage.Headers.IfMatch); CopyETags(conditionalDirective.IfNoneMatch, requestMessage.Headers.IfNoneMatch); }
public static CacheRequestDirective ReadCacheRequestDirective(this HttpRequest request) { var result = new CacheRequestDirective(); var typedHeaders = request.GetTypedHeaders(); var CacheControl = typedHeaders.CacheControl; if (CacheControl != null) { result.MaxAge = CacheControl.MaxAge; result.MaxStale = CacheControl.MaxStale ? (CacheControl.MaxStaleLimit ?? TimeSpan.MaxValue) : (TimeSpan?)null; result.MinFresh = CacheControl.MinFresh; result.NoCache = CacheControl.NoCache; result.NoStore = CacheControl.NoStore; result.OnlyIfCached = CacheControl.OnlyIfCached; } else { result.NoCache = request.Headers[HeaderNames.Pragma].ToString() == "no-cache"; } return result; }