Exemple #1
0
        private void CheckValidTryParse(string input, CacheControlHeaderValue expectedResult)
        {
            CacheControlHeaderValue result = null;

            Assert.True(CacheControlHeaderValue.TryParse(input, out result));
            Assert.Equal(expectedResult, result);
        }
Exemple #2
0
        private void CheckInvalidTryParse(string input, int startIndex)
        {
            CacheControlHeaderValue result = null;

            Assert.False(CacheControlHeaderValue.TryParse(input, out result));
            Assert.Null(result);
        }
        private void LogCacheHeaderOverrideWarning(HttpResponse response)
        {
            var logWarning = false;
            CacheControlHeaderValue cacheControlHeaderValue;

            if (CacheControlHeaderValue.TryParse(response.Headers[HeaderNames.CacheControl].ToString(), out cacheControlHeaderValue))
            {
                if (!cacheControlHeaderValue.NoCache)
                {
                    logWarning = true;
                }
            }

            var pragmaHeader = response.Headers[HeaderNames.Pragma];

            if (!logWarning &&
                !string.IsNullOrEmpty(pragmaHeader) &&
                string.Compare(pragmaHeader, "no-cache", ignoreCase: true) != 0)
            {
                logWarning = true;
            }

            if (logWarning)
            {
                _logger.ResponseCacheHeadersOverridenToNoCache();
            }
        }
        public void TryParse()
        {
            CacheControlHeaderValue res;

            Assert.IsTrue(CacheControlHeaderValue.TryParse("*", out res), "#1");
            Assert.AreEqual(1, res.Extensions.Count, "#2");
        }
        private void AddToCache(string resource, HttpResponseMessage response)
        {
            var cacheHeader = response.Headers.FirstOrDefault(h => h.Key == "cache-control");

            if (!string.IsNullOrEmpty(cacheHeader.Key) &&
                CacheControlHeaderValue.TryParse(cacheHeader.Value.ToString(), out var cacheControl) &&
                cacheControl?.MaxAge.HasValue is true)
            {
                this.cache.Add(resource, response, cacheControl.MaxAge !.Value);
            }
        }
Exemple #6
0
        public static void initialHttpRequestMessage(OssHttpRequestMessage hReqMes, ObjectMetadata metadata)
        {
            if (metadata.CacheControl != null)
            {
                CacheControlHeaderValue temp;
                if (CacheControlHeaderValue.TryParse(metadata.CacheControl, out temp) == true)
                {
                    hReqMes.Headers.CacheControl = temp;
                }
            }

            if (metadata.ContentDisposition != null)
            {
                ContentDispositionHeaderValue temp;
                if (ContentDispositionHeaderValue.TryParse(metadata.CacheControl, out temp) == true)
                {
                    hReqMes.Content.Headers.ContentDisposition = temp;
                }
            }

            if (metadata.ContentLength != 0)
            {
                hReqMes.Content.Headers.ContentLength = metadata.ContentLength;
            }

            if (metadata.ContentType != null)
            {
                MediaTypeHeaderValue temp;
                if (MediaTypeHeaderValue.TryParse(metadata.CacheControl, out temp) == true)
                {
                    hReqMes.Content.Headers.ContentType = temp;
                }
                else
                {
                    hReqMes.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                }
            }
            else
            {
                hReqMes.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            }

            //if (metadata.ExpirationTime != null)
            //{
            //    hReqMes.Content.Headers.Expires = metadata.ExpirationTime;
            //}

            foreach (KeyValuePair <string, string> entry in metadata.UserMetadata)
            {
                hReqMes.Headers.Pragma.Add(new NameValueHeaderValue("x-oss-meta-" + entry.Key, entry.Value));
            }
        }
Exemple #7
0
        private static TimeSpan?GetCacheTtl(RequestContext requestContext)
        {
            var response = requestContext.Response;
            // Only consider kernel-mode caching if the Cache-Control response header is present.
            var cacheControlHeader = response.Headers[HeaderNames.CacheControl];

            if (string.IsNullOrEmpty(cacheControlHeader))
            {
                return(null);
            }

            // Before we check the header value, check for the existence of other headers which would
            // make us *not* want to cache the response.
            if (response.Headers.ContainsKey(HeaderNames.SetCookie) ||
                response.Headers.ContainsKey(HeaderNames.Vary) ||
                response.Headers.ContainsKey(HeaderNames.Pragma))
            {
                return(null);
            }

            // We require 'public' and 's-max-age' or 'max-age' or the Expires header.
            CacheControlHeaderValue cacheControl;

            if (CacheControlHeaderValue.TryParse(cacheControlHeader, out cacheControl) && cacheControl.Public)
            {
                if (cacheControl.SharedMaxAge.HasValue)
                {
                    return(cacheControl.SharedMaxAge);
                }
                else if (cacheControl.MaxAge.HasValue)
                {
                    return(cacheControl.MaxAge);
                }

                DateTimeOffset expirationDate;
                if (HeaderUtilities.TryParseDate(response.Headers[HeaderNames.Expires], out expirationDate))
                {
                    var expiresOffset = expirationDate - DateTimeOffset.UtcNow;
                    if (expiresOffset > TimeSpan.Zero)
                    {
                        return(expiresOffset);
                    }
                }
            }

            return(null);
        }
Exemple #8
0
        public static CacheControlHeaderValue GetWatCacheControlHeader(this HttpRequestMessage request)
        {
            IEnumerable <string> headerValues;

            if (request.Headers.TryGetValues(CacheControlHeaderName, out headerValues))
            {
                foreach (var header in headerValues)
                {
                    CacheControlHeaderValue cacheControlHeader;
                    if (CacheControlHeaderValue.TryParse(header, out cacheControlHeader))
                    {
                        return(cacheControlHeader);
                    }
                }
            }

            return(new CacheControlHeaderValue());
        }
        /// <inheritdoc />
        public async Task <ImageMetadata> GetMetaDataAsync()
        {
            GetObjectMetadataResponse metadata = await this.amazonS3.GetObjectMetadataAsync(this.bucketName, this.imagePath);

            // Try to parse the max age from the source. If it's not zero then we pass it along
            // to set the cache control headers for the response.
            TimeSpan maxAge = TimeSpan.MinValue;

            if (CacheControlHeaderValue.TryParse(metadata.Headers.CacheControl, out CacheControlHeaderValue cacheControl))
            {
                // Weirdly passing null to TryParse returns true.
                if (cacheControl?.MaxAge.HasValue == true)
                {
                    maxAge = cacheControl.MaxAge.Value;
                }
            }

            return(new ImageMetadata(metadata.LastModified, maxAge, metadata.ContentLength));
        }
    /// <summary>
    /// Sets the 'Cache-Control' header to 'no-cache, no-store' and 'Pragma' header to 'no-cache' overriding any user set value.
    /// </summary>
    /// <param name="httpContext">The <see cref="HttpContext"/>.</param>
    protected virtual void SetDoNotCacheHeaders(HttpContext httpContext)
    {
        var logWarning      = false;
        var responseHeaders = httpContext.Response.Headers;

        if (responseHeaders.TryGetValue(HeaderNames.CacheControl, out var cacheControlHeader) &&
            CacheControlHeaderValue.TryParse(cacheControlHeader.ToString(), out var cacheControlHeaderValue))
        {
            // If the Cache-Control is already set, override it only if required
            if (!cacheControlHeaderValue.NoCache || !cacheControlHeaderValue.NoStore)
            {
                logWarning = true;
                responseHeaders.CacheControl = "no-cache, no-store";
            }
        }
        else
        {
            responseHeaders.CacheControl = "no-cache, no-store";
        }

        if (responseHeaders.TryGetValue(HeaderNames.Pragma, out var pragmaHeader) && pragmaHeader.Count > 0)
        {
            // If the Pragma is already set, override it only if required
            if (!string.Equals(pragmaHeader[0], "no-cache", StringComparison.OrdinalIgnoreCase))
            {
                logWarning = true;
                httpContext.Response.Headers.Pragma = "no-cache";
            }
        }
        else
        {
            httpContext.Response.Headers.Pragma = "no-cache";
        }

        // Since antiforgery token generation is not very obvious to the end users (ex: MVC's form tag generates them
        // by default), log a warning to let users know of the change in behavior to any cache headers they might
        // have set explicitly.
        if (logWarning)
        {
            _logger.ResponseCacheHeadersOverridenToNoCache();
        }
    }
 private void CheckInvalidTryParse(string?input)
 {
     Assert.False(CacheControlHeaderValue.TryParse(input, out var result));
     Assert.Null(result);
 }