1use std::borrow::Cow;
2use std::fmt::{Display, Formatter};
3use std::ops::Deref;
4use std::path::Path;
5use std::str::FromStr;
6use std::sync::{Arc, LazyLock, RwLock};
7
8use itertools::Either;
9use rustc_hash::{FxHashMap, FxHashSet};
10use thiserror::Error;
11use url::{ParseError, Url};
12use uv_auth::RealmRef;
13use uv_cache_key::CanonicalUrl;
14use uv_pep508::{Scheme, VerbatimUrl, VerbatimUrlError, split_scheme};
15use uv_redacted::DisplaySafeUrl;
16use uv_warnings::warn_user;
17
18use crate::{Index, IndexStatusCodeStrategy, Verbatim};
19
20static PYPI_URL: LazyLock<DisplaySafeUrl> =
21 LazyLock::new(|| DisplaySafeUrl::parse("https://pypi.org/simple").unwrap());
22
23static DEFAULT_INDEX: LazyLock<Index> = LazyLock::new(|| {
24 Index::from_index_url(IndexUrl::Pypi(Arc::new(VerbatimUrl::from_url(
25 PYPI_URL.clone(),
26 ))))
27});
28
29#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
31pub enum IndexUrl {
32 Pypi(Arc<VerbatimUrl>),
33 Url(Arc<VerbatimUrl>),
34 Path(Arc<VerbatimUrl>),
35}
36
37impl IndexUrl {
38 pub fn parse(path: &str, root_dir: Option<&Path>) -> Result<Self, IndexUrlError> {
43 let url = VerbatimUrl::from_url_or_path(path, root_dir)?;
44 Ok(Self::from(url))
45 }
46
47 pub fn root(&self) -> Option<DisplaySafeUrl> {
52 let mut segments = self.url().path_segments()?;
53 let last = match segments.next_back()? {
54 "" => segments.next_back()?,
56 segment => segment,
57 };
58
59 if !(last.eq_ignore_ascii_case("simple") || last.eq_ignore_ascii_case("+simple")) {
61 return None;
62 }
63
64 let mut url = self.url().clone();
65 url.path_segments_mut().ok()?.pop_if_empty().pop();
66 Some(url)
67 }
68}
69
70#[cfg(feature = "schemars")]
71impl schemars::JsonSchema for IndexUrl {
72 fn schema_name() -> Cow<'static, str> {
73 Cow::Borrowed("IndexUrl")
74 }
75
76 fn json_schema(_generator: &mut schemars::generate::SchemaGenerator) -> schemars::Schema {
77 schemars::json_schema!({
78 "type": "string",
79 "description": "The URL of an index to use for fetching packages (e.g., `https://pypi.org/simple`), or a local path."
80 })
81 }
82}
83
84impl IndexUrl {
85 #[inline]
86 fn inner(&self) -> &VerbatimUrl {
87 match self {
88 Self::Pypi(url) | Self::Url(url) | Self::Path(url) => url,
89 }
90 }
91
92 pub fn url(&self) -> &DisplaySafeUrl {
94 self.inner().raw()
95 }
96
97 pub fn into_url(self) -> DisplaySafeUrl {
99 self.inner().to_url()
100 }
101
102 pub fn without_credentials(&self) -> Cow<'_, DisplaySafeUrl> {
104 let url = self.url();
105 if url.username().is_empty() && url.password().is_none() {
106 Cow::Borrowed(url)
107 } else {
108 let mut url = url.clone();
109 let _ = url.set_username("");
110 let _ = url.set_password(None);
111 Cow::Owned(url)
112 }
113 }
114
115 pub fn warn_on_disambiguated_relative_path(&self) {
120 let Self::Path(verbatim_url) = &self else {
121 return;
122 };
123
124 if let Some(path) = verbatim_url.given() {
125 if !is_disambiguated_path(path) {
126 if cfg!(windows) {
127 warn_user!(
128 "Relative paths passed to `--index` or `--default-index` should be disambiguated from index names (use `.\\{path}` or `./{path}`). Support for ambiguous values will be removed in the future"
129 );
130 } else {
131 warn_user!(
132 "Relative paths passed to `--index` or `--default-index` should be disambiguated from index names (use `./{path}`). Support for ambiguous values will be removed in the future"
133 );
134 }
135 }
136 }
137 }
138}
139
140impl Display for IndexUrl {
141 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
142 Display::fmt(self.inner(), f)
143 }
144}
145
146impl Verbatim for IndexUrl {
147 fn verbatim(&self) -> Cow<'_, str> {
148 self.inner().verbatim()
149 }
150}
151
152fn is_disambiguated_path(path: &str) -> bool {
158 if cfg!(windows) {
159 if path.starts_with(".\\") || path.starts_with("..\\") || path.starts_with('/') {
160 return true;
161 }
162 }
163 if path.starts_with("./") || path.starts_with("../") || Path::new(path).is_absolute() {
164 return true;
165 }
166 if let Some((scheme, _)) = split_scheme(path) {
168 return Scheme::parse(scheme).is_some();
169 }
170 false
172}
173
174#[derive(Error, Debug)]
176pub enum IndexUrlError {
177 #[error(transparent)]
178 Io(#[from] std::io::Error),
179 #[error(transparent)]
180 Url(#[from] ParseError),
181 #[error(transparent)]
182 VerbatimUrl(#[from] VerbatimUrlError),
183}
184
185impl FromStr for IndexUrl {
186 type Err = IndexUrlError;
187
188 fn from_str(s: &str) -> Result<Self, Self::Err> {
189 Self::parse(s, None)
190 }
191}
192
193impl serde::ser::Serialize for IndexUrl {
194 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
195 where
196 S: serde::ser::Serializer,
197 {
198 self.inner().without_credentials().serialize(serializer)
199 }
200}
201
202impl<'de> serde::de::Deserialize<'de> for IndexUrl {
203 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
204 where
205 D: serde::de::Deserializer<'de>,
206 {
207 struct Visitor;
208
209 impl serde::de::Visitor<'_> for Visitor {
210 type Value = IndexUrl;
211
212 fn expecting(&self, f: &mut Formatter) -> std::fmt::Result {
213 f.write_str("a string")
214 }
215
216 fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
217 IndexUrl::from_str(v).map_err(serde::de::Error::custom)
218 }
219 }
220
221 deserializer.deserialize_str(Visitor)
222 }
223}
224
225impl From<VerbatimUrl> for IndexUrl {
226 fn from(url: VerbatimUrl) -> Self {
227 if url.scheme() == "file" {
228 Self::Path(Arc::new(url))
229 } else if *url.raw() == *PYPI_URL {
230 Self::Pypi(Arc::new(url))
231 } else {
232 Self::Url(Arc::new(url))
233 }
234 }
235}
236
237impl From<IndexUrl> for DisplaySafeUrl {
238 fn from(index: IndexUrl) -> Self {
239 index.inner().to_url()
240 }
241}
242
243impl Deref for IndexUrl {
244 type Target = Url;
245
246 fn deref(&self) -> &Self::Target {
247 self.inner()
248 }
249}
250
251#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
256#[serde(rename_all = "kebab-case", deny_unknown_fields)]
257pub struct IndexLocations {
258 indexes: Vec<Index>,
259 flat_index: Vec<Index>,
260 no_index: bool,
261}
262
263impl IndexLocations {
264 pub fn new(indexes: Vec<Index>, flat_index: Vec<Index>, no_index: bool) -> Self {
266 Self {
267 indexes,
268 flat_index,
269 no_index,
270 }
271 }
272
273 #[must_use]
280 pub fn combine(self, indexes: Vec<Index>, flat_index: Vec<Index>, no_index: bool) -> Self {
281 Self {
282 indexes: self.indexes.into_iter().chain(indexes).collect(),
283 flat_index: self.flat_index.into_iter().chain(flat_index).collect(),
284 no_index: self.no_index || no_index,
285 }
286 }
287
288 pub fn is_none(&self) -> bool {
291 *self == Self::default()
292 }
293}
294
295fn is_same_index(a: &IndexUrl, b: &IndexUrl) -> bool {
297 RealmRef::from(&**b.url()) == RealmRef::from(&**a.url())
298 && CanonicalUrl::new(a.url()) == CanonicalUrl::new(b.url())
299}
300
301impl<'a> IndexLocations {
302 pub fn default_index(&'a self) -> Option<&'a Index> {
308 if self.no_index {
309 None
310 } else {
311 let mut seen = FxHashSet::default();
312 self.indexes
313 .iter()
314 .filter(move |index| index.name.as_ref().is_none_or(|name| seen.insert(name)))
315 .find(|index| index.default)
316 .or_else(|| Some(&DEFAULT_INDEX))
317 }
318 }
319
320 pub fn implicit_indexes(&'a self) -> impl Iterator<Item = &'a Index> + 'a {
324 if self.no_index {
325 Either::Left(std::iter::empty())
326 } else {
327 let mut seen = FxHashSet::default();
328 Either::Right(
329 self.indexes
330 .iter()
331 .filter(move |index| index.name.as_ref().is_none_or(|name| seen.insert(name)))
332 .filter(|index| !index.default && !index.explicit),
333 )
334 }
335 }
336
337 pub fn indexes(&'a self) -> impl Iterator<Item = &'a Index> + 'a {
346 self.implicit_indexes()
347 .chain(self.default_index())
348 .filter(|index| !index.explicit)
349 }
350
351 pub fn simple_indexes(&'a self) -> impl Iterator<Item = &'a Index> + 'a {
355 if self.no_index {
356 Either::Left(std::iter::empty())
357 } else {
358 let mut seen = FxHashSet::default();
359 Either::Right(
360 self.indexes
361 .iter()
362 .filter(move |index| index.name.as_ref().is_none_or(|name| seen.insert(name))),
363 )
364 }
365 }
366
367 pub fn flat_indexes(&'a self) -> impl Iterator<Item = &'a Index> + 'a {
369 self.flat_index.iter()
370 }
371
372 pub fn no_index(&self) -> bool {
374 self.no_index
375 }
376
377 pub fn index_urls(&'a self) -> IndexUrls {
379 IndexUrls {
380 indexes: self.indexes.clone(),
381 no_index: self.no_index,
382 }
383 }
384
385 pub fn allowed_indexes(&'a self) -> Vec<&'a Index> {
392 if self.no_index {
393 self.flat_index.iter().rev().collect()
394 } else {
395 let mut indexes = vec![];
396
397 let mut seen = FxHashSet::default();
398 let mut default = false;
399 for index in {
400 self.indexes
401 .iter()
402 .chain(self.flat_index.iter())
403 .filter(move |index| index.name.as_ref().is_none_or(|name| seen.insert(name)))
404 } {
405 if index.default {
406 if default {
407 continue;
408 }
409 default = true;
410 }
411 indexes.push(index);
412 }
413 if !default {
414 indexes.push(&*DEFAULT_INDEX);
415 }
416
417 indexes.reverse();
418 indexes
419 }
420 }
421
422 pub fn known_indexes(&'a self) -> impl Iterator<Item = &'a Index> {
431 if self.no_index {
432 Either::Left(self.flat_index.iter().rev())
433 } else {
434 Either::Right(
435 std::iter::once(&*DEFAULT_INDEX)
436 .chain(self.flat_index.iter().rev())
437 .chain(self.indexes.iter().rev()),
438 )
439 }
440 }
441
442 pub fn simple_api_cache_control_for(&self, url: &IndexUrl) -> Option<&str> {
444 for index in &self.indexes {
445 if is_same_index(index.url(), url) {
446 return index.simple_api_cache_control();
447 }
448 }
449 None
450 }
451
452 pub fn artifact_cache_control_for(&self, url: &IndexUrl) -> Option<&str> {
454 for index in &self.indexes {
455 if is_same_index(index.url(), url) {
456 return index.artifact_cache_control();
457 }
458 }
459 None
460 }
461}
462
463impl From<&IndexLocations> for uv_auth::Indexes {
464 fn from(index_locations: &IndexLocations) -> Self {
465 Self::from_indexes(index_locations.allowed_indexes().into_iter().map(|index| {
466 let mut url = index.url().url().clone();
467 url.set_username("").ok();
468 url.set_password(None).ok();
469 let mut root_url = index.url().root().unwrap_or_else(|| url.clone());
470 root_url.set_username("").ok();
471 root_url.set_password(None).ok();
472 uv_auth::Index {
473 url,
474 root_url,
475 auth_policy: index.authenticate,
476 }
477 }))
478 }
479}
480
481#[derive(Default, Debug, Clone, PartialEq, Eq)]
486pub struct IndexUrls {
487 indexes: Vec<Index>,
488 no_index: bool,
489}
490
491impl<'a> IndexUrls {
492 pub fn from_indexes(indexes: Vec<Index>) -> Self {
493 Self {
494 indexes,
495 no_index: false,
496 }
497 }
498
499 fn default_index(&'a self) -> Option<&'a Index> {
505 if self.no_index {
506 None
507 } else {
508 let mut seen = FxHashSet::default();
509 self.indexes
510 .iter()
511 .filter(move |index| index.name.as_ref().is_none_or(|name| seen.insert(name)))
512 .find(|index| index.default)
513 .or_else(|| Some(&DEFAULT_INDEX))
514 }
515 }
516
517 fn implicit_indexes(&'a self) -> impl Iterator<Item = &'a Index> + 'a {
521 if self.no_index {
522 Either::Left(std::iter::empty())
523 } else {
524 let mut seen = FxHashSet::default();
525 Either::Right(
526 self.indexes
527 .iter()
528 .filter(move |index| index.name.as_ref().is_none_or(|name| seen.insert(name)))
529 .filter(|index| !index.default && !index.explicit),
530 )
531 }
532 }
533
534 pub fn indexes(&'a self) -> impl Iterator<Item = &'a Index> + 'a {
542 let mut seen = FxHashSet::default();
543 self.implicit_indexes()
544 .chain(self.default_index())
545 .filter(|index| !index.explicit)
546 .filter(move |index| seen.insert(index.raw_url())) }
548
549 pub fn defined_indexes(&'a self) -> impl Iterator<Item = &'a Index> + 'a {
560 if self.no_index {
561 return Either::Left(std::iter::empty());
562 }
563
564 let mut seen = FxHashSet::default();
565 let (non_default, default) = self
566 .indexes
567 .iter()
568 .filter(move |index| {
569 if let Some(name) = &index.name {
570 seen.insert(name)
571 } else {
572 true
573 }
574 })
575 .partition::<Vec<_>, _>(|index| !index.default);
576
577 Either::Right(non_default.into_iter().chain(default))
578 }
579
580 pub fn no_index(&self) -> bool {
582 self.no_index
583 }
584
585 pub fn status_code_strategy_for(&self, url: &IndexUrl) -> IndexStatusCodeStrategy {
587 for index in &self.indexes {
588 if is_same_index(index.url(), url) {
589 return index.status_code_strategy();
590 }
591 }
592 IndexStatusCodeStrategy::Default
593 }
594
595 pub fn simple_api_cache_control_for(&self, url: &IndexUrl) -> Option<&str> {
597 for index in &self.indexes {
598 if is_same_index(index.url(), url) {
599 return index.simple_api_cache_control();
600 }
601 }
602 None
603 }
604
605 pub fn artifact_cache_control_for(&self, url: &IndexUrl) -> Option<&str> {
607 for index in &self.indexes {
608 if is_same_index(index.url(), url) {
609 return index.artifact_cache_control();
610 }
611 }
612 None
613 }
614}
615
616bitflags::bitflags! {
617 #[derive(Debug, Copy, Clone)]
618 struct Flags: u8 {
619 const NO_RANGE_REQUESTS = 1;
621 const UNAUTHORIZED = 1 << 2;
623 const FORBIDDEN = 1 << 1;
625 }
626}
627
628#[derive(Debug, Default, Clone)]
634pub struct IndexCapabilities(Arc<RwLock<FxHashMap<IndexUrl, Flags>>>);
635
636impl IndexCapabilities {
637 pub fn supports_range_requests(&self, index_url: &IndexUrl) -> bool {
639 !self
640 .0
641 .read()
642 .unwrap()
643 .get(index_url)
644 .is_some_and(|flags| flags.intersects(Flags::NO_RANGE_REQUESTS))
645 }
646
647 pub fn set_no_range_requests(&self, index_url: IndexUrl) {
649 self.0
650 .write()
651 .unwrap()
652 .entry(index_url)
653 .or_insert(Flags::empty())
654 .insert(Flags::NO_RANGE_REQUESTS);
655 }
656
657 pub fn unauthorized(&self, index_url: &IndexUrl) -> bool {
659 self.0
660 .read()
661 .unwrap()
662 .get(index_url)
663 .is_some_and(|flags| flags.intersects(Flags::UNAUTHORIZED))
664 }
665
666 pub fn set_unauthorized(&self, index_url: IndexUrl) {
668 self.0
669 .write()
670 .unwrap()
671 .entry(index_url)
672 .or_insert(Flags::empty())
673 .insert(Flags::UNAUTHORIZED);
674 }
675
676 pub fn forbidden(&self, index_url: &IndexUrl) -> bool {
678 self.0
679 .read()
680 .unwrap()
681 .get(index_url)
682 .is_some_and(|flags| flags.intersects(Flags::FORBIDDEN))
683 }
684
685 pub fn set_forbidden(&self, index_url: IndexUrl) {
687 self.0
688 .write()
689 .unwrap()
690 .entry(index_url)
691 .or_insert(Flags::empty())
692 .insert(Flags::FORBIDDEN);
693 }
694}
695
696#[cfg(test)]
697mod tests {
698 use super::*;
699 use crate::{IndexCacheControl, IndexFormat, IndexName};
700 use uv_small_str::SmallString;
701
702 #[test]
703 fn test_index_url_parse_valid_paths() {
704 assert!(is_disambiguated_path("/absolute/path"));
706 assert!(is_disambiguated_path("./relative/path"));
708 assert!(is_disambiguated_path("../../relative/path"));
709 if cfg!(windows) {
710 assert!(is_disambiguated_path("C:/absolute/path"));
712 assert!(is_disambiguated_path(".\\relative\\path"));
714 assert!(is_disambiguated_path("..\\..\\relative\\path"));
715 }
716 }
717
718 #[test]
719 fn test_index_url_parse_ambiguous_paths() {
720 assert!(!is_disambiguated_path("index"));
722 assert!(!is_disambiguated_path("relative/path"));
724 }
725
726 #[test]
727 fn test_index_url_parse_with_schemes() {
728 assert!(is_disambiguated_path("file:///absolute/path"));
729 assert!(is_disambiguated_path("https://registry.com/simple/"));
730 assert!(is_disambiguated_path(
731 "git+https://github.com/example/repo.git"
732 ));
733 }
734
735 #[test]
736 fn test_cache_control_lookup() {
737 use std::str::FromStr;
738
739 use uv_small_str::SmallString;
740
741 use crate::IndexFormat;
742 use crate::index_name::IndexName;
743
744 let indexes = vec![
745 Index {
746 name: Some(IndexName::from_str("index1").unwrap()),
747 url: IndexUrl::from_str("https://index1.example.com/simple").unwrap(),
748 cache_control: Some(crate::IndexCacheControl {
749 api: Some(SmallString::from("max-age=300")),
750 files: Some(SmallString::from("max-age=1800")),
751 }),
752 explicit: false,
753 default: false,
754 origin: None,
755 format: IndexFormat::Simple,
756 publish_url: None,
757 authenticate: uv_auth::AuthPolicy::default(),
758 ignore_error_codes: None,
759 },
760 Index {
761 name: Some(IndexName::from_str("index2").unwrap()),
762 url: IndexUrl::from_str("https://index2.example.com/simple").unwrap(),
763 cache_control: None,
764 explicit: false,
765 default: false,
766 origin: None,
767 format: IndexFormat::Simple,
768 publish_url: None,
769 authenticate: uv_auth::AuthPolicy::default(),
770 ignore_error_codes: None,
771 },
772 ];
773
774 let index_urls = IndexUrls::from_indexes(indexes);
775
776 let url1 = IndexUrl::from_str("https://index1.example.com/simple").unwrap();
777 assert_eq!(
778 index_urls.simple_api_cache_control_for(&url1),
779 Some("max-age=300")
780 );
781 assert_eq!(
782 index_urls.artifact_cache_control_for(&url1),
783 Some("max-age=1800")
784 );
785
786 let url2 = IndexUrl::from_str("https://index2.example.com/simple").unwrap();
787 assert_eq!(index_urls.simple_api_cache_control_for(&url2), None);
788 assert_eq!(index_urls.artifact_cache_control_for(&url2), None);
789
790 let url3 = IndexUrl::from_str("https://index3.example.com/simple").unwrap();
791 assert_eq!(index_urls.simple_api_cache_control_for(&url3), None);
792 assert_eq!(index_urls.artifact_cache_control_for(&url3), None);
793 }
794
795 #[test]
796 fn test_pytorch_default_cache_control() {
797 let indexes = vec![Index {
799 name: Some(IndexName::from_str("pytorch").unwrap()),
800 url: IndexUrl::from_str("https://download.pytorch.org/whl/cu118").unwrap(),
801 cache_control: None, explicit: false,
803 default: false,
804 origin: None,
805 format: IndexFormat::Simple,
806 publish_url: None,
807 authenticate: uv_auth::AuthPolicy::default(),
808 ignore_error_codes: None,
809 }];
810
811 let index_urls = IndexUrls::from_indexes(indexes.clone());
812 let index_locations = IndexLocations::new(indexes, Vec::new(), false);
813
814 let pytorch_url = IndexUrl::from_str("https://download.pytorch.org/whl/cu118").unwrap();
815
816 assert_eq!(index_urls.simple_api_cache_control_for(&pytorch_url), None);
818 assert_eq!(
819 index_urls.artifact_cache_control_for(&pytorch_url),
820 Some("max-age=365000000, immutable, public")
821 );
822
823 assert_eq!(
825 index_locations.simple_api_cache_control_for(&pytorch_url),
826 None
827 );
828 assert_eq!(
829 index_locations.artifact_cache_control_for(&pytorch_url),
830 Some("max-age=365000000, immutable, public")
831 );
832 }
833
834 #[test]
835 fn test_pytorch_user_override_cache_control() {
836 let indexes = vec![Index {
838 name: Some(IndexName::from_str("pytorch").unwrap()),
839 url: IndexUrl::from_str("https://download.pytorch.org/whl/cu118").unwrap(),
840 cache_control: Some(IndexCacheControl {
841 api: Some(SmallString::from("no-cache")),
842 files: Some(SmallString::from("max-age=3600")),
843 }),
844 explicit: false,
845 default: false,
846 origin: None,
847 format: IndexFormat::Simple,
848 publish_url: None,
849 authenticate: uv_auth::AuthPolicy::default(),
850 ignore_error_codes: None,
851 }];
852
853 let index_urls = IndexUrls::from_indexes(indexes.clone());
854 let index_locations = IndexLocations::new(indexes, Vec::new(), false);
855
856 let pytorch_url = IndexUrl::from_str("https://download.pytorch.org/whl/cu118").unwrap();
857
858 assert_eq!(
860 index_urls.simple_api_cache_control_for(&pytorch_url),
861 Some("no-cache")
862 );
863 assert_eq!(
864 index_urls.artifact_cache_control_for(&pytorch_url),
865 Some("max-age=3600")
866 );
867
868 assert_eq!(
870 index_locations.simple_api_cache_control_for(&pytorch_url),
871 Some("no-cache")
872 );
873 assert_eq!(
874 index_locations.artifact_cache_control_for(&pytorch_url),
875 Some("max-age=3600")
876 );
877 }
878}