From 5143869cf7ab89df25e29249c275e12a2817dcf8 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 1 Mar 2024 17:53:04 -0500 Subject: [PATCH] Drop hash fragment when downloading from registry --- crates/uv-client/src/registry_client.rs | 12 ++++++++-- .../src/distribution_database.rs | 16 ++++++++------ crates/uv-distribution/src/source/mod.rs | 22 +++++++++---------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/crates/uv-client/src/registry_client.rs b/crates/uv-client/src/registry_client.rs index be511b8e8ef4d..cd25d04e11c00 100644 --- a/crates/uv-client/src/registry_client.rs +++ b/crates/uv-client/src/registry_client.rs @@ -225,6 +225,7 @@ impl RegistryClient { ) -> Result, CachedClientError>, Error> { // Format the URL for PyPI. let mut url: Url = index.clone().into(); + url.set_fragment(None); url.path_segments_mut() .unwrap() .pop_if_empty() @@ -386,7 +387,10 @@ impl RegistryClient { .as_ref() .is_some_and(pypi_types::DistInfoMetadata::is_available) { - let url = Url::parse(&format!("{url}.metadata")).map_err(ErrorKind::UrlParseError)?; + // Like `pip`, drop the fragment, if it exists. + let mut url = url.clone(); + url.set_fragment(None); + url.set_path(&format!("{}.metadata", url.path())); let cache_entry = self.cache.entry( CacheBucket::Wheels, @@ -455,6 +459,10 @@ impl RegistryClient { Connectivity::Offline => CacheControl::AllowStale, }; + // Like `pip`, drop the fragment, if it exists. + let mut url = url.clone(); + url.set_fragment(None); + let client = self.client_raw.clone(); let req = self .client @@ -521,7 +529,7 @@ impl RegistryClient { } // Stream the file, searching for the METADATA. - let reader = self.stream_external(url).await?; + let reader = self.stream_external(&url).await?; read_metadata_async_stream(filename, url.to_string(), reader).await } diff --git a/crates/uv-distribution/src/distribution_database.rs b/crates/uv-distribution/src/distribution_database.rs index b79bb3526c9e7..a60f62e0bca44 100644 --- a/crates/uv-distribution/src/distribution_database.rs +++ b/crates/uv-distribution/src/distribution_database.rs @@ -104,7 +104,7 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context> return Err(Error::NoBinary); } - let url = match &wheel.file.url { + let mut url = match &wheel.file.url { FileLocation::RelativeUrl(base, url) => { pypi_types::base_url_join_relative(base, url)? } @@ -152,6 +152,9 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context> } }; + // Like `pip`, drop the fragment, if it exists. + url.set_fragment(None); + // Create an entry for the wheel itself alongside its HTTP cache. let wheel_entry = self.cache.entry( CacheBucket::Wheels, @@ -244,12 +247,11 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context> .instrument(info_span!("download", wheel = %wheel)) }; - let req = self - .client - .cached_client() - .uncached() - .get(wheel.url.raw().clone()) - .build()?; + // Like `pip`, drop the fragment, if it exists. + let mut url = wheel.url.raw().clone(); + url.set_fragment(None); + + let req = self.client.cached_client().uncached().get(url).build()?; let cache_control = match self.client.connectivity() { Connectivity::Online => CacheControl::from( self.cache diff --git a/crates/uv-distribution/src/source/mod.rs b/crates/uv-distribution/src/source/mod.rs index 078283e4e0a58..26188440b7ff8 100644 --- a/crates/uv-distribution/src/source/mod.rs +++ b/crates/uv-distribution/src/source/mod.rs @@ -263,6 +263,10 @@ impl<'a, T: BuildContext> SourceDistCachedBuilder<'a, T> { Connectivity::Offline => CacheControl::AllowStale, }; + // Like `pip`, drop the fragment, if it exists. + let mut url = url.clone(); + url.set_fragment(None); + let download = |response| { async { // At this point, we're seeing a new or updated source distribution. Initialize a @@ -280,12 +284,7 @@ impl<'a, T: BuildContext> SourceDistCachedBuilder<'a, T> { .boxed() .instrument(info_span!("download", source_dist = %source_dist)) }; - let req = self - .client - .cached_client() - .uncached() - .get(url.clone()) - .build()?; + let req = self.client.cached_client().uncached().get(url).build()?; let manifest = self .client .cached_client() @@ -366,6 +365,10 @@ impl<'a, T: BuildContext> SourceDistCachedBuilder<'a, T> { Connectivity::Offline => CacheControl::AllowStale, }; + // Like `pip`, drop the fragment, if it exists. + let mut url = url.clone(); + url.set_fragment(None); + let download = |response| { async { // At this point, we're seeing a new or updated source distribution. Initialize a @@ -383,12 +386,7 @@ impl<'a, T: BuildContext> SourceDistCachedBuilder<'a, T> { .boxed() .instrument(info_span!("download", source_dist = %source_dist)) }; - let req = self - .client - .cached_client() - .uncached() - .get(url.clone()) - .build()?; + let req = self.client.cached_client().uncached().get(url).build()?; let manifest = self .client .cached_client()