7 unstable releases

0.4.2 Sep 3, 2024
0.4.1 Jul 11, 2024
0.3.0 Nov 23, 2023
0.2.0 Feb 1, 2023
0.1.1 Sep 29, 2022

#232 in HTTP server

Download history 282/week @ 2024-07-11 63/week @ 2024-07-18 81/week @ 2024-07-25 45/week @ 2024-08-01 36/week @ 2024-08-08 39/week @ 2024-08-15 44/week @ 2024-08-22 259/week @ 2024-08-29 165/week @ 2024-09-05 185/week @ 2024-09-12 307/week @ 2024-09-19 212/week @ 2024-09-26 137/week @ 2024-10-03 127/week @ 2024-10-10 200/week @ 2024-10-17 95/week @ 2024-10-24

572 downloads per month
Used in 2 crates

MIT license

15KB
241 lines

ETag middleware for Actix web

To use

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

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


lib.rs:

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)

Dependencies

~15–25MB
~443K SLoC