Skip to content

Commit

Permalink
Fix broken relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Feb 16, 2024
1 parent 418d9ca commit 829a1ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
25 changes: 15 additions & 10 deletions crates/pypi-types/src/base_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ pub fn base_url_join_relative(base: &str, maybe_relative: &str) -> Result<Url, J
Ok(absolute) => Ok(absolute),
Err(err) => {
if err == url::ParseError::RelativeUrlWithoutBase {
let base = Url::parse(base).map_err(|err| JoinRelativeError {
let base_url = Url::parse(base).map_err(|err| JoinRelativeError::ParseError {
original: base.to_string(),
source: err,
})?;
base.join(maybe_relative).map_err(|err| JoinRelativeError {
original: format!("{base}/{maybe_relative}"),
source: err,
})

base_url
.join(maybe_relative)
.map_err(|_| JoinRelativeError::ParseError {
original: format!("{base}/{maybe_relative}"),
source: err,
})
} else {
Err(JoinRelativeError {
Err(JoinRelativeError::ParseError {
original: maybe_relative.to_string(),
source: err,
})
Expand All @@ -36,10 +39,12 @@ pub fn base_url_join_relative(base: &str, maybe_relative: &str) -> Result<Url, J
/// The error message includes the URL (`base` or `maybe_relative`) passed to
/// `base_url_join_relative` that provoked the error.
#[derive(Clone, Debug, thiserror::Error)]
#[error("Failed to parse URL: `{original}`")]
pub struct JoinRelativeError {
original: String,
source: url::ParseError,
pub enum JoinRelativeError {
#[error("Failed to parse URL: `{original}`")]
ParseError {
original: String,
source: url::ParseError,
},
}

#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
Expand Down
5 changes: 4 additions & 1 deletion crates/uv-client/src/registry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ impl RegistryClient {
url.path_segments_mut()
.unwrap()
.pop_if_empty()
.push(package_name.as_ref());
.push(package_name.as_ref())
// The URL *must* end in a trailing slash for proper relative path behavior
// ref https://github.com/servo/rust-url/issues/333
.push("");

trace!("Fetching metadata for {package_name} from {url}");

Expand Down

0 comments on commit 829a1ed

Please sign in to comment.