Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ internal static string BuildEtag(string timeStamp, string? suffix)
return $"\"{AssemblyWriteTime}-{timeStamp}-{suffix}\"";
}
```
<sup><a href='/src/Delta/DeltaExtensions_Shared.cs#L182-L194' title='Snippet source file'>snippet source</a> | <a href='#snippet-BuildEtag' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Delta/DeltaExtensions_Shared.cs#L172-L184' title='Snippet source file'>snippet source</a> | <a href='#snippet-BuildEtag' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
56 changes: 23 additions & 33 deletions src/Delta/DeltaExtensions_Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,28 @@ internal static async Task<bool> HandleRequest(HttpContext context, ILogger logg
var response = context.Response;
var path = request.Path.Value!;

var logEnabled = logger.IsEnabled(level);
if (!HttpMethods.IsGet(request.Method))
{
WriteNo304Header(response, "Request method is not GET", level, logger, path, logEnabled);
WriteNo304Header(response, "Request method is not GET", level, logger, path);
return false;
}

if (response.Headers.ETag.Count != 0)
{
WriteNo304Header(response, "Response already has ETag", level, logger, path, logEnabled);
WriteNo304Header(response, "Response already has ETag", level, logger, path);
return false;
}

if (response.IsImmutableCache())
{
WriteNo304Header(response, "Response already has Cache-Control=immutable", level, logger, path, logEnabled);
WriteNo304Header(response, "Response already has Cache-Control=immutable", level, logger, path);
return false;
}

if (shouldExecute != null &&
!shouldExecute(context))
{
WriteNo304Header(response, "shouldExecute=false", level, logger, path, logEnabled);
WriteNo304Header(response, "shouldExecute=false", level, logger, path);
return false;
}

Expand All @@ -107,10 +106,7 @@ Ensure authentication middleware runs before UseDelta so that User claims are av
var suffixValue = suffix?.Invoke(context);
var etag = BuildEtag(timeStamp, suffixValue);
response.Headers.ETag = etag;
if (logEnabled)
{
logger.Log(level, "Delta {path}: ETag {etag}", path, etag);
}
LogEtag(logger, level, path, etag);

if (!request.Headers.TryGetValue("If-None-Match", out var ifNoneMatch))
{
Expand All @@ -125,54 +121,48 @@ Ensure authentication middleware runs before UseDelta so that User claims are av
reason = "Request has no If-None-Match. Request Cache-Control header may interfere with caching";
}

WriteNo304Header(response, reason, level, logger, path, logEnabled);
WriteNo304Header(response, reason, level, logger, path);

return false;
}

if (ifNoneMatch != etag)
{
WriteNo304Header(response, "Request If-None-Match != ETag", level, logger, path, logEnabled);
if (logEnabled)
{
logger.Log(
level,
"""
Delta {path}: No 304. Request If-None-Match != ETag
If-None-Match: {ifNoneMatch}
ETag: {etag}
""",
path,
ifNoneMatch.ToString(),
etag);
}
WriteNo304Header(response, "Request If-None-Match != ETag", level, logger, path);
LogEtagMismatch(logger, level, path, ifNoneMatch.ToString(), etag);

return false;
}

if (logEnabled)
{
logger.Log(level, "Delta {path}: 304", path);
}
Log304(logger, level, path);

response.StatusCode = 304;
response.NoCache();
return true;
}

static void WriteNo304Header(HttpResponse response, string reason, LogLevel level, ILogger logger, PathString path, bool logEnabled)
static void WriteNo304Header(HttpResponse response, string reason, LogLevel level, ILogger logger, string path)
{
if (UseResponseDiagnostics)
{
response.Headers["Delta-No304"] = reason;
}

if (logEnabled)
{
logger.Log(level, "Delta {path}: No 304. {reason}", path.Value, reason);
}
LogNo304(logger, level, path, reason);
}

[LoggerMessage(Message = "Delta {path}: ETag {etag}")]
static partial void LogEtag(ILogger logger, LogLevel level, string path, string etag);

[LoggerMessage(Message = "Delta {path}: No 304. {reason}")]
static partial void LogNo304(ILogger logger, LogLevel level, string path, string reason);

[LoggerMessage(Message = "Delta {path}: No 304. Request If-None-Match != ETag\nIf-None-Match: {ifNoneMatch}\nETag: {etag}")]
static partial void LogEtagMismatch(ILogger logger, LogLevel level, string path, string ifNoneMatch, string etag);

[LoggerMessage(Message = "Delta {path}: 304")]
static partial void Log304(ILogger logger, LogLevel level, string path);

static ILogger GetLogger(this IServiceProvider provider)
{
var factory = provider.GetRequiredService<ILoggerFactory>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -13,6 +13,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -17,6 +17,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -14,6 +14,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -18,6 +18,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -13,6 +13,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -17,6 +17,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -14,6 +14,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -18,6 +18,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -13,6 +13,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -17,6 +17,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -14,6 +14,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -18,6 +18,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -13,6 +13,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -17,6 +17,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -14,6 +14,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -18,6 +18,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -14,6 +14,9 @@
},
log: {
Information: Delta /path: No 304. Request method is not GET,
EventId: {
Name: LogNo304
},
State: [
{
path: /path
Expand Down
Loading