client redirects only handled when location header present

closes #2105
This commit is contained in:
Rob Ede 2022-03-08 01:23:59 +00:00
parent 4b3d0edc9e
commit f983103e44
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
1 changed files with 34 additions and 6 deletions

View File

@ -161,7 +161,8 @@ where
| StatusCode::SEE_OTHER | StatusCode::SEE_OTHER
| StatusCode::TEMPORARY_REDIRECT | StatusCode::TEMPORARY_REDIRECT
| StatusCode::PERMANENT_REDIRECT | StatusCode::PERMANENT_REDIRECT
if *max_redirect_times > 0 => if *max_redirect_times > 0
&& res.headers().contains_key(header::LOCATION) =>
{ {
let reuse_body = res.head().status == StatusCode::TEMPORARY_REDIRECT let reuse_body = res.head().status == StatusCode::TEMPORARY_REDIRECT
|| res.head().status == StatusCode::PERMANENT_REDIRECT; || res.head().status == StatusCode::PERMANENT_REDIRECT;
@ -287,7 +288,10 @@ mod tests {
use actix_web::{web, App, Error, HttpRequest, HttpResponse}; use actix_web::{web, App, Error, HttpRequest, HttpResponse};
use super::*; use super::*;
use crate::{http::header::HeaderValue, ClientBuilder}; use crate::{
http::{header::HeaderValue, StatusCode},
ClientBuilder,
};
#[actix_rt::test] #[actix_rt::test]
async fn test_basic_redirect() { async fn test_basic_redirect() {
@ -315,6 +319,23 @@ mod tests {
assert_eq!(res.status().as_u16(), 400); assert_eq!(res.status().as_u16(), 400);
} }
#[actix_rt::test]
async fn redirect_without_location() {
let client = ClientBuilder::new()
.disable_redirects()
.wrap(Redirect::new().max_redirect_times(10))
.finish();
let srv = actix_test::start(|| {
App::new().service(web::resource("/").route(web::to(|| async {
Ok::<_, Error>(HttpResponse::Found().finish())
})))
});
let res = client.get(srv.url("/")).send().await.unwrap();
assert_eq!(res.status(), StatusCode::FOUND);
}
#[actix_rt::test] #[actix_rt::test]
async fn test_redirect_limit() { async fn test_redirect_limit() {
let client = ClientBuilder::new() let client = ClientBuilder::new()
@ -328,14 +349,14 @@ mod tests {
.service(web::resource("/").route(web::to(|| async { .service(web::resource("/").route(web::to(|| async {
Ok::<_, Error>( Ok::<_, Error>(
HttpResponse::Found() HttpResponse::Found()
.append_header(("location", "/test")) .insert_header(("location", "/test"))
.finish(), .finish(),
) )
}))) })))
.service(web::resource("/test").route(web::to(|| async { .service(web::resource("/test").route(web::to(|| async {
Ok::<_, Error>( Ok::<_, Error>(
HttpResponse::Found() HttpResponse::Found()
.append_header(("location", "/test2")) .insert_header(("location", "/test2"))
.finish(), .finish(),
) )
}))) })))
@ -345,8 +366,15 @@ mod tests {
}); });
let res = client.get(srv.url("/")).send().await.unwrap(); let res = client.get(srv.url("/")).send().await.unwrap();
assert_eq!(res.status(), StatusCode::FOUND);
assert_eq!(res.status().as_u16(), 302); assert_eq!(
res.headers()
.get(header::LOCATION)
.unwrap()
.to_str()
.unwrap(),
"/test2"
);
} }
#[actix_rt::test] #[actix_rt::test]