make etag a middleware before output cache
This commit is contained in:
@@ -11,24 +11,10 @@ namespace TerribleDev.Blog.Web.Filters
|
||||
{
|
||||
public static string staticEtag = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString())).ToHexString().Substring(0,8);
|
||||
public static ConcurrentDictionary<string, string> cache = new ConcurrentDictionary<string, string>();
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
var etag = context.HttpContext.Request.Headers["If-None-Match"];
|
||||
if(etag == staticEtag)
|
||||
{
|
||||
Console.WriteLine("ETAG HIT");
|
||||
context.Result = new StatusCodeResult(304);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.OnActionExecuting(context);
|
||||
}
|
||||
}
|
||||
public override void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
if(context.HttpContext.Response.StatusCode >= 200 && context.HttpContext.Response.StatusCode < 300 && context.HttpContext.Response.Headers.ETag.Count == 0)
|
||||
{
|
||||
Console.WriteLine("ETAG MISS");
|
||||
context.HttpContext.Response.Headers.Add("ETag", staticEtag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,6 +141,16 @@ namespace TerribleDev.Blog.Web
|
||||
// },
|
||||
UpgradeInsecureRequests = true
|
||||
});
|
||||
app.Use(async (context, next) => {
|
||||
var etag = context.Request.Headers.IfNoneMatch.ToString();
|
||||
if(etag != null && string.Equals(etag, StaticETag.staticEtag, StringComparison.Ordinal))
|
||||
{
|
||||
context.Response.StatusCode = 304;
|
||||
await context.Response.CompleteAsync();
|
||||
return;
|
||||
}
|
||||
await next();
|
||||
});
|
||||
if(Env.IsProduction())
|
||||
{
|
||||
app.UseOutputCaching();
|
||||
|
||||
Reference in New Issue
Block a user