mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 10:27:42 +02:00
refactor Middleware trait, use Result
This commit is contained in:
@ -5,7 +5,7 @@ use std::collections::HashSet;
|
||||
use http::{self, Method, HttpTryFrom, Uri};
|
||||
use http::header::{self, HeaderName};
|
||||
|
||||
use error::ResponseError;
|
||||
use error::{Result, ResponseError};
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
use middleware::{Middleware, Response, Started};
|
||||
@ -152,17 +152,16 @@ impl Cors {
|
||||
|
||||
impl<S> Middleware<S> for Cors {
|
||||
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Started {
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
||||
if Method::OPTIONS == *req.method() {
|
||||
if let Err(err) = self.validate_origin(req) {
|
||||
return Started::Err(err.into())
|
||||
}
|
||||
self.validate_origin(req)?;
|
||||
self.validate_allowed_method(req)?;
|
||||
}
|
||||
Started::Done
|
||||
Ok(Started::Done)
|
||||
}
|
||||
|
||||
fn response(&self, _: &mut HttpRequest<S>, mut resp: HttpResponse) -> Response {
|
||||
Response::Done(resp)
|
||||
fn response(&self, _: &mut HttpRequest<S>, mut resp: HttpResponse) -> Result<Response> {
|
||||
Ok(Response::Done(resp))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
use http::{HeaderMap, HttpTryFrom};
|
||||
use http::header::{HeaderName, HeaderValue, CONTENT_TYPE};
|
||||
|
||||
use error::Result;
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
use middleware::{Response, Middleware};
|
||||
@ -40,7 +41,7 @@ impl DefaultHeaders {
|
||||
|
||||
impl<S> Middleware<S> for DefaultHeaders {
|
||||
|
||||
fn response(&self, _: &mut HttpRequest<S>, mut resp: HttpResponse) -> Response {
|
||||
fn response(&self, _: &mut HttpRequest<S>, mut resp: HttpResponse) -> Result<Response> {
|
||||
for (key, value) in self.headers.iter() {
|
||||
if !resp.headers().contains_key(key) {
|
||||
resp.headers_mut().insert(key, value.clone());
|
||||
@ -51,7 +52,7 @@ impl<S> Middleware<S> for DefaultHeaders {
|
||||
resp.headers_mut().insert(
|
||||
CONTENT_TYPE, HeaderValue::from_static("application/octet-stream"));
|
||||
}
|
||||
Response::Done(resp)
|
||||
Ok(Response::Done(resp))
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,14 +114,14 @@ mod tests {
|
||||
|
||||
let resp = HttpResponse::Ok().finish().unwrap();
|
||||
let resp = match mw.response(&mut req, resp) {
|
||||
Response::Done(resp) => resp,
|
||||
Ok(Response::Done(resp)) => resp,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
||||
|
||||
let resp = HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish().unwrap();
|
||||
let resp = match mw.response(&mut req, resp) {
|
||||
Response::Done(resp) => resp,
|
||||
Ok(Response::Done(resp)) => resp,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0002");
|
||||
|
@ -7,6 +7,7 @@ use libc;
|
||||
use time;
|
||||
use regex::Regex;
|
||||
|
||||
use error::Result;
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
use middleware::{Middleware, Started, Finished};
|
||||
@ -101,9 +102,9 @@ impl Logger {
|
||||
|
||||
impl<S> Middleware<S> for Logger {
|
||||
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Started {
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
||||
req.extensions().insert(StartTime(time::now()));
|
||||
Started::Done
|
||||
Ok(Started::Done)
|
||||
}
|
||||
|
||||
fn finish(&self, req: &mut HttpRequest<S>, resp: &HttpResponse) -> Finished {
|
||||
@ -305,7 +306,7 @@ mod tests {
|
||||
.force_close().body(Body::Empty).unwrap();
|
||||
|
||||
match logger.start(&mut req) {
|
||||
Started::Done => (),
|
||||
Ok(Started::Done) => (),
|
||||
_ => panic!(),
|
||||
};
|
||||
match logger.finish(&mut req, &resp) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Middlewares
|
||||
use futures::Future;
|
||||
|
||||
use error::Error;
|
||||
use error::{Error, Result};
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
|
||||
@ -18,8 +18,6 @@ pub use self::session::{RequestSession, Session, SessionImpl, SessionBackend, Se
|
||||
pub enum Started {
|
||||
/// Execution completed
|
||||
Done,
|
||||
/// Moddleware error
|
||||
Err(Error),
|
||||
/// New http response got generated. If middleware generates response
|
||||
/// handler execution halts.
|
||||
Response(HttpResponse),
|
||||
@ -29,8 +27,6 @@ pub enum Started {
|
||||
|
||||
/// Middleware execution result
|
||||
pub enum Response {
|
||||
/// Moddleware error
|
||||
Err(Error),
|
||||
/// New http response got generated
|
||||
Done(HttpResponse),
|
||||
/// Result is a future that resolves to a new http response
|
||||
@ -51,14 +47,14 @@ pub trait Middleware<S>: 'static {
|
||||
|
||||
/// Method is called when request is ready. It may return
|
||||
/// future, which should resolve before next middleware get called.
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Started {
|
||||
Started::Done
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
||||
Ok(Started::Done)
|
||||
}
|
||||
|
||||
/// Method is called when handler returns response,
|
||||
/// but before sending http message to peer.
|
||||
fn response(&self, req: &mut HttpRequest<S>, resp: HttpResponse) -> Response {
|
||||
Response::Done(resp)
|
||||
fn response(&self, req: &mut HttpRequest<S>, resp: HttpResponse) -> Result<Response> {
|
||||
Ok(Response::Done(resp))
|
||||
}
|
||||
|
||||
/// Method is called after body stream get sent to peer.
|
||||
|
@ -141,7 +141,7 @@ impl<S, T: SessionBackend<S>> SessionStorage<T, S> {
|
||||
|
||||
impl<S: 'static, T: SessionBackend<S>> Middleware<S> for SessionStorage<T, S> {
|
||||
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Started {
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
||||
let mut req = req.clone();
|
||||
|
||||
let fut = self.0.from_request(&mut req)
|
||||
@ -154,14 +154,14 @@ impl<S: 'static, T: SessionBackend<S>> Middleware<S> for SessionStorage<T, S> {
|
||||
Err(err) => FutErr(err)
|
||||
}
|
||||
});
|
||||
Started::Future(Box::new(fut))
|
||||
Ok(Started::Future(Box::new(fut)))
|
||||
}
|
||||
|
||||
fn response(&self, req: &mut HttpRequest<S>, resp: HttpResponse) -> Response {
|
||||
fn response(&self, req: &mut HttpRequest<S>, resp: HttpResponse) -> Result<Response> {
|
||||
if let Some(s_box) = req.extensions().remove::<Arc<SessionImplBox>>() {
|
||||
s_box.0.write(resp)
|
||||
} else {
|
||||
Response::Done(resp)
|
||||
Ok(Response::Done(resp))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -179,7 +179,7 @@ pub trait SessionImpl: 'static {
|
||||
fn clear(&mut self);
|
||||
|
||||
/// Write session to storage backend.
|
||||
fn write(&self, resp: HttpResponse) -> Response;
|
||||
fn write(&self, resp: HttpResponse) -> Result<Response>;
|
||||
}
|
||||
|
||||
/// Session's storage backend trait definition.
|
||||
@ -205,8 +205,8 @@ impl SessionImpl for DummySessionImpl {
|
||||
fn set(&mut self, key: &str, value: String) {}
|
||||
fn remove(&mut self, key: &str) {}
|
||||
fn clear(&mut self) {}
|
||||
fn write(&self, resp: HttpResponse) -> Response {
|
||||
Response::Done(resp)
|
||||
fn write(&self, resp: HttpResponse) -> Result<Response> {
|
||||
Ok(Response::Done(resp))
|
||||
}
|
||||
}
|
||||
|
||||
@ -255,11 +255,11 @@ impl SessionImpl for CookieSession {
|
||||
self.state.clear()
|
||||
}
|
||||
|
||||
fn write(&self, mut resp: HttpResponse) -> Response {
|
||||
fn write(&self, mut resp: HttpResponse) -> Result<Response> {
|
||||
if self.changed {
|
||||
let _ = self.inner.set_cookie(&mut resp, &self.state);
|
||||
}
|
||||
Response::Done(resp)
|
||||
Ok(Response::Done(resp))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user