mirror of https://github.com/fafhrd91/actix-web
rename redirect middleware name. fix method not reset on 307/308
This commit is contained in:
parent
9578b50bb2
commit
2a2fc6022b
|
@ -1,6 +1,6 @@
|
|||
mod redirect;
|
||||
|
||||
pub use self::redirect::RedirectMiddleware;
|
||||
pub use self::redirect::Redirect;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ use std::{
|
|||
pin::Pin,
|
||||
rc::Rc,
|
||||
task::{Context, Poll},
|
||||
convert::TryFrom
|
||||
};
|
||||
|
||||
use actix_http::{
|
||||
|
@ -21,17 +22,17 @@ use super::Transform;
|
|||
use crate::connect::{ConnectRequest, ConnectResponse};
|
||||
use crate::ClientResponse;
|
||||
|
||||
pub struct RedirectMiddleware {
|
||||
pub struct Redirect {
|
||||
max_redirect_times: u8,
|
||||
}
|
||||
|
||||
impl Default for RedirectMiddleware {
|
||||
impl Default for Redirect {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl RedirectMiddleware {
|
||||
impl Redirect {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
max_redirect_times: 10,
|
||||
|
@ -44,7 +45,7 @@ impl RedirectMiddleware {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S> Transform<S, ConnectRequest> for RedirectMiddleware
|
||||
impl<S> Transform<S, ConnectRequest> for Redirect
|
||||
where
|
||||
S: Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError> + 'static,
|
||||
{
|
||||
|
@ -150,8 +151,7 @@ where
|
|||
body,
|
||||
addr,
|
||||
connector,
|
||||
} => {
|
||||
match ready!(fut.poll(cx))? {
|
||||
} => match ready!(fut.poll(cx))? {
|
||||
ConnectResponse::Client(res) => match res.head().status {
|
||||
StatusCode::MOVED_PERMANENTLY
|
||||
| StatusCode::FOUND
|
||||
|
@ -218,13 +218,14 @@ where
|
|||
};
|
||||
|
||||
let addr = addr.take();
|
||||
let method = method.take();
|
||||
let method = method.take().unwrap();
|
||||
let connector = connector.take();
|
||||
let mut max_redirect_times = *max_redirect_times;
|
||||
|
||||
// use a new request head.
|
||||
let mut head = RequestHead::default();
|
||||
head.uri = uri.clone();
|
||||
head.method = method.clone();
|
||||
|
||||
let head = RequestHeadType::Owned(head);
|
||||
|
||||
|
@ -239,7 +240,7 @@ where
|
|||
fut,
|
||||
max_redirect_times,
|
||||
uri: Some(uri),
|
||||
method,
|
||||
method: Some(method),
|
||||
body,
|
||||
addr,
|
||||
connector,
|
||||
|
@ -250,15 +251,12 @@ where
|
|||
_ => Poll::Ready(Ok(ConnectResponse::Client(res))),
|
||||
},
|
||||
_ => unreachable!("ConnectRequest::Tunnel is not handled by Redirect"),
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rebuild_uri(res: &ClientResponse, org_uri: Uri) -> Result<Uri, SendRequestError> {
|
||||
use std::convert::TryFrom;
|
||||
|
||||
let uri = res
|
||||
.headers()
|
||||
.get(header::LOCATION)
|
||||
|
@ -295,7 +293,7 @@ mod tests {
|
|||
async fn test_basic_redirect() {
|
||||
let client = ClientBuilder::new()
|
||||
.connector(crate::Connector::new())
|
||||
.wrap(RedirectMiddleware::new().max_redirect_times(10))
|
||||
.wrap(Redirect::new().max_redirect_times(10))
|
||||
.finish();
|
||||
|
||||
let srv = start(|| {
|
||||
|
@ -320,7 +318,7 @@ mod tests {
|
|||
#[actix_rt::test]
|
||||
async fn test_redirect_limit() {
|
||||
let client = ClientBuilder::new()
|
||||
.wrap(RedirectMiddleware::new().max_redirect_times(1))
|
||||
.wrap(Redirect::new().max_redirect_times(1))
|
||||
.connector(crate::Connector::new())
|
||||
.finish();
|
||||
|
||||
|
|
|
@ -492,9 +492,7 @@ mod tests {
|
|||
JsonPayloadError::Payload(PayloadError::Overflow) => {
|
||||
matches!(other, JsonPayloadError::Payload(PayloadError::Overflow))
|
||||
}
|
||||
JsonPayloadError::ContentType => {
|
||||
matches!(other, JsonPayloadError::ContentType)
|
||||
}
|
||||
JsonPayloadError::ContentType => matches!(other, JsonPayloadError::ContentType),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue