use std::path::{Path, PathBuf};
use url::Url;
use crate::error::{CoreError, ErrorCode};
use crate::model::sources::{SourceScope, key_shape};
pub const AGGREGATOR_HOSTS: [&str; 7] = [
"github.com",
"gitlab.com",
"bitbucket.org",
"codeberg.org",
"gitee.com",
"git.sr.ht",
"huggingface.co",
];
const CASE_INSENSITIVE_HOSTS: [&str; 4] =
["github.com", "gitlab.com", "bitbucket.org", "codeberg.org"];
pub const OFFICIAL_OWNERS: [&str; 2] = ["github.com/anthropics", "github.com/modelcontextprotocol"];
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Transport {
Https,
Http,
Ssh,
Git,
Scp,
Local,
}
impl Transport {
fn of_scheme(scheme: &str) -> Option<Self> {
match scheme.to_ascii_lowercase().as_str() {
"https" => Some(Transport::Https),
"http" => Some(Transport::Http),
"ssh" => Some(Transport::Ssh),
"git" => Some(Transport::Git),
_ => None,
}
}
const fn scheme(self) -> &'static str {
match self {
Transport::Https => "https",
Transport::Http => "http",
Transport::Ssh | Transport::Scp => "ssh",
Transport::Git => "git",
Transport::Local => "file",
}
}
const fn default_port(self) -> Option<u16> {
match self {
Transport::Https => Some(443),
Transport::Http => Some(80),
Transport::Ssh => Some(22),
Transport::Git => Some(9418),
Transport::Scp | Transport::Local => None,
}
}
const fn marker(self) -> &'static str {
match self {
Transport::Http => "http://",
Transport::Git => "git://",
_ => "",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SourceRef {
key: String,
url: String,
address: String,
host: Option<String>,
owner: Option<String>,
transport: Transport,
whole_host: bool,
}
struct Remote {
transport: Transport,
userinfo: Option<String>,
host: String,
absolute: bool,
segments: Vec<String>,
}
enum Parsed {
Remote(Remote),
Local(String),
}
fn invalid(text: &str, why: &str) -> CoreError {
CoreError::new(
ErrorCode::InvalidValue,
format!(
"{:?} is not a source: {why}",
SourceRef::redact_userinfo(text)
),
)
}
fn scp_like(text: &str) -> Option<(&str, &str)> {
let (user_host, rest) = text.split_once(':')?;
if !user_host.contains('@') || user_host.contains(['/', '\\']) || rest.starts_with("//") {
return None;
}
Some((user_host, rest))
}
fn is_windows_drive(text: &str) -> bool {
let bytes = text.as_bytes();
bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':'
}
fn is_local_path(text: &str) -> bool {
Path::new(text).is_absolute() || is_windows_drive(text) || text.starts_with('/')
}
fn check_text(text: &str) -> Result<(), &'static str> {
if text.chars().any(|c| c.is_control() || c.is_whitespace()) {
return Err("it contains spaces or control characters");
}
if text.contains('\\') {
return Err("it contains a backslash");
}
if text.contains(['?', '#']) {
return Err("a query or fragment (`?`, `#`) does not name a repository");
}
let lower = text.to_ascii_lowercase();
if ["%2e", "%2f", "%5c"]
.iter()
.any(|code| lower.contains(code))
{
return Err("it percent-encodes `.`, `/` or `\\`");
}
if text.matches('@').count() > 1 {
return Err("it has more than one `@`");
}
Ok(())
}
fn checked_segments(path: &str) -> Result<Vec<&str>, &'static str> {
let path = path.strip_suffix('/').unwrap_or(path);
if path.is_empty() {
return Ok(Vec::new());
}
path.split('/')
.map(|segment| match segment {
"" => Err("it has an empty path segment (`//`)"),
"." | ".." => Err("it has a `.` or `..` path segment"),
segment => Ok(segment),
})
.collect()
}
fn normalize_escapes(segment: &str) -> Result<String, &'static str> {
let mut out = String::with_capacity(segment.len());
let mut rest = segment;
while let Some(at) = rest.find('%') {
out.push_str(&rest[..at]);
let hex = rest
.get(at + 1..at + 3)
.filter(|hex| hex.bytes().all(|b| b.is_ascii_hexdigit()))
.ok_or("it has a `%` that is not a percent-encoded byte")?;
let byte = u8::from_str_radix(hex, 16).map_err(|_| "it has a bad percent escape")?;
if byte.is_ascii_alphanumeric() || b"-_~".contains(&byte) {
out.push(char::from(byte));
} else {
out.push('%');
out.push_str(&hex.to_ascii_uppercase());
}
rest = &rest[at + 3..];
}
out.push_str(rest);
Ok(out)
}
fn canonical_host(host: &str) -> Result<String, &'static str> {
let lower = host.to_ascii_lowercase();
let host = lower.strip_suffix('.').unwrap_or(&lower);
let ok = match host.strip_prefix('[').and_then(|ip| ip.strip_suffix(']')) {
Some(ip) => {
!ip.is_empty()
&& ip
.bytes()
.all(|b| b.is_ascii_hexdigit() || b == b':' || b == b'.')
}
None => {
!host.is_empty()
&& host.split('.').all(|label| {
!label.is_empty()
&& label.bytes().all(|b| {
b.is_ascii_lowercase() || b.is_ascii_digit() || b"-_".contains(&b)
})
})
}
};
if ok {
Ok(host.to_owned())
} else {
Err("its host is not a host name")
}
}
fn url_remote(text: &str, transport: Transport) -> Result<Remote, &'static str> {
check_text(text)?;
let rest = text.split_once("://").map_or("", |(_, rest)| rest);
let (authority, raw_path) = rest.split_once('/').unwrap_or((rest, ""));
if authority
.rsplit_once('@')
.map_or(authority, |(_, host)| host)
.is_empty()
{
return Err("it has no host");
}
if raw_path.contains('@') {
return Err("it has an `@` in the path");
}
let raw = checked_segments(raw_path)?;
let parsed = Url::parse(text).map_err(|_| "it is not a valid URL")?;
let host = canonical_host(parsed.host_str().ok_or("it has no host")?)?;
let host = match parsed.port() {
Some(port) if Some(port) != transport.default_port() => format!("{host}:{port}"),
_ => host,
};
let userinfo = match (parsed.username(), parsed.password()) {
("", None) => None,
(user, None) => Some(user.to_owned()),
(user, Some(password)) => Some(format!("{user}:{password}")),
};
let path = parsed.path();
let path = path.strip_prefix('/').unwrap_or(path);
let segments = checked_segments(path)?
.into_iter()
.map(normalize_escapes)
.collect::<Result<Vec<_>, _>>()?;
if segments.len() != raw.len() {
return Err("it is not a valid URL");
}
Ok(Remote {
transport,
userinfo,
host,
absolute: true,
segments,
})
}
fn scp_remote(text: &str, user_host: &str, path: &str) -> Result<Remote, &'static str> {
check_text(text)?;
if path.contains(['%', '@']) {
return Err("an scp-style path cannot contain `%` or `@`");
}
let (user, host) = user_host.rsplit_once('@').unwrap_or(("", user_host));
let host = canonical_host(host)?;
let absolute = path.starts_with('/');
let segments = checked_segments(path.strip_prefix('/').unwrap_or(path))?
.into_iter()
.map(str::to_owned)
.collect();
Ok(Remote {
transport: Transport::Scp,
userinfo: (!user.is_empty()).then(|| user.to_owned()),
host,
absolute,
segments,
})
}
impl SourceRef {
pub fn from_git_url(url: &str) -> Result<Self, CoreError> {
let text = url.trim();
Self::parse(text, text).map(|(source, _)| source)
}
pub fn from_key_or_url(text: &str) -> Result<Self, CoreError> {
let text = text.trim();
Self::parse(&as_url(text), text).map(|(source, _)| source)
}
pub fn canonical_key(text: &str) -> Result<(String, SourceScope), CoreError> {
let text = text.trim();
let Some(base) = text.strip_suffix("/*") else {
return Ok((Self::from_key_or_url(text)?.key, SourceScope::Repo));
};
let Parsed::Remote(remote) = Self::split(&as_url(&format!("{base}/x")), text)? else {
return Err(invalid(text, "a local source has no owner or host key"));
};
match remote.segments.as_slice() {
[_] => Ok((
format!("{}{}/*", remote.transport.marker(), remote.host),
SourceScope::Host,
)),
[_, _] => {
let (source, _) = Self::from_remote(remote).map_err(|why| invalid(text, why))?;
match source.scope_key(SourceScope::Owner) {
Some(key) => Ok((key, SourceScope::Owner)),
None => Err(invalid(text, "it has no owner")),
}
}
_ => Err(invalid(
text,
"a key ending in `/*` is host/* (a host) or host/owner/* (an owner)",
)),
}
}
pub fn redact_userinfo(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(at) = rest.find("://") {
let (head, tail) = rest.split_at(at + 3);
out.push_str(head);
let end = tail
.find(|c: char| c == '/' || c.is_whitespace() || matches!(c, '"' | '\'' | '`'))
.unwrap_or(tail.len());
match tail[..end].rfind('@') {
Some(at) => {
out.push_str("***");
out.push_str(&tail[at..end]);
}
None => out.push_str(&tail[..end]),
}
rest = &tail[end..];
}
out.push_str(rest);
out
}
fn split(text: &str, shown: &str) -> Result<Parsed, CoreError> {
if text.to_ascii_lowercase().starts_with("file://") {
let path = &text["file://".len()..];
return Ok(Parsed::Local(match path.strip_prefix('/') {
Some(rest) if is_windows_drive(rest) => rest.to_owned(),
Some(_) => path.to_owned(),
None => format!("//{path}"),
}));
}
let remote = if let Some((scheme, _)) = text.split_once("://") {
let transport = Transport::of_scheme(scheme)
.ok_or_else(|| invalid(shown, "unsupported URL scheme"))?;
url_remote(text, transport)
} else if let Some((user_host, path)) = scp_like(text) {
scp_remote(text, user_host, path)
} else if is_local_path(text) {
return Ok(Parsed::Local(text.to_owned()));
} else {
return Err(invalid(shown, "not a git URL or an absolute path"));
};
remote
.map(Parsed::Remote)
.map_err(|why| invalid(shown, why))
}
fn parse(text: &str, shown: &str) -> Result<(Self, Option<String>), CoreError> {
match Self::split(text, shown)? {
Parsed::Local(path) => Ok((Self::local(&path, shown)?, None)),
Parsed::Remote(remote) => Self::from_remote(remote).map_err(|why| invalid(shown, why)),
}
}
pub fn from_local_path(path: &Path) -> Result<Self, CoreError> {
let text = path.to_string_lossy();
if !is_local_path(&text) {
return Err(invalid(&text, "not an absolute path"));
}
Self::local(&text, &text)
}
pub fn whole_host(url: &str) -> Result<Self, CoreError> {
let text = url.trim();
let parsed = Url::parse(text).map_err(|_| invalid(text, "it is not a valid URL"))?;
let transport = match parsed.scheme() {
"https" => Transport::Https,
"http" => Transport::Http,
_ => return Err(invalid(text, "unsupported URL scheme")),
};
if !parsed.username().is_empty() || parsed.password().is_some() {
return Err(invalid(text, "it carries a user name or password"));
}
let host = canonical_host(
parsed
.host_str()
.ok_or_else(|| invalid(text, "it has no host"))?,
)
.map_err(|why| invalid(text, why))?;
let host = match parsed.port() {
Some(port) if Some(port) != transport.default_port() => format!("{host}:{port}"),
_ => host,
};
let key = format!("{}{host}/*", transport.marker());
if key_shape(&key) != Some(SourceScope::Host) {
return Err(invalid(text, "its host cannot be a source key"));
}
Ok(Self {
url: format!("{}://{host}", transport.scheme()),
address: host.clone(),
key,
host: Some(host),
owner: None,
transport,
whole_host: true,
})
}
fn local(path: &str, original: &str) -> Result<Self, CoreError> {
let mut path = path.replace('\\', "/");
while path.len() > 1 && path.ends_with('/') {
path.pop();
}
if is_windows_drive(&path) {
path[..1].make_ascii_uppercase();
}
let rest = if path.starts_with("//") {
path.as_str()
} else {
path.trim_start_matches('/')
};
let key = format!("file:///{rest}");
if key_shape(&key) != Some(SourceScope::Repo) {
return Err(invalid(original, "not a usable local path"));
}
Ok(Self {
url: key.clone(),
key,
address: String::new(),
host: None,
owner: None,
transport: Transport::Local,
whole_host: false,
})
}
fn from_remote(remote: Remote) -> Result<(Self, Option<String>), &'static str> {
let Remote {
transport,
userinfo,
host,
absolute,
mut segments,
} = remote;
if let Some(cut) = segments.iter().position(|part| part == "-") {
segments.truncate(cut);
}
if host == "github.com" {
segments.truncate(2);
}
if CASE_INSENSITIVE_HOSTS.contains(&host.as_str()) {
segments
.iter_mut()
.for_each(|part| part.make_ascii_lowercase());
}
let dot_git = match segments.last_mut() {
Some(last) if last.ends_with(".git") => {
last.truncate(last.len() - ".git".len());
if last.is_empty() {
return Err("it has no repository name");
}
true
}
_ => false,
};
if segments.is_empty() {
return Err("it has no repository path");
}
let path = segments.join("/");
let key = format!("{}{host}/{path}", transport.marker());
if key_shape(&key) != Some(SourceScope::Repo) {
return Err("it has characters a source key cannot hold");
}
let fetch_path = if dot_git { format!("{path}.git") } else { path };
let address = if transport == Transport::Scp {
format!("{host}:{}{fetch_path}", if absolute { "/" } else { "" })
} else {
format!("{host}/{fetch_path}")
};
let user = match transport {
Transport::Ssh | Transport::Scp => userinfo
.as_deref()
.map(|info| info.split_once(':').map_or(info, |(user, _)| user)),
_ => None,
};
let url = with_userinfo(transport, user, &address);
let source = Self {
key,
url,
address,
owner: (segments.len() >= 2).then(|| segments[0].clone()),
host: Some(host),
transport,
whole_host: false,
};
Ok((source, userinfo))
}
pub fn key(&self) -> &str {
&self.key
}
pub fn url(&self) -> &str {
&self.url
}
pub fn fetch_url(&self, given: &str) -> String {
let given = given.trim();
let Ok((other, userinfo)) = Self::parse(given, "") else {
return self.url.clone();
};
let same = other.transport == self.transport
&& other.key == self.key
&& other.address == self.address;
match (same, self.transport, userinfo) {
(true, Transport::Local, _) => given.to_owned(),
(true, _, Some(userinfo)) => {
with_userinfo(self.transport, Some(&userinfo), &self.address)
}
_ => self.url.clone(),
}
}
pub fn is_insecure(&self) -> bool {
!self.transport.marker().is_empty()
}
pub fn is_local(&self) -> bool {
self.host.is_none()
}
pub fn local_path(&self) -> Option<PathBuf> {
if !self.is_local() {
return None;
}
let rest = self.key.strip_prefix("file:///")?;
Some(if is_windows_drive(rest) || rest.starts_with("//") {
PathBuf::from(rest)
} else {
PathBuf::from(format!("/{rest}"))
})
}
pub fn host(&self) -> Option<&str> {
self.host.as_deref()
}
pub fn owner(&self) -> Option<&str> {
self.owner.as_deref()
}
pub fn scope_key(&self, scope: SourceScope) -> Option<String> {
let marker = self.transport.marker();
match scope {
SourceScope::Repo => Some(self.key.clone()),
SourceScope::Owner => Some(format!(
"{marker}{}/{}/*",
self.host.as_ref()?,
self.owner.as_ref()?
)),
SourceScope::Host => Some(format!("{marker}{}/*", self.host.as_ref()?)),
}
}
pub fn matching_keys(&self) -> Vec<String> {
let mut keys: Vec<String> = [SourceScope::Repo, SourceScope::Owner, SourceScope::Host]
.into_iter()
.filter_map(|scope| self.scope_key(scope))
.collect();
keys.dedup();
keys
}
pub fn block_keys(&self) -> Vec<String> {
let mut keys = self.matching_keys();
let marker = self.transport.marker();
if !marker.is_empty() {
let secure: Vec<String> = keys
.iter()
.filter_map(|key| key.strip_prefix(marker).map(str::to_owned))
.collect();
keys.extend(secure);
}
keys
}
pub fn scopes(&self) -> Vec<SourceScope> {
if self.whole_host {
return match &self.host {
Some(host) if !AGGREGATOR_HOSTS.contains(&host.as_str()) => {
vec![SourceScope::Host]
}
_ => Vec::new(),
};
}
let mut scopes = vec![SourceScope::Repo];
if self.owner.is_some() {
scopes.push(SourceScope::Owner);
}
if let Some(host) = &self.host
&& !AGGREGATOR_HOSTS.contains(&host.as_str())
{
scopes.push(SourceScope::Host);
}
scopes
}
pub fn is_official(&self) -> bool {
match (&self.host, &self.owner) {
(Some(host), Some(owner)) if !self.is_insecure() => {
OFFICIAL_OWNERS.contains(&format!("{host}/{owner}").as_str())
}
_ => false,
}
}
}
fn with_userinfo(transport: Transport, userinfo: Option<&str>, address: &str) -> String {
let at = userinfo.map(|info| format!("{info}@")).unwrap_or_default();
match transport {
Transport::Scp => format!("{at}{address}"),
_ => format!("{}://{at}{address}", transport.scheme()),
}
}
fn as_url(text: &str) -> String {
if text.contains("://") || scp_like(text).is_some() || is_local_path(text) {
text.to_owned()
} else {
format!("https://{text}")
}
}