Avoid stripping query parameters from URLs (#10253)

## Summary

Closes https://github.com/astral-sh/uv/issues/10251.
This commit is contained in:
Charlie Marsh 2024-12-31 12:59:26 -05:00 committed by GitHub
parent c77aa5820b
commit cf88828e55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 9 deletions

View File

@ -157,15 +157,22 @@ impl UrlString {
/// Return the [`UrlString`] with any query parameters and fragments removed. /// Return the [`UrlString`] with any query parameters and fragments removed.
pub fn base_str(&self) -> &str { pub fn base_str(&self) -> &str {
self.as_ref() self.as_ref()
.split_once(['#', '?']) .split_once('?')
.or_else(|| self.as_ref().split_once('#'))
.map(|(path, _)| path) .map(|(path, _)| path)
.unwrap_or(self.as_ref()) .unwrap_or(self.as_ref())
} }
/// Return the [`UrlString`] with any query parameters and fragments removed. /// Return the [`UrlString`] with any fragments removed.
#[must_use] #[must_use]
pub fn as_base_url(&self) -> Self { pub fn without_fragment(&self) -> Self {
Self(self.base_str().to_string()) Self(
self.as_ref()
.split_once('#')
.map(|(path, _)| path)
.unwrap_or(self.as_ref())
.to_string(),
)
} }
} }

View File

@ -3862,15 +3862,14 @@ impl<'de> serde::Deserialize<'de> for Hash {
/// Convert a [`FileLocation`] into a normalized [`UrlString`]. /// Convert a [`FileLocation`] into a normalized [`UrlString`].
fn normalize_file_location(location: &FileLocation) -> Result<UrlString, ToUrlError> { fn normalize_file_location(location: &FileLocation) -> Result<UrlString, ToUrlError> {
match location { match location {
FileLocation::AbsoluteUrl(ref absolute) => Ok(absolute.as_base_url()), FileLocation::AbsoluteUrl(ref absolute) => Ok(absolute.without_fragment()),
FileLocation::RelativeUrl(_, _) => Ok(normalize_url(location.to_url()?)), FileLocation::RelativeUrl(_, _) => Ok(normalize_url(location.to_url()?)),
} }
} }
/// Convert a [`Url`] into a normalized [`UrlString`]. /// Convert a [`Url`] into a normalized [`UrlString`] by removing the fragment.
fn normalize_url(mut url: Url) -> UrlString { fn normalize_url(mut url: Url) -> UrlString {
url.set_fragment(None); url.set_fragment(None);
url.set_query(None);
UrlString::from(url) UrlString::from(url)
} }
@ -3995,9 +3994,8 @@ fn normalize_requirement(requirement: Requirement, root: &Path) -> Result<Requir
// Redact the credentials. // Redact the credentials.
redact_credentials(&mut location); redact_credentials(&mut location);
// Remove the fragment and query from the URL; they're already present in the source. // Remove the fragment from the URL; it's already present in the source.
location.set_fragment(None); location.set_fragment(None);
location.set_query(None);
// Reconstruct the PEP 508 URL from the underlying data. // Reconstruct the PEP 508 URL from the underlying data.
let url = Url::from(ParsedArchiveUrl { let url = Url::from(ParsedArchiveUrl {