1use std::fmt::{self, Display, Formatter};
2
3use rustc_hash::FxHashSet;
4use uv_redacted::SafeUrl;
5
6#[derive(
8 Copy,
9 Clone,
10 Debug,
11 Default,
12 Hash,
13 Eq,
14 PartialEq,
15 Ord,
16 PartialOrd,
17 serde::Serialize,
18 serde::Deserialize,
19)]
20#[serde(rename_all = "kebab-case")]
21#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
22pub enum AuthPolicy {
23 #[default]
29 Auto,
30 Always,
35 Never,
39}
40
41impl Display for AuthPolicy {
42 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
43 match self {
44 Self::Auto => write!(f, "auto"),
45 Self::Always => write!(f, "always"),
46 Self::Never => write!(f, "never"),
47 }
48 }
49}
50
51#[derive(Debug, Clone, Hash, Eq, PartialEq)]
55pub struct Index {
56 pub url: SafeUrl,
57 pub root_url: SafeUrl,
60 pub auth_policy: AuthPolicy,
61}
62
63impl Index {
64 fn is_prefix_for(&self, url: &SafeUrl) -> bool {
65 if self.root_url.scheme() != url.scheme()
66 || self.root_url.host_str() != url.host_str()
67 || self.root_url.port_or_known_default() != url.port_or_known_default()
68 {
69 return false;
70 }
71
72 is_path_prefix(self.root_url.path(), url.path())
73 }
74}
75
76pub(crate) fn is_path_prefix(prefix: &str, path: &str) -> bool {
81 strip_path_prefix(prefix, path).is_some()
82}
83
84pub fn strip_path_prefix<'a>(prefix: &str, path: &'a str) -> Option<&'a str> {
88 if prefix == path {
89 return Some("");
90 }
91
92 let suffix = path.strip_prefix(prefix)?;
93 if prefix.ends_with('/') {
94 Some(suffix)
95 } else {
96 suffix.strip_prefix('/')
97 }
98}
99
100#[derive(Debug, Default, Clone, Eq, PartialEq)]
105pub struct Indexes(FxHashSet<Index>);
106
107impl Indexes {
108 pub fn new() -> Self {
109 Self(FxHashSet::default())
110 }
111
112 pub fn from_indexes(urls: impl IntoIterator<Item = Index>) -> Self {
114 let mut index_urls = Self::new();
115 for url in urls {
116 index_urls.0.insert(url);
117 }
118 index_urls
119 }
120
121 pub(crate) fn index_for(&self, url: &SafeUrl) -> Option<&Index> {
123 self.find_prefix_index(url)
124 }
125
126 pub(crate) fn auth_policy_for(&self, url: &SafeUrl) -> AuthPolicy {
128 self.find_prefix_index(url)
129 .map(|index| index.auth_policy)
130 .unwrap_or(AuthPolicy::Auto)
131 }
132
133 fn find_prefix_index(&self, url: &SafeUrl) -> Option<&Index> {
134 self.0.iter().find(|&index| index.is_prefix_for(url))
135 }
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141
142 fn index(root_url: &str, auth_policy: AuthPolicy) -> Index {
143 let root_url = SafeUrl::parse(root_url).unwrap();
144 Index {
145 url: root_url.clone(),
146 root_url,
147 auth_policy,
148 }
149 }
150
151 #[test]
152 fn test_index_path_prefix_requires_segment_boundary() {
153 let index = index("https://example.com/simple", AuthPolicy::Always);
154
155 for url in [
156 "https://example.com/simple",
157 "https://example.com/simple/",
158 "https://example.com/simple/anyio",
159 ] {
160 assert!(
161 index.is_prefix_for(&SafeUrl::parse(url).unwrap()),
162 "Failed to match URL with prefix: {url}"
163 );
164 }
165
166 for url in [
167 "https://example.com/simpleevil",
168 "https://example.com/simple-evil",
169 "https://example.com/simpl",
170 ] {
171 assert!(
172 !index.is_prefix_for(&SafeUrl::parse(url).unwrap()),
173 "Should not match URL with partial path segment: {url}"
174 );
175 }
176 }
177
178 #[test]
179 fn test_strip_path_prefix() {
180 assert_eq!(strip_path_prefix("/files", "/files"), Some(""));
181 assert_eq!(strip_path_prefix("/files/", "/files"), None);
182 assert_eq!(
183 strip_path_prefix("/files/", "/files//wheel.whl"),
184 Some("/wheel.whl")
185 );
186 assert_eq!(
187 strip_path_prefix("/files", "/files//wheel.whl"),
188 Some("/wheel.whl")
189 );
190 }
191}