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

Support relative URLs, closes #2252 #2253

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 7 additions & 4 deletions src/cargo/sources/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ use url::Url;
use core::{Source, SourceId, PackageId, Package, Summary, Registry};
use core::dependency::{Dependency, DependencyInner, Kind};
use sources::{PathSource, git};
use util::{CargoResult, Config, internal, ChainError, ToUrl, human};
use util::{CargoResult, Config, internal, ChainError, ToUrl, ToUrlWithBase, human};
use util::{hex, Sha256, paths};
use ops;

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

Ok(try!(result.map_err(human)))
}

/// Get the default url for the registry
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use self::process_builder::{process, ProcessBuilder};
pub use self::rustc::Rustc;
pub use self::sha256::Sha256;
pub use self::to_semver::ToSemver;
pub use self::to_url::ToUrl;
pub use self::to_url::{ToUrl, ToUrlWithBase};
pub use self::vcs::{GitRepo, HgRepo};

pub mod config;
Expand Down
20 changes: 20 additions & 0 deletions src/cargo/util/to_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ pub trait ToUrl {
fn to_url(self) -> Result<Url, String>;
}

pub trait ToUrlWithBase {
fn to_url_with_base<U: ToUrl>(self, base: U) -> Result<Url, String>;
}

impl ToUrl for Url {
fn to_url(self) -> Result<Url, String> {
Ok(self)
Expand All @@ -25,6 +29,22 @@ impl<'a> ToUrl for &'a str {
}
}

impl<'a> ToUrlWithBase for &'a str {
fn to_url_with_base<U: ToUrl>(self, base: U) -> Result<Url, String> {
let base_url: Result<Url, String> = base.to_url().map_err(|s| {
format!("invalid url `{}`: {}", self, s)
});
let base_url = try!(base_url);

UrlParser::new()
.scheme_type_mapper(mapper)
.base_url(&base_url)
.parse(self).map_err(|s| {
format!("invalid url `{}`: {}", self, s)
})
}
}

impl<'a> ToUrl for &'a Path {
fn to_url(self) -> Result<Url, String> {
Url::from_file_path(self).map_err(|()| {
Expand Down