Пример #1
0
        internal static StoredHttpResponseMessage Create(HttpResponseMessage response, byte[] content)
        {
            var retval = new StoredHttpResponseMessage
            {
                Content    = content,
                StatusCode = response.StatusCode,
                Version    = response.Version?.ToString(),
            };

            if (response.Headers != null)
            {
                foreach (var header in response.Headers)
                {
                    retval.Headers.Add(new Tuple <string, string[]>(header.Key, header.Value.ToArray()));
                }
            }


            if (response.Content != null)
            {
                foreach (var cheader in response.Content?.Headers)
                {
                    retval.ContentHeaders.Add(new Tuple <string, string[]>(cheader.Key, cheader.Value.ToArray()));
                }
            }


            return(retval);
        }
Пример #2
0
        private async Task <HttpResponseMessage> HandleServerResponse(HttpRequestMessage request, HttpResponseMessage serverResponse, string cacheKey, StoredHttpResponseMessage storedCachedResponse, CacheStatus status)
        {
            if (serverResponse == null)
            {
                return(null);
            }

            var updatedCacheKey = _keyStrategy.GetCacheKey(request, serverResponse);


            var wasRevalidated      = status == CacheStatus.Revalidated;
            var isResponseCacheable = IsResponseCacheable(serverResponse);

            if (wasRevalidated)
            {
                if (serverResponse.StatusCode == HttpStatusCode.NotModified)
                {
                    var cachedResponse = storedCachedResponse.CopyIntoResponseMessage(request);
                    cachedResponse.Headers.CacheControl = serverResponse.Headers.CacheControl;
                    cachedResponse.RequestMessage       = request;
                    cachedResponse.Headers.Date         = SystemClock.UtcNow;

                    _cache.Put(cacheKey, storedCachedResponse);

                    //We need to dispose the response from the server since we're not returning it and because of that the pipeline will not dispose it.
                    serverResponse.Dispose();
                    cachedResponse.Headers.AddClientCacheStatusHeader(status);
                    return(cachedResponse);
                }

                if (isResponseCacheable)
                {
                    _cache.Remove(cacheKey);
                    var serializedResponse = await StoredHttpResponseMessage.Create(serverResponse).ConfigureAwait(false);

                    _cache.Put(updatedCacheKey, serializedResponse);
                }
            }
            else if (isResponseCacheable)
            {
                var serializedResponse = await StoredHttpResponseMessage.Create(serverResponse).ConfigureAwait(false);

                _cache.Put(updatedCacheKey, serializedResponse);
            }
            serverResponse.Headers.AddClientCacheStatusHeader(status);

            return(serverResponse);
        }