Initial impl

This commit is contained in:
Torbjörn Molin committed 2026-10-19 09:31:57 +02:00
commit 4b729c5d32
133 files changed
+20475

No files matched your search

+336
View File
@@ -0,0 +1,336 @@
//! libcurl implementation of [`reqlite_core::http::HttpClient`].
//!
//! v1 design (implementation plan §2.3): each request runs on Tokio's blocking
//! pool with its own curl `Easy2` handle. The async side races the blocking
//! task against the cancel token; curl's progress callback checks the same
//! token and aborts the transfer.
use std::fs::File;
use std::io::{Read, Write};
use std::sync::Arc;
use std::time::Duration;
use curl::easy::{Easy2, Form, Handler, HttpVersion, List, ReadError, WriteError};
use reqlite_core::http::{
canonical_reason, BodyStore, HttpClient, HttpError, Proxy, ResolvedBody, ResolvedPartBody, ResolvedRequest,
Response, Timings,
};
use reqlite_core::CancelToken;
/// The libcurl-backed client. Cheap to clone; holds no connections (each
/// request uses a fresh handle in v1).
#[derive(Debug, Clone, Default)]
pub struct CurlClient {
_priv: (),
}
impl CurlClient {
pub fn new() -> Self {
curl::init();
Self { _priv: () }
}
/// libcurl's version string, e.g. for an "about" box.
pub fn version() -> String {
let v = curl::Version::get();
format!(
"libcurl {} ({}{})",
v.version(),
v.ssl_version().unwrap_or("no TLS"),
if v.feature_http2() { ", HTTP/2" } else { "" }
)
}
}
impl HttpClient for CurlClient {
async fn send(&self, req: ResolvedRequest, cancel: CancelToken) -> Result<Response, HttpError> {
let token = cancel.clone();
let task = tokio::task::spawn_blocking(move || perform(req, token));
tokio::select! {
res = task => match res {
Ok(r) => r,
Err(e) => Err(HttpError::Other(format!("transfer task failed: {e}"))),
},
_ = cancel.cancelled() => Err(HttpError::Cancelled),
}
}
}
struct Collector {
cancel: CancelToken,
status_line: String,
headers: Vec<(String, String)>,
mem: Vec<u8>,
file: Option<tempfile::NamedTempFile>,
threshold: u64,
size: u64,
upload: Option<Box<dyn Read + Send>>,
io_error: Option<std::io::Error>,
}
impl Handler for Collector {
fn write(&mut self, data: &[u8]) -> Result<usize, WriteError> {
if self.file.is_none() && (self.mem.len() + data.len()) as u64 > self.threshold {
let spill = tempfile::Builder::new()
.prefix("reqlite-response-")
.tempfile()
.and_then(|mut f| f.write_all(&self.mem).map(|_| f));
match spill {
Ok(f) => {
self.file = Some(f);
self.mem = Vec::new();
}
Err(e) => {
self.io_error = Some(e);
return Ok(0);
}
}
}
if let Some(f) = &mut self.file {
if let Err(e) = f.write_all(data) {
self.io_error = Some(e);
return Ok(0);
}
} else {
self.mem.extend_from_slice(data);
}
self.size += data.len() as u64;
Ok(data.len())
}
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ReadError> {
match &mut self.upload {
Some(r) => r.read(buf).map_err(|_| ReadError::Abort),
None => Ok(0),
}
}
fn header(&mut self, data: &[u8]) -> bool {
let line = String::from_utf8_lossy(data);
let line = line.trim_end_matches(['\r', '\n']);
if line.starts_with("HTTP/") {
// A new response (redirect hop or after `100 Continue`).
self.status_line = line.to_string();
self.headers.clear();
} else if let Some((n, v)) = line.split_once(':') {
self.headers.push((n.trim().to_string(), v.trim().to_string()));
}
true
}
fn progress(&mut self, _dltotal: f64, _dlnow: f64, _ultotal: f64, _ulnow: f64) -> bool {
!self.cancel.is_cancelled()
}
}
fn perform(req: ResolvedRequest, cancel: CancelToken) -> Result<Response, HttpError> {
let o = &req.options;
let mut easy = Easy2::new(Collector {
cancel: cancel.clone(),
status_line: String::new(),
headers: Vec::new(),
mem: Vec::new(),
file: None,
threshold: o.spill_threshold,
size: 0,
upload: None,
io_error: None,
});
match setup(&mut easy, &req) {
Ok(()) => {}
Err(SetupError::Curl(e)) => return Err(map_error(&e, &cancel, None)),
Err(SetupError::Io(e)) => return Err(HttpError::Io(e)),
}
tracing::debug!(method = %req.method, url = %req.url, "sending");
if let Err(e) = easy.perform() {
let io = easy.get_mut().io_error.take();
return Err(map_error(&e, &cancel, io));
}
let timings = Timings {
dns: easy.namelookup_time().unwrap_or_default(),
connect: easy.connect_time().unwrap_or_default(),
tls: easy.appconnect_time().unwrap_or_default(),
ttfb: easy.starttransfer_time().unwrap_or_default(),
total: easy.total_time().unwrap_or_default(),
};
let status = easy.response_code().unwrap_or(0) as u16;
let effective_url = easy.effective_url().ok().flatten().unwrap_or(&req.url).to_string();
let c = easy.get_mut();
let mut parts = c.status_line.splitn(3, ' ');
let http_version = parts.next().unwrap_or("").to_string();
let _code = parts.next();
let reason = match parts.next().map(str::trim) {
Some(r) if !r.is_empty() => r.to_string(),
_ => canonical_reason(status).to_string(),
};
let body = match c.file.take() {
Some(mut f) => {
f.flush().map_err(|e| HttpError::Io(e.to_string()))?;
BodyStore::TempFile(Arc::new(f.into_temp_path()))
}
None => BodyStore::InMemory(Arc::new(std::mem::take(&mut c.mem))),
};
Ok(Response {
status,
reason,
http_version,
headers: std::mem::take(&mut c.headers),
body,
timings,
size: c.size,
effective_url,
})
}
enum SetupError {
Curl(curl::Error),
Io(String),
}
impl From<curl::Error> for SetupError {
fn from(e: curl::Error) -> Self {
SetupError::Curl(e)
}
}
fn setup(easy: &mut Easy2<Collector>, req: &ResolvedRequest) -> Result<(), SetupError> {
let o = &req.options;
easy.url(&req.url)?;
easy.progress(true)?;
easy.signal(false)?;
let method = req.method.as_str();
let has_body = !matches!(req.body, ResolvedBody::None);
match &req.body {
ResolvedBody::None => {}
ResolvedBody::Bytes(b) => {
easy.post(true)?;
easy.post_fields_copy(b)?;
}
ResolvedBody::File(path) => {
let f = File::open(path).map_err(|e| SetupError::Io(format!("{}: {e}", path.display())))?;
let len = f.metadata().map(|m| m.len()).unwrap_or(0);
easy.get_mut().upload = Some(Box::new(f));
easy.post(true)?;
easy.post_field_size(len)?;
}
ResolvedBody::Multipart(parts) => {
let mut form = Form::new();
for p in parts {
let mut part = form.part(&p.name);
match &p.body {
ResolvedPartBody::Bytes(b) => {
part.contents(b);
}
ResolvedPartBody::File(f) => {
part.file(f);
}
}
if let Some(fname) = &p.filename {
part.filename(fname);
}
if let Some(ct) = &p.content_type {
part.content_type(ct);
}
part.add()
.map_err(|e| SetupError::Io(format!("multipart part `{}`: {e}", p.name)))?;
}
easy.httppost(form)?;
}
}
match (method, has_body) {
("GET", false) => easy.get(true)?,
("HEAD", false) => easy.nobody(true)?,
("POST", true) => {}
(m, _) => easy.custom_request(m)?,
}
let mut list = List::new();
let mut has_expect = false;
let mut has_accept_encoding = false;
let multipart = matches!(req.body, ResolvedBody::Multipart(_));
for (n, v) in &req.headers {
// curl generates the multipart Content-Type (with its own boundary).
if multipart && n.eq_ignore_ascii_case("content-type") {
continue;
}
has_expect |= n.eq_ignore_ascii_case("expect");
has_accept_encoding |= n.eq_ignore_ascii_case("accept-encoding");
if v.is_empty() {
list.append(&format!("{n};"))?;
} else {
list.append(&format!("{n}: {v}"))?;
}
}
if !has_expect {
list.append("Expect:")?;
}
easy.http_headers(list)?;
if !has_accept_encoding {
// Advertise everything curl can decode and decode it transparently.
easy.accept_encoding("")?;
}
easy.follow_location(o.follow_redirects)?;
easy.max_redirections(o.max_redirects)?;
if let Some(t) = o.timeout {
easy.timeout(t)?;
}
easy.connect_timeout(o.connect_timeout.unwrap_or(Duration::from_secs(30)))?;
easy.ssl_verify_peer(o.verify_tls)?;
easy.ssl_verify_host(o.verify_tls)?;
if let Some(ca) = &o.ca_file {
easy.cainfo(ca)?;
}
match &o.proxy {
Proxy::System => {}
Proxy::None => {
easy.proxy("")?;
easy.noproxy("*")?;
}
Proxy::Manual(p) => easy.proxy(p)?,
}
match o.http_version.as_deref() {
Some("HTTP/1.0") => easy.http_version(HttpVersion::V10)?,
Some("HTTP/1.1") => easy.http_version(HttpVersion::V11)?,
Some("HTTP/2" | "HTTP/2.0") => easy.http_version(HttpVersion::V2)?,
_ => easy.http_version(HttpVersion::V2TLS)?,
}
Ok(())
}
fn map_error(e: &curl::Error, cancel: &CancelToken, io: Option<std::io::Error>) -> HttpError {
let detail = || match e.extra_description() {
Some(x) => format!("{} ({x})", e.description()),
None => e.description().to_string(),
};
if cancel.is_cancelled() || e.is_aborted_by_callback() {
HttpError::Cancelled
} else if let Some(io) = io {
HttpError::Io(io.to_string())
} else if e.is_operation_timedout() {
HttpError::Timeout
} else if e.is_couldnt_resolve_host() || e.is_couldnt_resolve_proxy() {
HttpError::Dns(detail())
} else if e.is_couldnt_connect() {
HttpError::Connect(detail())
} else if e.is_ssl_connect_error()
|| e.is_peer_failed_verification()
|| e.is_ssl_certproblem()
|| e.is_ssl_cacert_badfile()
|| e.is_ssl_cipher()
|| e.is_ssl_issuer_error()
{
HttpError::Tls(detail())
} else if e.is_url_malformed() || e.is_unsupported_protocol() {
HttpError::InvalidUrl(detail())
} else if e.is_read_error() || e.is_write_error() {
HttpError::Io(detail())
} else {
HttpError::Other(detail())
}
}