private void Generate412PreconditionFailedResponse(HttpContext httpContext) { var headers = httpContext.Response.Headers; httpContext.Response.StatusCode = StatusCodes.Status412PreconditionFailed; // set the ETag & Last-Modified date. // remove any other ETag and Last-Modified headers (could be set // by other pieces of code) headers.Remove(HeaderNames.ETag); headers.Remove(HeaderNames.LastModified); // generate key, ETag and LastModified var requestKey = GenerateRequestKey(httpContext.Request); // strong eTags by default! var eTag = new ETag(ETagType.Strong, httpContext.Request.Headers[HeaderNames.IfNoneMatch].ToString()); var lastModified = DateTimeOffset.UtcNow; // store (overwrite) _store.SetAsync(requestKey, new ValidationValue(eTag, lastModified)); // set headers headers[HeaderNames.ETag] = eTag.Value; headers[HeaderNames.LastModified] = lastModified.ToString("r", CultureInfo.InvariantCulture); }
private async Task GenerateResponseFromStore(HttpContext httpContext) { var headers = httpContext.Response.Headers; // set the ETag & Last-Modified date. // remove any other ETag and Last-Modified headers (could be set // by other pieces of code) headers.Remove(HeaderNames.ETag); headers.Remove(HeaderNames.LastModified); // generate key var requestKey = GenerateRequestKey(httpContext.Request); // set LastModified // r = RFC1123 pattern (https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx) var lastModified = GetUtcNowWithoutMilliseconds(); headers[HeaderNames.LastModified] = lastModified.ToString("r", CultureInfo.InvariantCulture); ETag eTag = null; // take ETag value from the store (if it's found) var savedResponse = await _store.GetAsync(requestKey); if (savedResponse?.ETag != null) { eTag = new ETag(savedResponse.ETag.ETagType, savedResponse.ETag.Value); headers[HeaderNames.ETag] = savedResponse.ETag.Value; } // store (overwrite) await _store.SetAsync(requestKey, new ValidationValue(eTag, lastModified)); var logInformation = string.Empty; if (eTag != null) { logInformation = $"ETag: {eTag.ETagType.ToString()}, {eTag.Value}, "; } logInformation += $"Last-Modified: {lastModified.ToString("r", CultureInfo.InvariantCulture)}."; _logger.LogInformation($"Generation done. {logInformation}"); }