mirror of
https://github.com/actix/actix-web.git
synced 2025-06-05 06:38:47 +00:00
only support GET requests and 200 OK responses
This commit is contained in:
parent
024666509e
commit
d3899e5c11
1 changed files with 26 additions and 19 deletions
|
@ -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<S> Filter<S> for DefaultFilter {
|
||||
fn filter(&self, req: &HttpRequest<S>, res: &HttpResponse) -> bool {
|
||||
use http::{Method, StatusCode};
|
||||
(*req.method() == Method::GET || *req.method() == Method::HEAD)
|
||||
&& res.status() == StatusCode::OK
|
||||
fn filter(&self, _req: &HttpRequest<S>, _res: &HttpResponse) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -148,8 +144,17 @@ impl<S> Filter<S> 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<S, H, F>
|
||||
where
|
||||
S: 'static,
|
||||
|
@ -186,10 +191,12 @@ where
|
|||
fn response(
|
||||
&mut self, req: &mut HttpRequest<S>, mut res: HttpResponse,
|
||||
) -> Result<middleware::Response> {
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue