public HttpCacheHeadersMiddleware(
            RequestDelegate next,
            IValidationValueStore store,
            ILoggerFactory loggerFactory,
            IOptions <ExpirationModelOptions> expirationModelOptions,
            IOptions <ValidationModelOptions> validationModelOptions)
        {
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            if (validationModelOptions == null)
            {
                throw new ArgumentNullException(nameof(validationModelOptions));
            }

            if (expirationModelOptions == null)
            {
                throw new ArgumentNullException(nameof(expirationModelOptions));
            }

            _next  = next ?? throw new ArgumentNullException(nameof(next));
            _store = store ?? throw new ArgumentNullException(nameof(store));
            _expirationModelOptions = expirationModelOptions.Value;
            _validationModelOptions = validationModelOptions.Value;
            _logger = loggerFactory.CreateLogger <HttpCacheHeadersMiddleware>();
        }
Exemple #2
0
        public HttpCacheHeaderWithExclusionsMiddleware(
            RequestDelegate next,
            ILoggerFactory loggerFactory,
            IDateParser dateParser,
            IValidatorValueStore store,
            IStoreKeyGenerator storeKeyGenerator,
            IETagGenerator eTagGenerator,
            ILastModifiedInjector lastModifiedInjector,
            IOptions <ExpirationModelOptions> expirationModelOptions,
            IOptions <ValidationModelOptions> validationModelOptions,
            IOptions <ExcludedRouteModelOptions> excludedRouteModelOptions)
        {
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            if (validationModelOptions == null)
            {
                throw new ArgumentNullException(nameof(validationModelOptions));
            }

            if (expirationModelOptions == null)
            {
                throw new ArgumentNullException(nameof(expirationModelOptions));
            }

            _next = next ?? throw new ArgumentNullException(nameof(next));

            _dateParser           = dateParser ?? throw new ArgumentNullException(nameof(dateParser));
            _store                = store ?? throw new ArgumentNullException(nameof(store));
            _storeKeyGenerator    = storeKeyGenerator ?? throw new ArgumentNullException(nameof(storeKeyGenerator));
            _eTagGenerator        = eTagGenerator ?? throw new ArgumentNullException(nameof(eTagGenerator));
            _lastModifiedInjector =
                lastModifiedInjector ?? throw new ArgumentNullException(nameof(lastModifiedInjector));

            _expirationModelOptions    = expirationModelOptions.Value;
            _validationModelOptions    = validationModelOptions.Value;
            _excludedRouteModelOptions = excludedRouteModelOptions.Value;

            _logger = loggerFactory.CreateLogger <HttpCacheHeadersMiddleware>();
        }
Exemple #3
0
        private async Task GenerateExpirationHeadersOnResponse(
            HttpContext httpContext,
            ExpirationModelOptions expirationModelOptions,
            ValidationModelOptions validationModelOptions)
        {
            _logger.LogInformation("Generating expiration headers.");

            var headers = httpContext.Response.Headers;

            // remove current Expires & Cache-Control headers
            headers.Remove(HeaderNames.Expires);
            headers.Remove(HeaderNames.CacheControl);

            // set expiration header (remove milliseconds)
            var expires = DateTimeOffset
                          .UtcNow
                          .AddSeconds(expirationModelOptions.MaxAge);

            var expiresValue = await _dateParser.ExpiresToString(expires);

            headers[HeaderNames.Expires] = expiresValue;

            var cacheControlHeaderValue = string.Format(
                CultureInfo.InvariantCulture,
                "{0},max-age={1}{2}{3}{4}{5}{6}{7}{8}",
                expirationModelOptions.CacheLocation.ToString().ToLowerInvariant(),
                expirationModelOptions.MaxAge,
                expirationModelOptions.SharedMaxAge == null ? null : ",s-maxage=",
                expirationModelOptions.SharedMaxAge,
                expirationModelOptions.NoStore ? ",no-store" : null,
                expirationModelOptions.NoTransform ? ",no-transform" : null,
                validationModelOptions.NoCache ? ",no-cache" : null,
                validationModelOptions.MustRevalidate ? ",must-revalidate" : null,
                validationModelOptions.ProxyRevalidate ? ",proxy-revalidate" : null);

            headers[HeaderNames.CacheControl] = cacheControlHeaderValue;

            _logger.LogInformation(
                $"Expiration headers generated. Expires: {expiresValue}. Cache-Control: {cacheControlHeaderValue}.");
        }