Add trailing slash if missing to find-links URL

This commit is contained in:
John Mumm 2025-06-30 13:15:50 +02:00
parent b2979d25a8
commit 52338214e3
No known key found for this signature in database
GPG key ID: 73D2271AFDC26EA8
2 changed files with 46 additions and 4 deletions

View file

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use futures::{FutureExt, StreamExt};
@ -149,10 +150,20 @@ impl<'a> FlatIndexClient<'a> {
Self::read_from_directory(&path, index)
.map_err(|err| FlatIndexError::FindLinksDirectory(path.clone(), err))
}
IndexUrl::Pypi(url) | IndexUrl::Url(url) => self
.read_from_url(url, index)
.await
.map_err(|err| FlatIndexError::FindLinksUrl(url.to_url(), err)),
IndexUrl::Pypi(url) | IndexUrl::Url(url) => {
// If the URL was originally provided with a slash, we restore that slash
// before making a request.
let url_with_original_slash =
if url.given().is_some_and(|given| given.ends_with('/')) {
index.url_with_trailing_slash()
} else {
Cow::Borrowed(index.url())
};
self.read_from_url(&url_with_original_slash, index)
.await
.map_err(|err| FlatIndexError::FindLinksUrl(url.to_url(), err))
}
}
}

View file

@ -117,6 +117,18 @@ impl IndexUrl {
}
}
/// Return the raw URL for the index with a trailing slash.
pub fn url_with_trailing_slash(&self) -> Cow<'_, DisplaySafeUrl> {
let path = self.url().path();
if path.ends_with('/') {
Cow::Borrowed(self.url())
} else {
let mut url = self.url().clone();
url.set_path(&format!("{path}/"));
Cow::Owned(url)
}
}
/// Convert the index URL into a [`DisplaySafeUrl`].
pub fn into_url(self) -> DisplaySafeUrl {
match self {
@ -713,4 +725,23 @@ mod tests {
"git+https://github.com/example/repo.git"
));
}
#[test]
fn test_index_url_with_trailing_slash() {
let url_with_trailing_slash = DisplaySafeUrl::parse("https://example.com/path/").unwrap();
let index_url_with_given_slash =
IndexUrl::parse("https://example.com/path/", None).unwrap();
assert_eq!(
&*index_url_with_given_slash.url_with_trailing_slash(),
&url_with_trailing_slash
);
let index_url_without_given_slash =
IndexUrl::parse("https://example.com/path", None).unwrap();
assert_eq!(
&*index_url_without_given_slash.url_with_trailing_slash(),
&url_with_trailing_slash
);
}
}