#actix #etag #actix-web


ETag middleware for Actix web >= 4.1

4 releases (2 breaking)

new 0.3.0 Nov 23, 2023
0.2.0 Feb 1, 2023
0.1.1 Sep 29, 2022
0.1.0 Sep 29, 2022

#269 in HTTP server

Download history 37/week @ 2023-08-04 69/week @ 2023-08-11 56/week @ 2023-08-18 49/week @ 2023-08-25 42/week @ 2023-09-01 83/week @ 2023-09-08 47/week @ 2023-09-15 34/week @ 2023-09-22 18/week @ 2023-09-29 91/week @ 2023-10-06 93/week @ 2023-10-13 68/week @ 2023-10-20 123/week @ 2023-10-27 83/week @ 2023-11-03 47/week @ 2023-11-10 83/week @ 2023-11-17

352 downloads per month
Used in unleash-edge

MIT license

274 lines

ETag middleware for Actix web

To use

use actix_web::{web, App, HttpServer, HttpResponse, Error};
use actix_middleware_etag::{Etag};
async fn main() -> std::io::Result<()> {
    HttpServer::new(move ||
            // Add etag headers to your actix application. Calculating the hash of your GET bodies and putting the base64 hash in the ETag header
        .bind(("", 8080))?

This will hash all bodies for GET requests and base64 encode the hash as a weak ETag header in the response


Actix Middleware - ETag

To avoid sending unnecessary bodies downstream, this middleware handles comparing If-None-Match headers to the calculated hash of the body of the GET request. Inspired by Node's express framework and how it does ETag calculation, this middleware behaves in a similar fashion.

First hash the resulting body, then base64 encode the hash and set this as the ETag header for the GET request.

This does not save CPU resources on server side, since the body is still being calculated.

Beware: This middleware does not look at headers, so if you need to refresh your headers even if body is exactly the same, use something else (or better yet, add a PR on this repo adding a sane way to adhere to headers as well)


~520K SLoC