diff --git a/src/fetch/mod.rs b/src/fetch/mod.rs index 09cf871..b9ba618 100644 --- a/src/fetch/mod.rs +++ b/src/fetch/mod.rs @@ -350,6 +350,7 @@ pub mod reqwest { )) .await; } + return Err(ErrorResponse::missing_location()); } if !resp.status().is_success() { @@ -593,7 +594,7 @@ pub mod cf_worker { .map_err(ErrorResponse::worker_fetch_error)) .await?; - if resp.status_code() == 301 || resp.status_code() == 302 { + if resp.status_code() >= 300 && resp.status_code() < 400 { if let Ok(Some(location)) = resp.headers().get("location") { return Box::pin(self.request_upstream( info, @@ -604,6 +605,7 @@ pub mod cf_worker { )) .await; } + return Err(ErrorResponse::missing_location()); } if resp.status_code() < 200 || resp.status_code() >= 300 { diff --git a/src/lib.rs b/src/lib.rs index 8715772..b52b041 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -596,6 +596,14 @@ impl Display for ErrorResponse { impl std::error::Error for ErrorResponse {} impl ErrorResponse { + /// Received a redirect without a location + #[must_use] + pub const fn missing_location() -> Self { + Self { + status: StatusCode::BAD_GATEWAY, + message: Cow::Borrowed("Redirect location missing"), + } + } #[cfg(not(feature = "cf-worker"))] /// URL must be a DNS name #[must_use]