Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tower-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ futures-util = { version = "0.3.14", optional = true, default-features = false }
http-body = { version = "1.0.0", optional = true }
http-body-util = { version = "0.1.0", optional = true }
http-range-header = { version = "0.4.0", optional = true }
iri-string = { version = "0.7.0", optional = true }
mime = { version = "0.3.17", optional = true, default-features = false }
mime_guess = { version = "2", optional = true, default-features = false }
percent-encoding = { version = "2.1.0", optional = true }
url = { version = "2.5", optional = true }
tokio = { version = "1.6", optional = true, default-features = false }
tokio-util = { version = "0.7", optional = true, default-features = false, features = ["io"] }
tower = { version = "0.5", optional = true }
Expand Down Expand Up @@ -87,7 +87,7 @@ add-extension = []
auth = ["base64", "validate-request"]
catch-panic = ["tracing", "futures-util/std", "dep:http-body", "dep:http-body-util"]
cors = []
follow-redirect = ["futures-util", "dep:http-body", "iri-string", "tower/util"]
follow-redirect = ["futures-util", "dep:http-body", "dep:url", "tower/util"]
fs = ["futures-core", "futures-util", "dep:http-body", "dep:http-body-util", "tokio/fs", "tokio-util/io", "tokio/io-util", "dep:http-range-header", "mime_guess", "mime", "percent-encoding", "httpdate", "set-status", "futures-util/alloc", "tracing"]
limit = ["dep:http-body", "dep:http-body-util"]
map-request-body = []
Expand Down
35 changes: 30 additions & 5 deletions tower-http/src/follow_redirect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ use http::{
Version,
};
use http_body::Body;
use iri_string::types::{UriAbsoluteString, UriReferenceStr};
use pin_project_lite::pin_project;
use std::{
convert::TryFrom,
Expand All @@ -115,6 +114,7 @@ use std::{
use tower::util::Oneshot;
use tower_layer::Layer;
use tower_service::Service;
use url::Url;

/// [`Layer`] for retrying requests with a [`Service`] to follow redirection responses.
///
Expand Down Expand Up @@ -393,10 +393,9 @@ where

/// Try to resolve a URI reference `relative` against a base URI `base`.
fn resolve_uri(relative: &str, base: &Uri) -> Option<Uri> {
let relative = UriReferenceStr::new(relative).ok()?;
let base = UriAbsoluteString::try_from(base.to_string()).ok()?;
let uri = relative.resolve_against(&base).to_string();
Uri::try_from(uri).ok()
let base_url = Url::parse(&base.to_string()).ok()?;
let resolved = base_url.join(relative).ok()?;
Uri::try_from(String::from(resolved)).ok()
}

#[cfg(test)]
Expand Down Expand Up @@ -473,4 +472,30 @@ mod tests {
}
Ok::<_, Infallible>(res.body(n).unwrap())
}

#[tokio::test]
async fn test_resolve_uri_unicode() {
let base = Uri::from_static("https://example.com/api");
// Case 1: Unicode in path
let relative = "/café";
let resolved = resolve_uri(relative, &base);
assert!(resolved.is_some(), "Should resolve URI with unicode path");
assert_eq!(
resolved.unwrap().to_string(),
"https://example.com/caf%C3%A9"
);

// Case 2: IDNA (Unicode in domain)
let relative_domain = "https://münchen.com/";
let resolved_domain = resolve_uri(relative_domain, &base);
assert!(
resolved_domain.is_some(),
"Should resolve URI with unicode domain"
);
// München is encoded as punycode: xn--mnchen-3ya
assert_eq!(
resolved_domain.unwrap().to_string(),
"https://xn--mnchen-3ya.com/"
);
}
}