diff --git a/src/middleware/etaghasher.rs b/src/middleware/etaghasher.rs index b68174c84..661fd3681 100644 --- a/src/middleware/etaghasher.rs +++ b/src/middleware/etaghasher.rs @@ -1,13 +1,14 @@ //! ETag header and `304 Not Modified` support for HTTP responses /// /// The `EtagHasher` middleware generates [RFC -/// 7232](https://tools.ietf.org/html/rfc7232) ETag headers for HTTP -/// responses, and checks the ETag for a response against those provided -/// in the `If-None-Match` header of the request, if present. In the -/// event of a match, instead of returning the original response, an -/// HTTP `304 Not Modified` response with no content is returned -/// instead. Only response [Body](enum.Body.html)s of type `Binary` are -/// supported; responses with other body types will be left unchanged. +/// 7232](https://tools.ietf.org/html/rfc7232) ETag headers for `200 OK` +/// responses to HTTP `GET` requests, and checks the ETag for a response +/// against those provided in the `If-None-Match` header of the request, +/// if present. In the event of a match, instead of returning the +/// original response, an HTTP `304 Not Modified` response with no +/// content is returned instead. Only response [Body](enum.Body.html)s +/// of type `Binary` are supported; responses with other body types will +/// be left unchanged. /// /// ETag values are generated by computing a hash function over the /// bytes of the body of the original response. Thus, using this @@ -32,9 +33,7 @@ /// instance is created; the `DefaultHasher` and `DefaultFilter` can be /// used if desired. Currently `DefaultHasher` computes an SHA-1 hash, /// but this should not be relied upon. The `DefaultFilter` returns -/// `true` when the request method is `GET` or `HEAD` and the original -/// response status is `200 OK`. If you provide your own `filter`, you -/// will want to check for these conditions as well. +/// `true` for all `(request, response)` pairs. /// /// ```rust /// # extern crate actix_web; @@ -131,14 +130,11 @@ impl Hasher for DefaultHasher { } } -/// Returns `true` when the request method is `GET` or `HEAD` and the -/// original response status is `200 OK`, and `false` otherwise. +/// Returns `true` for every `(request, response)` pair. pub struct DefaultFilter; impl Filter for DefaultFilter { - fn filter(&self, req: &HttpRequest, res: &HttpResponse) -> bool { - use http::{Method, StatusCode}; - (*req.method() == Method::GET || *req.method() == Method::HEAD) - && res.status() == StatusCode::OK + fn filter(&self, _req: &HttpRequest, _res: &HttpResponse) -> bool { + true } } @@ -148,8 +144,17 @@ impl Filter for DefaultFilter { /// The `EtagHasher` struct contains a Hasher to compute ETag values for /// byte slices and a Filter to determine whether ETag computation and /// checking should be applied to a particular (request, response) -/// pair. Only response [Body](enum.Body.html)s of type `Binary` are -/// supported; responses with other body types will be left unchanged. +/// pair. +/// +/// Middleware processing will be performed only if the following +/// conditions hold: +/// +/// * The request method is `GET` +/// * The status of the original response is `200 OK` +/// * The type of the original response [Body](enum.Body.html) is `Binary` +/// +/// If any of these conditions is false, the original response will be +/// passed through unmodified. pub struct EtagHasher where S: 'static, @@ -186,10 +191,12 @@ where fn response( &mut self, req: &mut HttpRequest, mut res: HttpResponse, ) -> Result { + use http::{Method, StatusCode}; use header; use Body; - if !self.filter.filter(req, &res) { + let valid = *req.method() == Method::GET && res.status() == StatusCode::OK; + if !(valid && self.filter.filter(req, &res)) { return Ok(middleware::Response::Done(res)); }