Exemple #1
0
        private async Task <bool> CheckIfUnmodifiedSinceIsValid(HttpContext httpContext, ValidatorValue validatorValue)
        {
            // Either the ETag matches (or one of them), or there was no IfMatch header.
            // Continue with checking the IfUnModifiedSince header, if it exists.
            if (httpContext.Request.Headers.Keys.Contains(HeaderNames.IfUnmodifiedSince))
            {
                // if the LastModified date is smaller than the IfUnmodifiedSince date,
                // the precondition is valid.
                var ifUnmodifiedSinceValue = httpContext.Request.Headers[HeaderNames.IfUnmodifiedSince].ToString();
                _logger.LogInformation($"Checking If-Unmodified-Since: {ifUnmodifiedSinceValue}");

                var parsedIfUnmodifiedSince = await _dateParser.IfUnmodifiedSinceToDateTimeOffset(ifUnmodifiedSinceValue);

                if (parsedIfUnmodifiedSince.HasValue)
                {
                    // If the LastModified date is smaller than the IfUnmodifiedSince date,
                    // the precondition is valid.
                    return(validatorValue.LastModified.CompareTo(parsedIfUnmodifiedSince.Value) < 0);
                }

                // can only check if we can parse it. Invalid values must be ignored.
                _logger.LogInformation("Cannot parse If-Unmodified-Since value as date, header is ignored.");
                return(true);
            }

            // if there is no IfUnmodifiedSince header, the check is valid.
            _logger.LogInformation("No If-Unmodified-Since header.");
            return(true);
        }
Exemple #2
0
        private async Task <bool> CheckIfModifiedSinceIsValid(HttpContext httpContext, ValidatorValue validatorValue)
        {
            if (httpContext.Request.Headers.Keys.Contains(HeaderNames.IfModifiedSince))
            {
                // if the LastModified date is smaller than the IfModifiedSince date,
                // we can return a 304 Not Modified (If there's also a matching ETag).
                // By adding an If-Modified-Since date
                // to a GET/HEAD request, the consumer is stating that (s)he only wants the resource
                // to be returned if if has been modified after that.
                var ifModifiedSinceValue = httpContext.Request.Headers[HeaderNames.IfModifiedSince].ToString();
                _logger.LogInformation($"Checking If-Modified-Since: {ifModifiedSinceValue}");

                var parsedIfModifiedSince = await _dateParser.IfModifiedSinceToDateTimeOffset(ifModifiedSinceValue);

                if (parsedIfModifiedSince.HasValue)
                {
                    return(validatorValue.LastModified.CompareTo(parsedIfModifiedSince.Value) <= 0);
                }

                // can only check if we can parse it. Invalid values must be ignored.
                _logger.LogInformation("Cannot parse If-Modified-Since value as date, header is ignored.");
                return(true);
            }

            // if there is no IfModifiedSince header, the check is valid.
            _logger.LogInformation("No If-Modified-Since header.");
            return(true);
        }