Skip to content

Commit fa75458

Browse files
authored
Use index-level instead of realm-level credential caching for known indexes (#12717)
The current uv behavior is to cache credentials either at the request URL or realm level. But in general, the expected behavior for indexes is to apply credentials at the index level (as implemented in #12651). This means that we also need to cache credentials at this level. Note that when uv does not detect an index URL for a request URL, it will continue to apply the old behavior. Depends on #12651.
1 parent 3fca195 commit fa75458

File tree

4 files changed

+291
-46
lines changed

4 files changed

+291
-46
lines changed

crates/uv-auth/src/cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ type FxOnceMap<K, V> = OnceMap<K, V, BuildHasherDefault<FxHasher>>;
1717
pub struct CredentialsCache {
1818
/// A cache per realm and username
1919
realms: RwLock<FxHashMap<(Realm, Username), Arc<Credentials>>>,
20-
/// A cache tracking the result of fetches from external services
21-
pub(crate) fetches: FxOnceMap<(Realm, Username), Option<Arc<Credentials>>>,
20+
/// A cache tracking the result of realm or index URL fetches from external services
21+
pub(crate) fetches: FxOnceMap<(String, Username), Option<Arc<Credentials>>>,
2222
/// A cache per URL, uses a trie for efficient prefix queries.
2323
urls: RwLock<UrlTrie>,
2424
}

crates/uv-auth/src/index.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Indexes {
8383
// efficient search.
8484
self.0
8585
.iter()
86-
.find(|index| url.as_str().starts_with(index.root_url.as_str()))
86+
.find(|index| is_url_prefix(&index.root_url, url))
8787
.map(|index| &index.root_url)
8888
}
8989

@@ -93,10 +93,21 @@ impl Indexes {
9393
// but we could use a trie instead of a HashMap here for more
9494
// efficient search.
9595
for index in &self.0 {
96-
if url.as_str().starts_with(index.root_url.as_str()) {
96+
if is_url_prefix(&index.root_url, url) {
9797
return index.auth_policy;
9898
}
9999
}
100100
AuthPolicy::Auto
101101
}
102102
}
103+
104+
fn is_url_prefix(base: &Url, url: &Url) -> bool {
105+
if base.scheme() != url.scheme()
106+
|| base.host_str() != url.host_str()
107+
|| base.port_or_known_default() != url.port_or_known_default()
108+
{
109+
return false;
110+
}
111+
112+
url.path().starts_with(base.path())
113+
}

0 commit comments

Comments
 (0)