Skip to content

Instantly share code, notes, and snippets.

@madskristensen
Last active March 18, 2024 15:11
Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save madskristensen/36357b1df9ddbfd123162cd4201124c4 to your computer and use it in GitHub Desktop.
Save madskristensen/36357b1df9ddbfd123162cd4201124c4 to your computer and use it in GitHub Desktop.
ASP.NET Core ETAg middleware
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Net.Http.Headers;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
public class ETagMiddleware
{
private readonly RequestDelegate _next;
public ETagMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var response = context.Response;
var originalStream = response.Body;
using (var ms = new MemoryStream())
{
response.Body = ms;
await _next(context);
if (IsEtagSupported(response))
{
string checksum = CalculateChecksum(ms);
response.Headers[HeaderNames.ETag] = checksum;
if (context.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etag) && checksum == etag)
{
response.StatusCode = StatusCodes.Status304NotModified;
return;
}
}
ms.Position = 0;
await ms.CopyToAsync(originalStream);
}
}
private static bool IsEtagSupported(HttpResponse response)
{
if (response.StatusCode != StatusCodes.Status200OK)
return false;
// The 20kb length limit is not based in science. Feel free to change
if (response.Body.Length > 20 * 1024)
return false;
if (response.Headers.ContainsKey(HeaderNames.ETag))
return false;
return true;
}
private static string CalculateChecksum(MemoryStream ms)
{
string checksum = "";
using (var algo = SHA1.Create())
{
ms.Position = 0;
byte[] bytes = algo.ComputeHash(ms);
checksum = $"\"{WebEncoders.Base64UrlEncode(bytes)}\"";
}
return checksum;
}
}
public static class ApplicationBuilderExtensions
{
public static void UseETagger(this IApplicationBuilder app)
{
app.UseMiddleware<ETagMiddleware>();
}
}
// Add "app.UseETagger();" to "Configure" method in Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
// Add this after static files but before MVC in order to provide ETags to MVC Views and Razor Pages.
app.UseETagger();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
@Hintalo
Copy link

Hintalo commented May 5, 2020

@zuckerthoben - it's the server who sends the very first etag (i.e. version field from optimistic concurrency control mechanism) related to requested resource, so the server has ability to check if the resource has changed in a more effective way than calculating hash from already prepared response.

Exactly. Saving the network bandwidth only is a partial solution that has no benefit for REST calls that have heavy computational logic on the server or trigger expensive database queries. A true cache solution on the server-side should be able to calculate the ETAG efficiently, without generating a response.

@vivekkjain
Copy link

Do we really have to use the Response Body to calculate the ETag. I would recommend to avoid any logic in the Middleware based on the Response Body, Instead I would calculate the Etag on API or Page controller and pass it to Response and use this middleware to add Header only.

@DerGuru
Copy link

DerGuru commented Mar 18, 2024

@Memnarch But how can the Server know if the result he wants to return is already the one the client has cached? The ETag saves you the bandwith to actually transfer the data from the server to the client. The backend therefore saves resources because he can answer more efficiently, even though he had to process the content of the answer beforehand. If you configured the cache to store and it is not yet expired, there will be no connection made to the server. If it is expired, but the ETag stays the same, the client can refresh the cache with the specified max age of the server and server does not have to actually send the data again.

Also, if you cache the result on server side, using the ETAG as key, then you also save resources on the server side. If the request is cachable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment