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