more robust 3xx handling

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
This commit is contained in:
ゆめ 2024-11-22 14:23:28 -06:00
parent dbe96d1482
commit 7e31f167ea
No known key found for this signature in database
2 changed files with 11 additions and 1 deletions

View file

@ -350,6 +350,7 @@ pub mod reqwest {
)) ))
.await; .await;
} }
return Err(ErrorResponse::missing_location());
} }
if !resp.status().is_success() { if !resp.status().is_success() {
@ -593,7 +594,7 @@ pub mod cf_worker {
.map_err(ErrorResponse::worker_fetch_error)) .map_err(ErrorResponse::worker_fetch_error))
.await?; .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") { if let Ok(Some(location)) = resp.headers().get("location") {
return Box::pin(self.request_upstream( return Box::pin(self.request_upstream(
info, info,
@ -604,6 +605,7 @@ pub mod cf_worker {
)) ))
.await; .await;
} }
return Err(ErrorResponse::missing_location());
} }
if resp.status_code() < 200 || resp.status_code() >= 300 { if resp.status_code() < 200 || resp.status_code() >= 300 {

View file

@ -596,6 +596,14 @@ impl Display for ErrorResponse {
impl std::error::Error for ErrorResponse {} impl std::error::Error for ErrorResponse {}
impl 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"))] #[cfg(not(feature = "cf-worker"))]
/// URL must be a DNS name /// URL must be a DNS name
#[must_use] #[must_use]