Skip to content

Commit 6e63d64

Browse files
committed
Support relative URLs, closes #2252
This commit adds a `ToUrlWithBase` trait which is used when parsing the value of `registry.index` from a Cargo config file. The base url is the file that contained the URL. The net effect is that you can now use relative URLs when specifying a registry.
1 parent f8084b0 commit 6e63d64

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/cargo/sources/registry.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ use url::Url;
174174
use core::{Source, SourceId, PackageId, Package, Summary, Registry};
175175
use core::dependency::{Dependency, DependencyInner, Kind};
176176
use sources::{PathSource, git};
177-
use util::{CargoResult, Config, internal, ChainError, ToUrl, human};
177+
use util::{CargoResult, Config, internal, ChainError, ToUrl, ToUrlWithBase, human};
178178
use util::{hex, Sha256, paths};
179179
use ops;
180180

@@ -251,9 +251,12 @@ impl<'cfg> RegistrySource<'cfg> {
251251
/// This is the main cargo registry by default, but it can be overridden in
252252
/// a .cargo/config
253253
pub fn url(config: &Config) -> CargoResult<Url> {
254-
let config = try!(ops::registry_configuration(config));
255-
let url = config.index.unwrap_or(DEFAULT.to_string());
256-
url.to_url().map_err(human)
254+
let result = match try!(config.get_string("registry.index")) {
255+
Some((value, path)) => value.to_url_with_base(path.as_path().clone()),
256+
None => DEFAULT.to_string().to_url(),
257+
};
258+
259+
Ok(try!(result.map_err(human)))
257260
}
258261

259262
/// Get the default url for the registry

src/cargo/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use self::process_builder::{process, ProcessBuilder};
1414
pub use self::rustc::Rustc;
1515
pub use self::sha256::Sha256;
1616
pub use self::to_semver::ToSemver;
17-
pub use self::to_url::ToUrl;
17+
pub use self::to_url::{ToUrl, ToUrlWithBase};
1818
pub use self::vcs::{GitRepo, HgRepo};
1919

2020
pub mod config;

src/cargo/util/to_url.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ pub trait ToUrl {
55
fn to_url(self) -> Result<Url, String>;
66
}
77

8+
pub trait ToUrlWithBase {
9+
fn to_url_with_base<U: ToUrl>(self, base: U) -> Result<Url, String>;
10+
}
11+
812
impl ToUrl for Url {
913
fn to_url(self) -> Result<Url, String> {
1014
Ok(self)
@@ -25,6 +29,22 @@ impl<'a> ToUrl for &'a str {
2529
}
2630
}
2731

32+
impl<'a> ToUrlWithBase for &'a str {
33+
fn to_url_with_base<U: ToUrl>(self, base: U) -> Result<Url, String> {
34+
let base_url: Result<Url, String> = base.to_url().map_err(|s| {
35+
format!("invalid url `{}`: {}", self, s)
36+
});
37+
let base_url = try!(base_url);
38+
39+
UrlParser::new()
40+
.scheme_type_mapper(mapper)
41+
.base_url(&base_url)
42+
.parse(self).map_err(|s| {
43+
format!("invalid url `{}`: {}", self, s)
44+
})
45+
}
46+
}
47+
2848
impl<'a> ToUrl for &'a Path {
2949
fn to_url(self) -> Result<Url, String> {
3050
Url::from_file_path(self).map_err(|()| {

0 commit comments

Comments
 (0)