repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by bugabinga
smith-harness/src/http.rs
RawFile outline
- MAX_BODY_BYTES line 15
- MAX_REQUEST_BYTES line 18
- HEAD_TIMEOUT line 21
- CHUNK_TIMEOUT line 24
- ReqwestHttpExecutor line 27
- ReqwestHttpExecutor line 32
- new line 34
- with_client line 45
- Default for ReqwestHttpExecutor line 51
- default line 52
- HttpExecutor for ReqwestHttpExecutor line 57
- execute line 58
- default_executor line 117
- transport_fault line 122
- bounded line 130
//! Host-owned HTTP engine over the `smith::http` vocabulary.
//!
//! One executor serves provider traffic today and plugin net-scope
//! effects later; every call is bounded, cancellable, and recordable.
use smith::error::{ProviderFault, Result, SmithError};
use smith::http::{HttpChunk, HttpExchange, HttpExecutor, HttpExecutorRef, HttpHead, HttpResponse};
use smith::tool::CancelHandle;
use std::io::Read;
use std::sync::Arc;
use std::sync::mpsc;
use std::time::Duration;
/// Largest accepted response body, in bytes.
pub const MAX_BODY_BYTES: u64 = 64 * 1024 * 1024;
/// Largest accepted single request body, in bytes.
pub const MAX_REQUEST_BYTES: usize = 16 * 1024 * 1024;
/// Timeout for the response head.
pub const HEAD_TIMEOUT: Duration = Duration::from_secs(120);
/// Deadline for one chunk gap.
pub const CHUNK_TIMEOUT: Duration = Duration::from_secs(300);
/// `reqwest`-backed executor; the only HTTP engine in the workspace.
#[derive(Clone)]
pub struct ReqwestHttpExecutor {
client: reqwest::blocking::Client,
}
impl ReqwestHttpExecutor {
/// A default executor with head and chunk timeouts.
#[must_use]
pub fn new() -> Self {
Self {
client: reqwest::blocking::Client::builder()
.timeout(HEAD_TIMEOUT + CHUNK_TIMEOUT)
.build()
.unwrap_or_default(),
}
}
/// Wrap an existing client, primarily for tests.
#[must_use]
pub const fn with_client(client: reqwest::blocking::Client) -> Self {
Self { client }
}
}
impl Default for ReqwestHttpExecutor {
fn default() -> Self {
Self::new()
}
}
impl HttpExecutor for ReqwestHttpExecutor {
fn execute(&self, exchange: HttpExchange, cancel: CancelHandle) -> Result<HttpResponse> {
if exchange.body.len() > MAX_REQUEST_BYTES {
return Err(bounded("request body exceeds the size bound"));
}
if cancel.is_cancelled() {
return Err(SmithError::Cancelled);
}
let method = match exchange.method {
smith::http::HttpMethod::Post => reqwest::Method::POST,
};
let mut prepared = self
.client
.request(method, &exchange.url)
.body(exchange.body.clone())
.timeout(HEAD_TIMEOUT);
for header in &exchange.headers {
prepared = prepared.header(&header.name, &header.value);
}
let mut response = prepared
.send()
.map_err(|e| transport_fault(&e.to_string()))?;
let head = HttpHead {
status: response.status().as_u16(),
};
let (sender, receiver) = mpsc::channel();
let cancel_stream = cancel;
std::thread::spawn(move || {
let mut buffer = [0u8; 16 * 1024];
let mut total: u64 = 0;
loop {
if cancel_stream.is_cancelled() {
return;
}
let read = match response.read(&mut buffer) {
Ok(0) | Err(_) => return,
Ok(read) => read,
};
total += u64::try_from(read).unwrap_or(u64::MAX);
if total > MAX_BODY_BYTES {
return;
}
if sender
.send(HttpChunk {
bytes: buffer[..read].to_vec(),
})
.is_err()
{
return;
}
}
});
Ok(HttpResponse {
head,
chunks: receiver,
})
}
}
/// The shared default executor.
#[must_use]
pub fn default_executor() -> HttpExecutorRef {
Arc::new(ReqwestHttpExecutor::new())
}
fn transport_fault(message: &str) -> SmithError {
SmithError::Provider {
fault: ProviderFault::Transient {
message: message.to_string(),
},
}
}
fn bounded(message: &str) -> SmithError {
SmithError::Provider {
fault: ProviderFault::Transient {
message: format!("http effect: {message}"),
},
}
}