Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: url with trailing slash mirror #943

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/rattler_conda_types/src/utils/url_with_trailing_slash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,25 @@ impl Display for UrlWithTrailingSlash {
write!(f, "{}", &self.0)
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json;

#[test]
fn test_url_with_trailing_slash() {
let url = Url::parse("http://example.com").unwrap();
let url_with_trailing_slash = UrlWithTrailingSlash::from(url.clone());
assert_eq!(
url_with_trailing_slash,
UrlWithTrailingSlash::from(url.clone())
);

let serialized = serde_json::to_string(&url_with_trailing_slash).unwrap();
assert_eq!(serialized, "\"http://example.com/\"");

let deserialized: UrlWithTrailingSlash = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized, url_with_trailing_slash);
}
}
1 change: 1 addition & 0 deletions crates/rattler_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

pub mod authentication_middleware;
pub mod authentication_storage;
pub mod url_with_trailing_slash;

Check failure on line 16 in crates/rattler_networking/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check intra-doc links

file not found for module `url_with_trailing_slash`

Check failure on line 16 in crates/rattler_networking/src/lib.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

file not found for module `url_with_trailing_slash`

pub mod mirror_middleware;
pub mod oci_middleware;
Expand Down
16 changes: 9 additions & 7 deletions crates/rattler_networking/src/mirror_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
};

use http::{Extensions, StatusCode};
use itertools::Itertools;

Check failure on line 8 in crates/rattler_networking/src/mirror_middleware.rs

View workflow job for this annotation

GitHub Actions / Check intra-doc links

unused import: `itertools::Itertools`

Check failure on line 8 in crates/rattler_networking/src/mirror_middleware.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

unused import: `itertools::Itertools`
use reqwest::{Request, Response, ResponseBuilderExt};
use reqwest_middleware::{Middleware, Next, Result};
use url::Url;

use crate::url_with_trailing_slash::UrlWithTrailingSlash;

Check failure on line 13 in crates/rattler_networking/src/mirror_middleware.rs

View workflow job for this annotation

GitHub Actions / Check intra-doc links

unresolved import `crate::url_with_trailing_slash::UrlWithTrailingSlash`

Check failure on line 13 in crates/rattler_networking/src/mirror_middleware.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

unresolved import `crate::url_with_trailing_slash::UrlWithTrailingSlash`

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// Settings for the specific mirror (e.g. no zstd or bz2 support)
pub struct Mirror {
/// The url of this mirror
pub url: Url,
pub url: UrlWithTrailingSlash,
/// Disable zstd support (for repodata.json.zst files)
pub no_zstd: bool,
/// Disable bz2 support (for repodata.json.bz2 files)
Expand All @@ -39,14 +41,14 @@

/// Middleware to handle mirrors
pub struct MirrorMiddleware {
mirror_map: HashMap<Url, Vec<MirrorState>>,
sorted_keys: Vec<(String, Url)>,
mirror_map: HashMap<UrlWithTrailingSlash, Vec<MirrorState>>,
sorted_keys: Vec<(String, UrlWithTrailingSlash)>,
}

impl MirrorMiddleware {
/// Create a new `MirrorMiddleware` from a map of mirrors
pub fn from_map(mirror_map: HashMap<Url, Vec<Mirror>>) -> Self {
let mirror_map: HashMap<Url, Vec<MirrorState>> = mirror_map
pub fn from_map(mirror_map: HashMap<UrlWithTrailingSlash, Vec<Mirror>>) -> Self {
let mirror_map: HashMap<UrlWithTrailingSlash, Vec<MirrorState>> = mirror_map
.into_iter()
.map(|(url, mirrors)| {
let mirrors = mirrors
Expand All @@ -65,7 +67,7 @@
.cloned()
.sorted_by(|a, b| b.path().len().cmp(&a.path().len()))
.map(|k| (k.to_string(), k.clone()))
.collect::<Vec<(String, Url)>>();
.collect::<Vec<(String, UrlWithTrailingSlash)>>();

Self {
mirror_map,
Expand All @@ -75,7 +77,7 @@

/// Get sorted keys. The keys are sorted by length of the path,
/// so the longest path comes first.
pub fn keys(&self) -> &[(String, Url)] {
pub fn keys(&self) -> &[(String, UrlWithTrailingSlash)] {
&self.sorted_keys
}
}
Expand Down
19 changes: 11 additions & 8 deletions crates/rattler_networking/src/oci_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,18 @@ impl Display for OciAction {
async fn get_token(url: &OCIUrl, action: OciAction) -> Result<String, OciMiddlewareError> {
let token_url = url.token_url(action)?;

tracing::trace!("OCI Mirror: requesting token from {}", token_url);
let response = reqwest::get(token_url.clone()).await?;

let token = reqwest::get(token_url)
.await?
.json::<OCIToken>()
.await?
.token;

Ok(token)
match response.error_for_status() {
Ok(response) => {
let token = response.json::<OCIToken>().await?;
Ok(token.token)
}
Err(e) => {
tracing::error!("OCI Mirror: failed to get token with URL: {}", token_url);
Err(OciMiddlewareError::Reqwest(e))
}
}
}

#[derive(Debug)]
Expand Down
34 changes: 22 additions & 12 deletions py-rattler/Cargo.lock

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

1 change: 1 addition & 0 deletions py-rattler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ url = "2.5.3"
openssl = { version = "0.10", optional = true }
pep508_rs = "0.9.1"
serde_json = "1.0.132"
pyo3-file = "0.9.0"

[build-dependencies]
pyo3-build-config = "0.22.5"
Expand Down
Loading
Loading