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 @@ -136,7 +136,7 @@ internal static string BuildEtag(string timeStamp, string? suffix)
return $"\"{AssemblyWriteTime}-{timeStamp}-{suffix}\"";
}
```
<sup><a href='/src/Delta/DeltaExtensions_Shared.cs#L183-L195' 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#L182-L194' title='Snippet source file'>snippet source</a> | <a href='#snippet-BuildEtag' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
25 changes: 10 additions & 15 deletions src/Delta.EF/DeltaExtensions_EFMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ public static IApplicationBuilder UseDelta<TDbContext>(this IApplicationBuilder
where TDbContext : DbContext
{
var logger = builder.ApplicationServices.GetLogger();

static Task<string> GetTimeStamp(HttpContext context) =>
context.RequestServices.GetRequiredService<TDbContext>().GetLastTimeStamp();

return builder.Use(
async (context, next) =>
{
if (await HandleRequest<TDbContext>(context, logger, suffix, shouldExecute, logLevel, allowAnonymous))
if (await HandleRequest(context, logger, suffix, GetTimeStamp, shouldExecute, logLevel, allowAnonymous))
{
return;
}
Expand All @@ -24,9 +28,13 @@ static TBuilder UseDelta<TBuilder, TDbContext>(this TBuilder builder, Func<HttpC
builder.AddEndpointFilterFactory((filterContext, next) =>
{
var logger = filterContext.ApplicationServices.GetLogger();

static Task<string> GetTimeStamp(HttpContext context) =>
context.RequestServices.GetRequiredService<TDbContext>().GetLastTimeStamp();

return async invocationContext =>
{
if (await HandleRequest<TDbContext>(invocationContext.HttpContext, logger, suffix, shouldExecute, logLevel, allowAnonymous))
if (await HandleRequest(invocationContext.HttpContext, logger, suffix, GetTimeStamp, shouldExecute, logLevel, allowAnonymous))
{
return Results.Empty;
}
Expand All @@ -35,19 +43,6 @@ static TBuilder UseDelta<TBuilder, TDbContext>(this TBuilder builder, Func<HttpC
};
});

static Task<bool> HandleRequest<T>(HttpContext context, ILogger logger, Func<HttpContext, string?>? suffix, Func<HttpContext, bool>? shouldExecute, LogLevel logLevel, bool allowAnonymous = false)
where T : DbContext =>
HandleRequest(
context,
logger,
suffix,
_ => _.RequestServices
.GetRequiredService<T>()
.GetLastTimeStamp(),
shouldExecute,
logLevel,
allowAnonymous);

public static Task<string> GetLastTimeStamp(this DbContext context, Cancel cancel = default)
{
var database = context.Database;
Expand Down
39 changes: 16 additions & 23 deletions src/Delta/DeltaExtensions_Middleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ public static IApplicationBuilder UseDelta(this IApplicationBuilder builder, Get
{
getConnection ??= DiscoverConnection;
var logger = builder.ApplicationServices.GetLogger();

Task<string> GetTimeStamp(HttpContext context)
{
var (connection, transaction) = getConnection(context);
return connection.GetLastTimeStamp(transaction);
}

return builder.Use(
async (context, next) =>
{
if (await HandleRequest(context, getConnection, logger, suffix, shouldExecute, logLevel, allowAnonymous))
if (await HandleRequest(context, logger, suffix, GetTimeStamp, shouldExecute, logLevel, allowAnonymous))
{
return;
}
Expand All @@ -26,9 +33,16 @@ static TBuilder UseDelta<TBuilder>(this TBuilder builder, GetConnection? getConn
return builder.AddEndpointFilterFactory((filterContext, next) =>
{
var logger = filterContext.ApplicationServices.GetLogger();

Task<string> GetTimeStamp(HttpContext context)
{
var (connection, transaction) = getConnection(context);
return connection.GetLastTimeStamp(transaction);
}

return async invocationContext =>
{
if (await HandleRequest(invocationContext.HttpContext, getConnection, logger, suffix, shouldExecute, logLevel, allowAnonymous))
if (await HandleRequest(invocationContext.HttpContext, logger, suffix, GetTimeStamp, shouldExecute, logLevel, allowAnonymous))
{
return Results.Empty;
}
Expand All @@ -38,27 +52,6 @@ static TBuilder UseDelta<TBuilder>(this TBuilder builder, GetConnection? getConn
});
}

static Task<bool> HandleRequest(
HttpContext context,
GetConnection getConnection,
ILogger logger,
Func<HttpContext, string?>? suffix,
Func<HttpContext, bool>? shouldExecute,
LogLevel logLevel,
bool allowAnonymous = false) =>
HandleRequest(
context,
logger,
suffix,
_ =>
{
var (connection, transaction) = getConnection(_);
return connection.GetLastTimeStamp(transaction);
},
shouldExecute,
logLevel,
allowAnonymous);

public static ComponentEndpointConventionBuilder UseDelta(
this ComponentEndpointConventionBuilder builder,
GetConnection? getConnection = null,
Expand Down
7 changes: 3 additions & 4 deletions src/Delta/DeltaExtensions_Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ internal static async Task<bool> HandleRequest(HttpContext context, ILogger logg
var path = request.Path;

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

Expand Down Expand Up @@ -123,7 +122,7 @@ Ensure authentication middleware runs before UseDelta so that User claims are av
}
else
{
reason = $"Request has no If-None-Match. Request also has Cache-Control header ({cacheControl}) which can interfere with caching";
reason = "Request has no If-None-Match. Request Cache-Control header may interfere with caching";
}

WriteNo304Header(response, reason, level, logger, path, logEnabled);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
{
target: {
notModified: false,
context: {
Request: {},
Response: {
StatusCode: OK,
Headers: {
Delta-No304: Request Method=POST
Delta-No304: Request method is not GET
}
}
}
},
log: {
Information: Delta /path: No 304. Request Method=POST,
Information: Delta /path: No 304. Request method is not GET,
State: [
{
path: /path
},
{
reason: Request Method=POST
reason: Request method is not GET
},
{
{OriginalFormat}: Delta {path}: No 304. {reason}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -10,19 +10,19 @@
Response: {
StatusCode: OK,
Headers: {
Delta-No304: Request Method=POST
Delta-No304: Request method is not GET
}
}
}
},
log: {
Information: Delta /path: No 304. Request Method=POST,
Information: Delta /path: No 304. Request method is not GET,
State: [
{
path: /path
},
{
reason: Request Method=POST
reason: Request method is not GET
},
{
{OriginalFormat}: Delta {path}: No 304. {reason}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -7,19 +7,19 @@
StatusCode: OK,
Headers: {
Cache-Control: public, max-age=31536000, immutable,
Delta-No304: Request Method=POST
Delta-No304: Request method is not GET
}
}
}
},
log: {
Information: Delta /path: No 304. Request Method=POST,
Information: Delta /path: No 304. Request method is not GET,
State: [
{
path: /path
},
{
reason: Request Method=POST
reason: Request method is not GET
},
{
{OriginalFormat}: Delta {path}: No 304. {reason}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -11,19 +11,19 @@
StatusCode: OK,
Headers: {
Cache-Control: public, max-age=31536000, immutable,
Delta-No304: Request Method=POST
Delta-No304: Request method is not GET
}
}
}
},
log: {
Information: Delta /path: No 304. Request Method=POST,
Information: Delta /path: No 304. Request method is not GET,
State: [
{
path: /path
},
{
reason: Request Method=POST
reason: Request method is not GET
},
{
{OriginalFormat}: Delta {path}: No 304. {reason}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
{
target: {
notModified: false,
context: {
Request: {},
Response: {
StatusCode: OK,
Headers: {
Delta-No304: Request Method=POST
Delta-No304: Request method is not GET
}
}
}
},
log: {
Information: Delta /path: No 304. Request Method=POST,
Information: Delta /path: No 304. Request method is not GET,
State: [
{
path: /path
},
{
reason: Request Method=POST
reason: Request method is not GET
},
{
{OriginalFormat}: Delta {path}: No 304. {reason}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -10,19 +10,19 @@
Response: {
StatusCode: OK,
Headers: {
Delta-No304: Request Method=POST
Delta-No304: Request method is not GET
}
}
}
},
log: {
Information: Delta /path: No 304. Request Method=POST,
Information: Delta /path: No 304. Request method is not GET,
State: [
{
path: /path
},
{
reason: Request Method=POST
reason: Request method is not GET
},
{
{OriginalFormat}: Delta {path}: No 304. {reason}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -7,19 +7,19 @@
StatusCode: OK,
Headers: {
Cache-Control: public, max-age=31536000, immutable,
Delta-No304: Request Method=POST
Delta-No304: Request method is not GET
}
}
}
},
log: {
Information: Delta /path: No 304. Request Method=POST,
Information: Delta /path: No 304. Request method is not GET,
State: [
{
path: /path
},
{
reason: Request Method=POST
reason: Request method is not GET
},
{
{OriginalFormat}: Delta {path}: No 304. {reason}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
target: {
notModified: false,
context: {
Expand All @@ -11,19 +11,19 @@
StatusCode: OK,
Headers: {
Cache-Control: public, max-age=31536000, immutable,
Delta-No304: Request Method=POST
Delta-No304: Request method is not GET
}
}
}
},
log: {
Information: Delta /path: No 304. Request Method=POST,
Information: Delta /path: No 304. Request method is not GET,
State: [
{
path: /path
},
{
reason: Request Method=POST
reason: Request method is not GET
},
{
{OriginalFormat}: Delta {path}: No 304. {reason}
Expand Down
Loading