use std::collections::BTreeMap;
use crate::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Method {
Get,
Post,
Put,
Patch,
Delete,
}
impl Method {
pub fn as_str(self) -> &'static str {
match self {
Self::Get => "GET",
Self::Post => "POST",
Self::Put => "PUT",
Self::Patch => "PATCH",
Self::Delete => "DELETE",
}
}
}
#[derive(Debug, Clone)]
pub struct HttpRequest {
pub method: Method,
pub url: String,
pub headers: Vec<(String, String)>,
pub body: Option<String>,
}
impl HttpRequest {
pub fn get(url: impl Into<String>) -> Self {
Self {
method: Method::Get,
url: url.into(),
headers: Vec::new(),
body: None,
}
}
pub fn delete(url: impl Into<String>) -> Self {
Self {
method: Method::Delete,
url: url.into(),
headers: Vec::new(),
body: None,
}
}
pub fn post_json(url: impl Into<String>, body: &serde_json::Value) -> Self {
Self {
method: Method::Post,
url: url.into(),
headers: vec![("Content-Type".into(), "application/json".into())],
body: Some(body.to_string()),
}
}
pub fn put_json(url: impl Into<String>, body: &serde_json::Value) -> Self {
Self {
method: Method::Put,
..Self::post_json(url, body)
}
}
pub fn patch_json(url: impl Into<String>, body: &serde_json::Value) -> Self {
Self {
method: Method::Patch,
..Self::post_json(url, body)
}
}
pub fn delete_json(url: impl Into<String>, body: &serde_json::Value) -> Self {
Self {
method: Method::Delete,
..Self::post_json(url, body)
}
}
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.push((name.into(), value.into()));
self
}
}
#[derive(Debug, Clone)]
pub struct HttpResponse {
pub status: u16,
pub headers: BTreeMap<String, String>,
pub body: String,
}
impl HttpResponse {
pub fn is_success(&self) -> bool {
(200..300).contains(&self.status)
}
pub fn error_for_status(&self, what: &str) -> Result<()> {
if self.is_success() {
return Ok(());
}
if crate::conditional::is_rate_limited(self) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let wait = crate::conditional::rate_state(self, now)
.map(|(_, reset)| reset.saturating_sub(now))
.unwrap_or(60);
return Err(Error::RateLimited {
host: what.to_string(),
retry_in_secs: wait.max(1),
});
}
let detail = host_message(&self.body).unwrap_or_else(|| self.body.trim().to_string());
let detail = truncate(&detail, 300);
Err(match self.status {
401 => Error::Unauthorized {
what: what.to_string(),
detail,
},
403 => Error::Forbidden {
what: what.to_string(),
detail,
},
status => Error::Api {
what: what.to_string(),
status,
detail,
},
})
}
pub fn json<T: serde::de::DeserializeOwned>(&self) -> Result<T> {
serde_json::from_str(&self.body).map_err(|e| Error::Decode(e.to_string()))
}
}
#[derive(Debug, Clone)]
pub struct HttpBytes {
pub status: u16,
pub headers: BTreeMap<String, String>,
pub body: Vec<u8>,
}
fn host_message(body: &str) -> Option<String> {
let value: serde_json::Value = serde_json::from_str(body).ok()?;
for key in ["message", "error", "error_description"] {
match value.get(key) {
Some(serde_json::Value::String(text)) => {
let extra: Vec<String> = value
.get("errors")
.and_then(|e| e.as_array())
.map(|rows| {
rows.iter()
.map(|row| match row {
serde_json::Value::String(s) => s.clone(),
other => other
.get("message")
.and_then(|m| m.as_str())
.map(str::to_string)
.unwrap_or_else(|| {
let field = other
.get("field")
.and_then(|f| f.as_str())
.unwrap_or("?");
let code = other
.get("code")
.and_then(|c| c.as_str())
.unwrap_or("invalid");
format!("{field}: {code}")
}),
})
.collect()
})
.unwrap_or_default();
return Some(if extra.is_empty() {
text.clone()
} else {
format!("{text} ({})", extra.join("; "))
});
}
Some(obj @ serde_json::Value::Object(_)) => return Some(obj.to_string()),
_ => {}
}
}
None
}
fn truncate(text: &str, max: usize) -> String {
let text = text.trim();
if text.chars().count() <= max {
return text.to_string();
}
let cut: String = text.chars().take(max).collect();
format!("{cut}...")
}
#[async_trait::async_trait]
pub trait HttpTransport: Send + Sync {
async fn send(&self, request: HttpRequest) -> Result<HttpResponse>;
async fn download(&self, request: HttpRequest, progress: &Progress<'_>) -> Result<Vec<u8>> {
let _ = (request, progress);
Err(Error::Http("this transport cannot download files".into()))
}
async fn fetch_bytes(&self, request: HttpRequest) -> Result<HttpBytes> {
let _ = request;
Err(Error::Http(
"this transport cannot fetch binary bodies".into(),
))
}
}
pub type Progress<'a> = dyn Fn(u64, Option<u64>) -> bool + Send + Sync + 'a;
#[derive(Default)]
pub struct RecordedTransport {
responses: std::sync::Mutex<Vec<(String, HttpResponse)>>,
files: std::sync::Mutex<Vec<(String, Vec<u8>)>>,
seen: std::sync::Mutex<Vec<HttpRequest>>,
}
impl RecordedTransport {
pub fn new() -> Self {
Self::default()
}
pub fn on(self, url_fragment: &str, status: u16, body: &str) -> Self {
self.responses.lock().unwrap().push((
url_fragment.to_string(),
HttpResponse {
status,
headers: BTreeMap::new(),
body: body.to_string(),
},
));
self
}
pub fn on_with_headers(
self,
url_fragment: &str,
status: u16,
body: &str,
headers: &[(&str, &str)],
) -> Self {
self.responses.lock().unwrap().push((
url_fragment.to_string(),
HttpResponse {
status,
headers: headers
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
body: body.to_string(),
},
));
self
}
pub fn on_file(self, url_fragment: &str, bytes: Vec<u8>) -> Self {
self.files
.lock()
.unwrap()
.push((url_fragment.to_string(), bytes));
self
}
pub fn requests(&self) -> Vec<HttpRequest> {
self.seen.lock().unwrap().clone()
}
}
#[async_trait::async_trait]
impl HttpTransport for RecordedTransport {
async fn download(&self, request: HttpRequest, progress: &Progress<'_>) -> Result<Vec<u8>> {
self.seen.lock().unwrap().push(request.clone());
let bytes = {
let files = self.files.lock().unwrap();
files
.iter()
.find(|(fragment, _)| request.url.contains(fragment.as_str()))
.map(|(_, bytes)| bytes.clone())
};
let Some(bytes) = bytes else {
return Err(Error::Http(format!(
"RecordedTransport has no file for {}",
request.url
)));
};
let total = bytes.len() as u64;
let mut received = 0u64;
for chunk in bytes.chunks(64 * 1024) {
received += chunk.len() as u64;
if !progress(received, Some(total)) {
return Err(Error::Http("download canceled".into()));
}
}
Ok(bytes)
}
async fn fetch_bytes(&self, request: HttpRequest) -> Result<HttpBytes> {
self.seen.lock().unwrap().push(request.clone());
let file = {
let files = self.files.lock().unwrap();
files
.iter()
.find(|(fragment, _)| request.url.contains(fragment.as_str()))
.map(|(_, bytes)| bytes.clone())
};
if let Some(body) = file {
return Ok(HttpBytes {
status: 200,
headers: BTreeMap::new(),
body,
});
}
let responses = self.responses.lock().unwrap();
match responses
.iter()
.find(|(fragment, _)| request.url.contains(fragment.as_str()))
{
Some((_, response)) => Ok(HttpBytes {
status: response.status,
headers: response.headers.clone(),
body: response.body.clone().into_bytes(),
}),
None => Err(Error::Http(format!(
"RecordedTransport has no response for {}",
request.url
))),
}
}
async fn send(&self, request: HttpRequest) -> Result<HttpResponse> {
self.seen.lock().unwrap().push(request.clone());
let responses = self.responses.lock().unwrap();
let position = responses
.iter()
.position(|(fragment, _)| request.url.contains(fragment.as_str()));
match position {
Some(i) => Ok(responses[i].1.clone()),
None => Err(Error::Http(format!(
"RecordedTransport has no response for {} {}",
request.method.as_str(),
request.url
))),
}
}
}
pub const BLOCK_EXTERNAL_ENV: &str = "GITPOULPI_BLOCK_EXTERNAL_HOSTS";
pub const LIVE_TESTS_ENV: &str = "GITPOULPI_LIVE_TESTS";
pub fn external_host_refusal(url: &str) -> Option<String> {
let on = |name: &str| matches!(std::env::var(name).as_deref(), Ok("1") | Ok("true"));
refusal(
url,
cfg!(test) || on(BLOCK_EXTERNAL_ENV),
on(LIVE_TESTS_ENV),
)
}
fn refusal(url: &str, blocking: bool, live: bool) -> Option<String> {
if !blocking || live {
return None;
}
let host = url_host(url)?;
if is_loopback_host(&host) {
return None;
}
Some(format!(
"refusing to connect to {host} from a test: tests only reach loopback; \
set {LIVE_TESTS_ENV}=1 for a live run"
))
}
fn url_host(url: &str) -> Option<String> {
let rest = url.split_once("://").map_or(url, |(_, r)| r);
let authority = rest.split(['/', '?', '#']).next()?;
let hostport = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
let host = if let Some(v6) = hostport.strip_prefix('[') {
v6.split(']').next()?
} else {
hostport.split(':').next()?
};
Some(host.to_ascii_lowercase())
}
fn is_loopback_host(host: &str) -> bool {
host == "localhost" || host.ends_with(".localhost") || host == "::1" || host.starts_with("127.")
}
#[cfg(feature = "network")]
fn same_origin_redirects() -> reqwest::redirect::Policy {
reqwest::redirect::Policy::custom(|attempt| {
let same = attempt.previous().last().is_some_and(|prev| {
prev.scheme() == attempt.url().scheme()
&& prev.host_str() == attempt.url().host_str()
&& prev.port_or_known_default() == attempt.url().port_or_known_default()
});
if attempt.previous().len() >= 5 {
attempt.error("too many redirects")
} else if same {
attempt.follow()
} else {
attempt.stop()
}
})
}
#[cfg(feature = "network")]
fn send_error(error: &reqwest::Error, url: &str) -> Error {
use std::error::Error as _;
let mut text = error.to_string();
let mut source = error.source();
while let Some(s) = source {
text.push(' ');
text.push_str(&s.to_string());
source = s.source();
}
if text.to_ascii_lowercase().contains("certificate") {
if let Some(problem) =
crate::trust::origin::host_of(url).and_then(|h| crate::trust::tls::problem_for(&h))
{
return Error::Trust(problem);
}
}
Error::Http(error.to_string())
}
#[cfg(feature = "network")]
pub struct ReqwestTransport {
client: reqwest::Client,
download_hosts: std::sync::Arc<Vec<String>>,
}
#[cfg(feature = "network")]
impl ReqwestTransport {
pub fn new() -> Result<Self> {
Self::with_timeout(std::time::Duration::from_secs(30))
}
pub fn with_timeout(timeout: std::time::Duration) -> Result<Self> {
let client = reqwest::Client::builder()
.user_agent(concat!("gitpoulpi/", env!("CARGO_PKG_VERSION")))
.use_preconfigured_tls(crate::trust::tls::client_config().map_err(Error::Http)?)
.redirect(same_origin_redirects())
.timeout(timeout)
.pool_max_idle_per_host(0)
.build()
.map_err(|e| Error::Http(e.to_string()))?;
Ok(Self {
client,
download_hosts: std::sync::Arc::default(),
})
}
#[must_use]
pub fn with_download_hosts(mut self, hosts: Vec<String>) -> Self {
self.download_hosts = std::sync::Arc::new(hosts);
self
}
}
#[cfg(feature = "network")]
#[async_trait::async_trait]
impl HttpTransport for ReqwestTransport {
async fn send(&self, request: HttpRequest) -> Result<HttpResponse> {
let method = match request.method {
Method::Get => reqwest::Method::GET,
Method::Post => reqwest::Method::POST,
Method::Put => reqwest::Method::PUT,
Method::Patch => reqwest::Method::PATCH,
Method::Delete => reqwest::Method::DELETE,
};
if let Some(why) = external_host_refusal(&request.url) {
return Err(Error::Http(why));
}
let request_url = request.url.clone();
let mut builder = self.client.request(method, &request.url);
for (name, value) in &request.headers {
builder = builder.header(name, value);
}
if let Some(body) = request.body {
builder = builder.body(body);
}
let response = builder
.send()
.await
.map_err(|e| send_error(&e, &request_url))?;
let status = response.status().as_u16();
let headers = response
.headers()
.iter()
.filter_map(|(k, v)| Some((k.as_str().to_string(), v.to_str().ok()?.to_string())))
.collect();
let body = response
.text()
.await
.map_err(|e| Error::Http(e.to_string()))?;
Ok(HttpResponse {
status,
headers,
body,
})
}
async fn fetch_bytes(&self, request: HttpRequest) -> Result<HttpBytes> {
if let Some(why) = external_host_refusal(&request.url) {
return Err(Error::Http(why));
}
let client = reqwest::Client::builder()
.user_agent(concat!("gitpoulpi/", env!("CARGO_PKG_VERSION")))
.use_preconfigured_tls(crate::trust::tls::client_config().map_err(Error::Http)?)
.redirect(reqwest::redirect::Policy::limited(5))
.timeout(std::time::Duration::from_secs(5))
.pool_max_idle_per_host(0)
.build()
.map_err(|e| Error::Http(e.to_string()))?;
let url = request.url.clone();
let mut builder = client.get(&request.url);
for (name, value) in &request.headers {
builder = builder.header(name, value);
}
let response = builder.send().await.map_err(|e| send_error(&e, &url))?;
let status = response.status().as_u16();
let headers = response
.headers()
.iter()
.filter_map(|(k, v)| Some((k.as_str().to_string(), v.to_str().ok()?.to_string())))
.collect();
let body = response
.bytes()
.await
.map_err(|e| Error::Http(e.to_string()))?
.to_vec();
Ok(HttpBytes {
status,
headers,
body,
})
}
async fn download(&self, request: HttpRequest, progress: &Progress<'_>) -> Result<Vec<u8>> {
if !crate::updates::download_host_allowed_with(&request.url, &self.download_hosts) {
return Err(Error::Http(format!(
"refusing to download from {}: not a GitHub release host",
request.url
)));
}
if let Some(why) = external_host_refusal(&request.url) {
return Err(Error::Http(why));
}
let hosts = self.download_hosts.clone();
let policy = reqwest::redirect::Policy::custom(move |attempt| {
if attempt.previous().len() >= 5 {
attempt.error("too many redirects")
} else if crate::updates::download_host_allowed_with(attempt.url().as_str(), &hosts) {
attempt.follow()
} else {
let host = attempt.url().host_str().unwrap_or("?").to_string();
attempt.error(format!(
"redirect to {host} refused: not a GitHub release host"
))
}
});
let client = reqwest::Client::builder()
.user_agent(concat!("gitpoulpi/", env!("CARGO_PKG_VERSION")))
.use_preconfigured_tls(crate::trust::tls::client_config().map_err(Error::Http)?)
.redirect(policy)
.read_timeout(std::time::Duration::from_secs(30))
.connect_timeout(std::time::Duration::from_secs(15))
.pool_max_idle_per_host(0)
.build()
.map_err(|e| Error::Http(e.to_string()))?;
let mut builder = client.get(&request.url);
for (name, value) in &request.headers {
builder = builder.header(name, value);
}
let url = request.url.clone();
let mut response = builder.send().await.map_err(|e| send_error(&e, &url))?;
let status = response.status();
if !status.is_success() {
return Err(Error::Http(format!(
"the download answered HTTP {}",
status.as_u16()
)));
}
let total = response.content_length();
let mut out = Vec::with_capacity(total.unwrap_or(0).min(256 << 20) as usize);
while let Some(chunk) = response
.chunk()
.await
.map_err(|e| Error::Http(e.to_string()))?
{
out.extend_from_slice(&chunk);
if !progress(out.len() as u64, total) {
return Err(Error::Http("download canceled".into()));
}
}
Ok(out)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_hosts_own_error_message_survives_into_the_error() {
let response = HttpResponse {
status: 401,
headers: BTreeMap::new(),
body: r#"{"message":"Bad credentials"}"#.into(),
};
let err = response.error_for_status("listing projects").unwrap_err();
let text = err.to_string();
assert!(text.contains("Bad credentials"), "{text}");
assert!(text.contains("listing projects"), "{text}");
assert!(matches!(err, Error::Unauthorized { .. }));
}
#[test]
fn a_non_json_error_body_is_truncated_rather_than_dumped() {
let response = HttpResponse {
status: 500,
headers: BTreeMap::new(),
body: "x".repeat(5000),
};
let text = response
.error_for_status("listing projects")
.unwrap_err()
.to_string();
assert!(text.len() < 400, "error was {} chars", text.len());
assert!(text.ends_with("..."));
}
#[test]
fn a_success_is_not_an_error() {
let response = HttpResponse {
status: 204,
headers: BTreeMap::new(),
body: String::new(),
};
assert!(response.error_for_status("anything").is_ok());
}
#[cfg(feature = "network")]
#[test]
fn a_request_never_picks_up_a_connection_frozen_on_another_runtime() {
use std::io::{Read, Write};
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/x", listener.local_addr().unwrap());
std::thread::spawn(move || {
for stream in listener.incoming().flatten() {
std::thread::spawn(move || {
let mut stream = stream;
let mut seen = Vec::new();
let mut buf = [0u8; 1024];
while let Ok(n) = stream.read(&mut buf) {
if n == 0 {
return;
}
seen.extend_from_slice(&buf[..n]);
while let Some(end) = seen.windows(4).position(|w| w == b"\r\n\r\n") {
seen.drain(..end + 4);
let _ =
stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok");
}
}
});
}
});
let transport = ReqwestTransport::with_timeout(std::time::Duration::from_secs(3)).unwrap();
let mut parked = Vec::new();
for _ in 0..3 {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let request = HttpRequest {
method: Method::Get,
url: url.clone(),
headers: Vec::new(),
body: None,
};
let response = rt.block_on(transport.send(request)).unwrap();
assert_eq!(response.body, "ok");
parked.push(rt);
}
}
#[test]
fn a_blocked_build_refuses_external_hosts_but_not_loopback() {
for url in [
"http://127.0.0.1:3000/api",
"http://localhost:8081/",
"http://[::1]:80/x",
"http://user:pw@127.0.0.1/",
] {
assert_eq!(refusal(url, true, false), None, "{url}");
}
for url in [
"https://api.github.com/user",
"https://gitlab.com/api/v4",
"http://user@example.org:80/x",
] {
assert!(refusal(url, true, false).is_some(), "{url}");
assert_eq!(refusal(url, true, true), None, "live switch lifts it");
assert_eq!(refusal(url, false, false), None, "production never refuses");
}
}
}