Skip to content

Commit

Permalink
Retain authentication when making range requests (#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb authored Feb 23, 2024
1 parent bd59076 commit fe18475
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ anstream = { version = "0.6.5" }
anyhow = { version = "1.0.79" }
async-compression = { version = "0.4.6" }
async-trait = { version = "0.1.77" }
async_http_range_reader = { version = "0.6.1" }
async_http_range_reader = { version = "0.7.0" }
async_zip = { git = "https://github.com/charliermarsh/rs-async-zip", rev = "d76801da0943de985254fc6255c0e476b57c5836", features = ["deflate"] }
base64 = { version = "0.21.7" }
cachedir = { version = "0.3.1" }
Expand Down
29 changes: 19 additions & 10 deletions crates/uv-client/src/registry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::str::FromStr;
use async_http_range_reader::AsyncHttpRangeReader;
use futures::{FutureExt, TryStreamExt};

use http::HeaderMap;
use reqwest::{Client, ClientBuilder, Response, StatusCode};
use reqwest_retry::policies::ExponentialBackoff;
use reqwest_retry::RetryTransientMiddleware;
Expand Down Expand Up @@ -440,14 +441,28 @@ impl RegistryClient {
Connectivity::Offline => CacheControl::AllowStale,
};

let client = self.client_raw.clone();
let req = self
.client
.uncached()
.head(url.clone())
.build()
.map_err(ErrorKind::RequestError)?;

// Copy authorization headers from the HEAD request to subsequent requests
let mut headers = HeaderMap::default();
if let Some(authorization) = req.headers().get("authorization") {
headers.append("authorization", authorization.clone());
}

// This response callback is special, we actually make a number of subsequent requests to
// fetch the file from the remote zip.
let client = self.client_raw.clone();
let read_metadata_range_request = |response: Response| {
async {
let mut reader = AsyncHttpRangeReader::from_head_response(client, response)
.await
.map_err(ErrorKind::AsyncHttpRangeReader)?;
let mut reader =
AsyncHttpRangeReader::from_head_response(client, response, headers)
.await
.map_err(ErrorKind::AsyncHttpRangeReader)?;
trace!("Getting metadata for {filename} by range request");
let text = wheel_metadata_from_remote_zip(filename, &mut reader).await?;
let metadata = Metadata21::parse(text.as_bytes()).map_err(|err| {
Expand All @@ -463,12 +478,6 @@ impl RegistryClient {
.instrument(info_span!("read_metadata_range_request", wheel = %filename))
};

let req = self
.client
.uncached()
.head(url.clone())
.build()
.map_err(ErrorKind::RequestError)?;
let result = self
.client
.get_serde(
Expand Down

0 comments on commit fe18475

Please sign in to comment.