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