use std::error::Error as StdError;
use std::fmt;
use std::num::NonZeroUsize;
use std::sync::Arc;
use std::time::Duration;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum RunMode {
Spawned,
Manual,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum HttpBackend {
Native,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum TlsTrust {
Platform,
SuppliedRootsOnly,
BundledMozilla,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CallbackDispatch {
Inline,
Workers(NonZeroUsize),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EngineConfig {
additional_tls_roots: Vec<Arc<[u8]>>,
tls_trust: TlsTrust,
run_mode: RunMode,
callback_dispatch: CallbackDispatch,
max_inflight_requests: NonZeroUsize,
command_queue_capacity: NonZeroUsize,
callback_queue_capacity: NonZeroUsize,
max_request_body_bytes: usize,
max_response_body_bytes: usize,
max_buffered_body_bytes: Option<usize>,
max_stream_queue_bytes_per_request: usize,
max_stream_queued_bytes: usize,
max_queued_bytes: usize,
max_header_bytes: usize,
max_header_count: usize,
max_connections: NonZeroUsize,
max_connections_per_origin: NonZeroUsize,
max_idle_connections: usize,
max_idle_connections_per_origin: usize,
idle_connection_timeout: Duration,
max_inflight_resolutions: NonZeroUsize,
max_standalone_tcp_connections: NonZeroUsize,
max_resolve_results: NonZeroUsize,
max_tcp_queue_bytes_per_connection: usize,
}
const DEFAULT_BODY_LIMIT: usize = 16 * 1024 * 1024;
const DEFAULT_STREAM_QUEUE_LIMIT: usize = 256 * 1024;
const DEFAULT_STREAM_QUEUED_LIMIT: usize = 16 * 1024 * 1024;
const DEFAULT_HEADER_BYTES_LIMIT: usize = 64 * 1024;
const DEFAULT_HEADER_COUNT_LIMIT: usize = 256;
const DEFAULT_MAX_CONNECTIONS: usize = 32;
const DEFAULT_MAX_CONNECTIONS_PER_ORIGIN: usize = 8;
const DEFAULT_MAX_IDLE_CONNECTIONS: usize = 32;
const DEFAULT_MAX_IDLE_CONNECTIONS_PER_ORIGIN: usize = 4;
const DEFAULT_IDLE_CONNECTION_TIMEOUT: Duration = Duration::from_secs(30);
const DEFAULT_MAX_INFLIGHT_RESOLUTIONS: usize = 256;
pub(crate) const HTTP_DNS_TXID_RESERVE: usize = 1_024;
pub(crate) const DNS_TRANSACTION_ID_SPACE: usize = u16::MAX as usize + 1;
pub(crate) const MAX_PUBLIC_DNS_TRANSACTIONS: usize =
DNS_TRANSACTION_ID_SPACE - HTTP_DNS_TXID_RESERVE;
const DEFAULT_MAX_STANDALONE_TCP_CONNECTIONS: usize = 32;
const DEFAULT_MAX_RESOLVE_RESULTS: usize = 32;
const DEFAULT_TCP_QUEUE_LIMIT: usize = 256 * 1024;
impl EngineConfig {
#[must_use]
pub fn with_max_buffered_body_bytes(mut self, bytes: usize) -> Self {
self.max_buffered_body_bytes = Some(bytes);
self
}
#[must_use]
pub fn max_buffered_body_bytes(&self) -> Option<usize> {
self.max_buffered_body_bytes
}
#[must_use]
pub fn spawned() -> Self {
Self {
additional_tls_roots: Vec::new(),
tls_trust: TlsTrust::Platform,
run_mode: RunMode::Spawned,
callback_dispatch: CallbackDispatch::Workers(nonzero(1)),
max_inflight_requests: nonzero(1_024),
command_queue_capacity: nonzero(1_024),
callback_queue_capacity: nonzero(1_024),
max_request_body_bytes: DEFAULT_BODY_LIMIT,
max_response_body_bytes: DEFAULT_BODY_LIMIT,
max_buffered_body_bytes: None,
max_stream_queue_bytes_per_request: DEFAULT_STREAM_QUEUE_LIMIT,
max_stream_queued_bytes: DEFAULT_STREAM_QUEUED_LIMIT,
max_queued_bytes: DEFAULT_STREAM_QUEUED_LIMIT,
max_header_bytes: DEFAULT_HEADER_BYTES_LIMIT,
max_header_count: DEFAULT_HEADER_COUNT_LIMIT,
max_connections: nonzero(DEFAULT_MAX_CONNECTIONS),
max_connections_per_origin: nonzero(DEFAULT_MAX_CONNECTIONS_PER_ORIGIN),
max_idle_connections: DEFAULT_MAX_IDLE_CONNECTIONS,
max_idle_connections_per_origin: DEFAULT_MAX_IDLE_CONNECTIONS_PER_ORIGIN,
idle_connection_timeout: DEFAULT_IDLE_CONNECTION_TIMEOUT,
max_inflight_resolutions: nonzero(DEFAULT_MAX_INFLIGHT_RESOLUTIONS),
max_standalone_tcp_connections: nonzero(DEFAULT_MAX_STANDALONE_TCP_CONNECTIONS),
max_resolve_results: nonzero(DEFAULT_MAX_RESOLVE_RESULTS),
max_tcp_queue_bytes_per_connection: DEFAULT_TCP_QUEUE_LIMIT,
}
}
#[must_use]
pub fn manual() -> Self {
Self {
additional_tls_roots: Vec::new(),
tls_trust: TlsTrust::Platform,
run_mode: RunMode::Manual,
callback_dispatch: CallbackDispatch::Inline,
max_inflight_requests: nonzero(1_024),
command_queue_capacity: nonzero(1_024),
callback_queue_capacity: nonzero(1_024),
max_request_body_bytes: DEFAULT_BODY_LIMIT,
max_response_body_bytes: DEFAULT_BODY_LIMIT,
max_buffered_body_bytes: None,
max_stream_queue_bytes_per_request: DEFAULT_STREAM_QUEUE_LIMIT,
max_stream_queued_bytes: DEFAULT_STREAM_QUEUED_LIMIT,
max_queued_bytes: DEFAULT_STREAM_QUEUED_LIMIT,
max_header_bytes: DEFAULT_HEADER_BYTES_LIMIT,
max_header_count: DEFAULT_HEADER_COUNT_LIMIT,
max_connections: nonzero(DEFAULT_MAX_CONNECTIONS),
max_connections_per_origin: nonzero(DEFAULT_MAX_CONNECTIONS_PER_ORIGIN),
max_idle_connections: DEFAULT_MAX_IDLE_CONNECTIONS,
max_idle_connections_per_origin: DEFAULT_MAX_IDLE_CONNECTIONS_PER_ORIGIN,
idle_connection_timeout: DEFAULT_IDLE_CONNECTION_TIMEOUT,
max_inflight_resolutions: nonzero(DEFAULT_MAX_INFLIGHT_RESOLUTIONS),
max_standalone_tcp_connections: nonzero(DEFAULT_MAX_STANDALONE_TCP_CONNECTIONS),
max_resolve_results: nonzero(DEFAULT_MAX_RESOLVE_RESULTS),
max_tcp_queue_bytes_per_connection: DEFAULT_TCP_QUEUE_LIMIT,
}
}
#[must_use]
pub fn with_tls_trust(mut self, trust: TlsTrust) -> Self {
self.tls_trust = trust;
self
}
#[must_use]
pub fn tls_trust(&self) -> TlsTrust {
self.tls_trust
}
#[must_use]
pub fn with_additional_tls_root_certificate(mut self, der: impl Into<Vec<u8>>) -> Self {
self.additional_tls_roots.push(Arc::from(der.into()));
self
}
#[must_use]
pub fn additional_tls_root_certificates(&self) -> &[Arc<[u8]>] {
&self.additional_tls_roots
}
#[must_use]
pub fn with_callback_workers(mut self, workers: NonZeroUsize) -> Self {
self.callback_dispatch = CallbackDispatch::Workers(workers);
self
}
#[must_use]
pub fn with_max_inflight_requests(mut self, requests: NonZeroUsize) -> Self {
self.max_inflight_requests = requests;
self
}
#[must_use]
pub fn with_command_queue_capacity(mut self, capacity: NonZeroUsize) -> Self {
self.command_queue_capacity = capacity;
self
}
#[must_use]
pub fn with_callback_queue_capacity(mut self, capacity: NonZeroUsize) -> Self {
self.callback_queue_capacity = capacity;
self
}
#[must_use]
pub fn with_max_request_body_bytes(mut self, bytes: usize) -> Self {
self.max_request_body_bytes = bytes;
self
}
#[must_use]
pub fn with_max_response_body_bytes(mut self, bytes: usize) -> Self {
self.max_response_body_bytes = bytes;
self
}
#[must_use]
pub fn with_max_stream_queue_bytes_per_request(mut self, bytes: usize) -> Self {
self.max_stream_queue_bytes_per_request = bytes;
self
}
#[must_use]
pub fn with_max_stream_queued_bytes(mut self, bytes: usize) -> Self {
self.max_stream_queued_bytes = bytes;
self
}
#[must_use]
pub fn with_max_queued_bytes(mut self, bytes: usize) -> Self {
self.max_queued_bytes = bytes;
self
}
#[must_use]
pub fn with_max_header_bytes(mut self, bytes: usize) -> Self {
self.max_header_bytes = bytes;
self
}
#[must_use]
pub fn with_max_header_count(mut self, count: usize) -> Self {
self.max_header_count = count;
self
}
#[must_use]
pub fn with_max_connections(mut self, connections: NonZeroUsize) -> Self {
self.max_connections = connections;
self
}
#[must_use]
pub fn with_max_connections_per_origin(mut self, connections: NonZeroUsize) -> Self {
self.max_connections_per_origin = connections;
self
}
#[must_use]
pub fn with_max_idle_connections(mut self, connections: usize) -> Self {
self.max_idle_connections = connections;
self
}
#[must_use]
pub fn with_max_idle_connections_per_origin(mut self, connections: usize) -> Self {
self.max_idle_connections_per_origin = connections;
self
}
#[must_use]
pub fn with_idle_connection_timeout(mut self, timeout: Duration) -> Self {
self.idle_connection_timeout = timeout;
self
}
#[must_use]
pub fn with_max_inflight_resolutions(mut self, resolutions: NonZeroUsize) -> Self {
let capped = resolutions.get().min(MAX_PUBLIC_DNS_TRANSACTIONS);
self.max_inflight_resolutions = NonZeroUsize::new(capped)
.expect("HTTP DNS reservation leaves a public transaction cap");
self
}
#[must_use]
pub fn with_max_standalone_tcp_connections(mut self, connections: NonZeroUsize) -> Self {
self.max_standalone_tcp_connections = connections;
self
}
#[must_use]
pub fn with_max_resolve_results(mut self, results: NonZeroUsize) -> Self {
self.max_resolve_results = results;
self
}
#[must_use]
pub fn with_max_tcp_queue_bytes_per_connection(mut self, bytes: usize) -> Self {
self.max_tcp_queue_bytes_per_connection = bytes;
self
}
#[must_use]
pub fn run_mode(&self) -> RunMode {
self.run_mode
}
#[must_use]
pub fn callback_dispatch(&self) -> CallbackDispatch {
self.callback_dispatch
}
#[must_use]
pub fn max_inflight_requests(&self) -> NonZeroUsize {
self.max_inflight_requests
}
#[must_use]
pub fn command_queue_capacity(&self) -> NonZeroUsize {
self.command_queue_capacity
}
#[must_use]
pub fn callback_queue_capacity(&self) -> NonZeroUsize {
self.callback_queue_capacity
}
#[must_use]
pub fn max_request_body_bytes(&self) -> usize {
self.max_request_body_bytes
}
#[must_use]
pub fn max_response_body_bytes(&self) -> usize {
self.max_response_body_bytes
}
#[must_use]
pub fn max_stream_queue_bytes_per_request(&self) -> usize {
self.max_stream_queue_bytes_per_request
}
#[must_use]
pub fn max_stream_queued_bytes(&self) -> usize {
self.max_stream_queued_bytes
}
#[must_use]
pub fn max_queued_bytes(&self) -> usize {
self.max_queued_bytes
}
#[must_use]
pub fn max_header_bytes(&self) -> usize {
self.max_header_bytes
}
#[must_use]
pub fn max_header_count(&self) -> usize {
self.max_header_count
}
#[must_use]
pub fn max_connections(&self) -> NonZeroUsize {
self.max_connections
}
#[must_use]
pub fn max_connections_per_origin(&self) -> NonZeroUsize {
self.max_connections_per_origin
}
#[must_use]
pub fn max_idle_connections(&self) -> usize {
self.max_idle_connections
}
#[must_use]
pub fn max_idle_connections_per_origin(&self) -> usize {
self.max_idle_connections_per_origin
}
#[must_use]
pub fn idle_connection_timeout(&self) -> Duration {
self.idle_connection_timeout
}
#[must_use]
pub fn max_inflight_resolutions(&self) -> NonZeroUsize {
self.max_inflight_resolutions
}
#[must_use]
pub fn max_standalone_tcp_connections(&self) -> NonZeroUsize {
self.max_standalone_tcp_connections
}
#[must_use]
pub fn max_resolve_results(&self) -> NonZeroUsize {
self.max_resolve_results
}
#[must_use]
pub fn max_tcp_queue_bytes_per_connection(&self) -> usize {
self.max_tcp_queue_bytes_per_connection
}
}
impl Default for EngineConfig {
fn default() -> Self {
Self::spawned()
}
}
const fn nonzero(value: usize) -> NonZeroUsize {
match NonZeroUsize::new(value) {
Some(value) => value,
None => panic!("NBReq internal non-zero constant was zero"),
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum DriveStatus {
Idle,
Progress,
DeadlineReached,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum Method {
Get,
Head,
Post,
Put,
Patch,
Delete,
Other(String),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Header {
name: String,
value: Vec<u8>,
}
impl Header {
#[must_use]
pub fn new(name: impl Into<String>, value: impl Into<Vec<u8>>) -> Self {
Self {
name: name.into(),
value: value.into(),
}
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn value(&self) -> &[u8] {
&self.value
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum TlsVerification {
#[default]
Verify,
DangerouslyDisableCertificateVerification,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct RequestOptions {
pub max_request_body_bytes: Option<usize>,
pub max_response_body_bytes: Option<usize>,
pub connect_timeout: Option<Duration>,
pub inactivity_timeout: Option<Duration>,
pub total_timeout: Option<Duration>,
pub redirect_limit: u8,
pub tls_verification: TlsVerification,
}
impl Default for RequestOptions {
fn default() -> Self {
Self {
max_request_body_bytes: None,
max_response_body_bytes: None,
connect_timeout: Some(Duration::from_secs(10)),
inactivity_timeout: Some(Duration::from_secs(30)),
total_timeout: Some(Duration::from_secs(120)),
redirect_limit: 5,
tls_verification: TlsVerification::Verify,
}
}
}
impl RequestOptions {
pub(crate) fn request_body_limit(&self, engine_limit: usize) -> usize {
self.max_request_body_bytes
.unwrap_or(engine_limit)
.min(engine_limit)
}
pub(crate) fn response_body_limit(&self, engine_limit: usize) -> usize {
self.max_response_body_bytes
.unwrap_or(engine_limit)
.min(engine_limit)
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct Request {
method: Method,
url: String,
headers: Vec<Header>,
body: Arc<crate::body_budget::BodyBuffer>,
options: RequestOptions,
}
impl Request {
pub(crate) fn admit_body(
&mut self,
budget: Arc<crate::body_budget::BodyBudget>,
) -> Result<(), Error> {
let body = Arc::try_unwrap(std::mem::take(&mut self.body))
.map_err(|_| Error::new(ErrorKind::Internal, "unadmitted request storage is shared"))?;
self.body = Arc::new(crate::body_budget::BodyBuffer::admit(
body.into_vec(),
budget,
)?);
Ok(())
}
#[cfg(feature = "native")]
pub(crate) fn body_owner(&self) -> Arc<crate::body_budget::BodyBuffer> {
Arc::clone(&self.body)
}
#[cfg(feature = "native")]
pub(crate) fn body_budget(&self) -> Option<Arc<crate::body_budget::BodyBudget>> {
self.body.budget()
}
#[must_use]
pub fn builder(method: Method, url: impl Into<String>) -> RequestBuilder {
RequestBuilder {
request: Self {
method,
url: url.into(),
headers: Vec::new(),
body: Arc::default(),
options: RequestOptions::default(),
},
}
}
#[must_use]
pub fn get(url: impl Into<String>) -> RequestBuilder {
Self::builder(Method::Get, url)
}
#[must_use]
pub fn post(url: impl Into<String>) -> RequestBuilder {
Self::builder(Method::Post, url)
}
#[must_use]
pub fn head(url: impl Into<String>) -> RequestBuilder {
Self::builder(Method::Head, url)
}
#[must_use]
pub fn put(url: impl Into<String>) -> RequestBuilder {
Self::builder(Method::Put, url)
}
#[must_use]
pub fn patch(url: impl Into<String>) -> RequestBuilder {
Self::builder(Method::Patch, url)
}
#[must_use]
pub fn delete(url: impl Into<String>) -> RequestBuilder {
Self::builder(Method::Delete, url)
}
#[must_use]
pub fn method(&self) -> &Method {
&self.method
}
#[must_use]
pub fn url(&self) -> &str {
&self.url
}
#[must_use]
pub fn headers(&self) -> &[Header] {
&self.headers
}
#[must_use]
pub fn body(&self) -> &[u8] {
&self.body
}
#[must_use]
pub fn options(&self) -> &RequestOptions {
&self.options
}
pub(crate) fn validate(&self) -> Result<(), Error> {
http_origin(&self.url, ErrorKind::InvalidRequest)?;
if matches!(&self.method, Method::Other(method) if !is_http_token(method)) {
return Err(Error::new(
ErrorKind::InvalidRequest,
"extension HTTP method is not a valid token",
));
}
for header in &self.headers {
if !is_http_token(header.name()) || !is_valid_http_header_value(header.value()) {
return Err(Error::new(
ErrorKind::InvalidRequest,
"request contains an invalid HTTP header",
));
}
}
Ok(())
}
#[cfg(any(feature = "native", test))]
pub(crate) fn redirected(
self,
url: String,
method: Method,
keep_body: bool,
cross_origin: bool,
) -> Self {
let headers = self
.headers
.into_iter()
.filter(|header| {
let name = header.name();
let body_header = name.eq_ignore_ascii_case("content-length")
|| name.eq_ignore_ascii_case("transfer-encoding");
let origin_bound = name.eq_ignore_ascii_case("authorization")
|| name.eq_ignore_ascii_case("proxy-authorization")
|| name.eq_ignore_ascii_case("cookie")
|| name.eq_ignore_ascii_case("host");
(keep_body || !body_header) && (!cross_origin || !origin_bound)
})
.collect();
Self {
method,
url,
headers,
body: if keep_body {
self.body
} else {
Arc::new(crate::body_budget::BodyBuffer::new(self.body.budget()))
},
options: self.options,
}
}
}
impl Clone for Request {
fn clone(&self) -> Self {
Self {
method: self.method.clone(),
url: self.url.clone(),
headers: self.headers.clone(),
body: Arc::new(crate::body_budget::BodyBuffer::from_vec(
self.body().to_vec(),
)),
options: self.options.clone(),
}
}
}
#[cfg(any(feature = "native", test))]
#[derive(Debug)]
pub(crate) struct RequestRedirect {
url: String,
method: Method,
keep_body: bool,
cross_origin: bool,
}
#[cfg(any(feature = "native", test))]
impl RequestRedirect {
pub(crate) fn apply(self, request: Request) -> Request {
request.redirected(self.url, self.method, self.keep_body, self.cross_origin)
}
}
#[cfg(any(feature = "native", test))]
pub(crate) fn plan_redirect(
request: &Request,
status: u16,
redirect_hops: u8,
target: impl FnOnce() -> Result<Option<String>, Error>,
) -> Result<Option<RequestRedirect>, Error> {
let (method, keep_body) = match status {
301 | 302 => match request.method() {
Method::Get | Method::Head => (request.method().clone(), true),
_ => return Ok(None),
},
303 => match request.method() {
Method::Head => (Method::Head, false),
_ => (Method::Get, false),
},
307 | 308 => (request.method().clone(), true),
_ => return Ok(None),
};
let limit = request.options().redirect_limit;
if limit == 0 {
return Ok(None);
}
if redirect_hops >= limit {
return Err(Error::new(
ErrorKind::Redirect,
format!("redirect limit of {limit} hops was exceeded"),
));
}
let Some(target) = target()? else {
return Ok(None);
};
let source_origin = http_origin(request.url(), ErrorKind::Redirect)?;
let target_origin = http_origin(&target, ErrorKind::Redirect)?;
if source_origin.scheme == "https" && target_origin.scheme == "http" {
return Err(Error::new(
ErrorKind::Redirect,
"an HTTPS-to-HTTP redirect was blocked",
));
}
let cross_origin = source_origin != target_origin;
Ok(Some(RequestRedirect {
url: target,
method,
keep_body,
cross_origin,
}))
}
#[derive(Clone, Debug)]
pub struct RequestBuilder {
request: Request,
}
impl RequestBuilder {
#[must_use]
pub fn max_request_body_bytes(mut self, bytes: usize) -> Self {
self.request.options.max_request_body_bytes = Some(bytes);
self
}
#[must_use]
pub fn max_response_body_bytes(mut self, bytes: usize) -> Self {
self.request.options.max_response_body_bytes = Some(bytes);
self
}
#[must_use]
pub fn header(mut self, name: impl Into<String>, value: impl Into<Vec<u8>>) -> Self {
self.request.headers.push(Header::new(name, value));
self
}
#[must_use]
pub fn body(mut self, body: impl Into<Vec<u8>>) -> Self {
self.request.body = Arc::new(crate::body_budget::BodyBuffer::from_vec(body.into()));
self
}
#[must_use]
pub fn options(mut self, options: RequestOptions) -> Self {
self.request.options = options;
self
}
#[must_use]
pub fn connect_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
self.request.options.connect_timeout = timeout.into();
self
}
#[must_use]
pub fn inactivity_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
self.request.options.inactivity_timeout = timeout.into();
self
}
#[must_use]
pub fn total_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
self.request.options.total_timeout = timeout.into();
self
}
#[must_use]
pub fn redirect_limit(mut self, limit: u8) -> Self {
self.request.options.redirect_limit = limit;
self
}
#[must_use]
pub fn tls_verification(mut self, verification: TlsVerification) -> Self {
self.request.options.tls_verification = verification;
self
}
pub fn build(self) -> Result<Request, Error> {
self.request.validate()?;
Ok(self.request)
}
}
pub(crate) fn is_http_token(value: &str) -> bool {
!value.is_empty()
&& value.bytes().all(|byte| {
byte.is_ascii_alphanumeric()
|| matches!(
byte,
b'!' | b'#'
| b'$'
| b'%'
| b'&'
| b'\''
| b'*'
| b'+'
| b'-'
| b'.'
| b'^'
| b'_'
| b'`'
| b'|'
| b'~'
)
})
}
pub(crate) fn is_valid_http_header_value(value: &[u8]) -> bool {
!value
.iter()
.any(|byte| *byte == 0x7f || (*byte < 0x20 && *byte != b'\t'))
}
#[derive(Eq, PartialEq)]
pub(crate) struct HttpOrigin {
pub(crate) scheme: String,
pub(crate) host: String,
pub(crate) port: u16,
}
pub(crate) fn http_origin(url: &str, error_kind: ErrorKind) -> Result<HttpOrigin, Error> {
let Some((scheme, remainder)) = url.split_once("://") else {
return Err(Error::new(error_kind, "HTTP URL has no scheme separator"));
};
let scheme = scheme.to_ascii_lowercase();
let default_port = match scheme.as_str() {
"http" => 80,
"https" => 443,
_ => {
return Err(Error::new(
error_kind,
"NBReq permits only HTTP and HTTPS URLs",
));
}
};
let authority_end = remainder.find(['/', '?', '#']).unwrap_or(remainder.len());
let authority = &remainder[..authority_end];
if authority.is_empty() || authority.contains('@') {
return Err(Error::new(
error_kind,
"HTTP URL authority is empty or contains embedded credentials",
));
}
let (host, port) = if let Some(bracketed) = authority.strip_prefix('[') {
let Some(closing) = bracketed.find(']') else {
return Err(Error::new(error_kind, "HTTP URL has an invalid IPv6 host"));
};
let host = &bracketed[..closing];
let suffix = &bracketed[closing + 1..];
let port = if suffix.is_empty() {
default_port
} else if let Some(port) = suffix.strip_prefix(':') {
parse_port(port, error_kind)?
} else {
return Err(Error::new(error_kind, "HTTP URL has an invalid authority"));
};
(host, port)
} else if let Some((host, port)) = authority.rsplit_once(':') {
if host.contains(':') {
return Err(Error::new(
error_kind,
"an IPv6 URL host must be enclosed in brackets",
));
}
(host, parse_port(port, error_kind)?)
} else {
(authority, default_port)
};
if host.is_empty() {
return Err(Error::new(error_kind, "HTTP URL host is empty"));
}
if host
.bytes()
.any(|byte| byte <= 0x20 || byte == 0x7f || byte == b'\\')
{
return Err(Error::new(error_kind, "HTTP URL host is invalid"));
}
Ok(HttpOrigin {
scheme,
host: host.to_ascii_lowercase(),
port,
})
}
fn parse_port(port: &str, error_kind: ErrorKind) -> Result<u16, Error> {
port.parse::<u16>()
.ok()
.filter(|port| *port != 0)
.ok_or_else(|| Error::new(error_kind, "HTTP URL port is invalid"))
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Response {
status: u16,
headers: Vec<Header>,
body: crate::ResponseBody,
}
impl Response {
#[cfg(feature = "native")]
pub(crate) fn from_buffer(
status: u16,
headers: Vec<Header>,
body: crate::body_budget::BodyBuffer,
) -> Self {
Self {
status,
headers,
body: crate::ResponseBody::from_buffer(body),
}
}
#[must_use]
pub fn new(status: u16, headers: Vec<Header>, body: Vec<u8>) -> Self {
Self {
status,
headers,
body: crate::ResponseBody::from_vec(body),
}
}
#[must_use]
pub fn status(&self) -> u16 {
self.status
}
#[must_use]
pub fn headers(&self) -> &[Header] {
&self.headers
}
#[must_use]
pub fn body(&self) -> &[u8] {
self.body.as_bytes()
}
#[must_use]
pub fn into_body(self) -> crate::ResponseBody {
self.body
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct RequestId {
pub(crate) engine: u64,
pub(crate) sequence: u64,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Completion {
Completed(Response),
Failed(Error),
Cancelled,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ErrorKind {
EngineStopped,
WrongEngine,
WrongMode,
ReentrantDrive,
ReentrantOperation,
InvalidRequest,
QueueFull,
Limit,
BackendUnavailable,
Unsupported,
Transport,
Timeout,
Redirect,
Internal,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum TimeoutKind {
Connect,
Inactivity,
Total,
Unknown,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum TransportStage {
Dns,
Connect,
Tls,
Send,
Receive,
Http,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum TlsFailure {
Configuration,
CertificateHostnameMismatch,
CertificateUnknownIssuer,
CertificateExpired,
CertificateNotYetValid,
CertificateRevoked,
CertificateInvalid,
PeerAlert,
Protocol,
Truncated,
Io,
Unknown,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum DnsFailure {
ServerFailure,
Refused,
Malformed,
Truncated,
NoNameserver,
Protocol,
Io,
Unknown,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum LimitKind {
BufferedBodyBytes,
RequestBodyBytes,
RequestHeaderBytes,
RequestHeaderCount,
ResponseBodyBytes,
ResponseHeaderBytes,
ResponseHeaderCount,
StreamingQueueBytes,
ResolveResults,
TcpQueueBytes,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Error {
kind: ErrorKind,
message: String,
timeout_kind: Option<TimeoutKind>,
transport_stage: Option<TransportStage>,
tls_failure: Option<TlsFailure>,
dns_failure: Option<DnsFailure>,
limit_kind: Option<LimitKind>,
}
impl Error {
pub(crate) fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
timeout_kind: None,
transport_stage: None,
tls_failure: None,
dns_failure: None,
limit_kind: None,
}
}
#[allow(dead_code)] pub(crate) fn timeout(kind: TimeoutKind, message: impl Into<String>) -> Self {
let mut error = Self::new(ErrorKind::Timeout, message);
error.timeout_kind = Some(kind);
error
}
#[allow(dead_code)] pub(crate) fn transport(stage: TransportStage, message: impl Into<String>) -> Self {
let mut error = Self::new(ErrorKind::Transport, message);
error.transport_stage = Some(stage);
error
}
#[allow(dead_code)] pub(crate) fn tls(
stage: TransportStage,
failure: TlsFailure,
message: impl Into<String>,
) -> Self {
let mut error = Self::transport(stage, message);
error.tls_failure = Some(failure);
error
}
#[cfg_attr(not(feature = "native"), allow(dead_code))]
pub(crate) fn dns(failure: DnsFailure, message: impl Into<String>) -> Self {
let mut error = Self::transport(TransportStage::Dns, message);
error.dns_failure = Some(failure);
error
}
pub(crate) fn limit(kind: LimitKind, message: impl Into<String>) -> Self {
let mut error = Self::new(ErrorKind::Limit, message);
error.limit_kind = Some(kind);
error
}
#[must_use]
pub fn kind(&self) -> ErrorKind {
self.kind
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
#[must_use]
pub fn timeout_kind(&self) -> Option<TimeoutKind> {
self.timeout_kind
}
#[must_use]
pub fn transport_stage(&self) -> Option<TransportStage> {
self.transport_stage
}
#[must_use]
pub fn tls_failure(&self) -> Option<TlsFailure> {
self.tls_failure
}
#[must_use]
pub fn dns_failure(&self) -> Option<DnsFailure> {
self.dns_failure
}
#[must_use]
pub fn limit_kind(&self) -> Option<LimitKind> {
self.limit_kind
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.message)
}
}
impl StdError for Error {}
#[derive(Debug)]
#[non_exhaustive]
pub enum ExecuteError {
Submission(Error),
Failed(Error),
Cancelled,
}
impl fmt::Display for ExecuteError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Submission(error) => write!(formatter, "operation was not accepted: {error}"),
Self::Failed(error) => write!(formatter, "operation failed: {error}"),
Self::Cancelled => formatter.write_str("operation was cancelled"),
}
}
}
impl StdError for ExecuteError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::Submission(error) | Self::Failed(error) => Some(error),
Self::Cancelled => None,
}
}
}
#[derive(Debug)]
pub struct ShutdownError {
source: Error,
}
impl ShutdownError {
pub(crate) fn new(source: Error) -> Self {
Self { source }
}
#[must_use]
pub fn error(&self) -> &Error {
&self.source
}
}
impl fmt::Display for ShutdownError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "Engine shutdown failed: {}", self.source)
}
}
impl StdError for ShutdownError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.source)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn public_resolution_budget_cannot_consume_http_dns_txid_reserve() {
let huge = NonZeroUsize::new(usize::MAX).expect("usize::MAX is non-zero");
let config = EngineConfig::spawned().with_max_inflight_resolutions(huge);
assert_eq!(
config.max_inflight_resolutions().get(),
MAX_PUBLIC_DNS_TRANSACTIONS
);
assert_eq!(
config.max_inflight_resolutions().get() + HTTP_DNS_TXID_RESERVE,
DNS_TRANSACTION_ID_SPACE
);
}
#[test]
fn request_builder_rejects_non_http_urls_invalid_tokens_and_header_injection() {
for builder in [
Request::get(""),
Request::get("ftp://example.test/"),
Request::get("http://user@example.test/"),
Request::get("http://bad host/"),
Request::get("http://example.test:0/"),
Request::builder(
Method::Other("NOT VALID".to_owned()),
"http://example.test/",
),
Request::get("http://example.test/").header("bad:name", "value"),
Request::get("http://example.test/").header("good-name", b"bad\r\nvalue".to_vec()),
Request::get("http://example.test/").header("good-name", b"bad\0value".to_vec()),
] {
let error = builder
.build()
.expect_err("invalid request must fail during construction");
assert_eq!(error.kind(), ErrorKind::InvalidRequest);
}
}
#[test]
fn request_builder_convenience_methods_set_portable_policy() {
let request = Request::builder(Method::Other("PURGE".to_owned()), "https://[::1]:8443/")
.connect_timeout(Duration::from_millis(100))
.inactivity_timeout(Duration::from_millis(200))
.total_timeout(Duration::from_millis(300))
.redirect_limit(2)
.tls_verification(TlsVerification::DangerouslyDisableCertificateVerification)
.build()
.expect("valid portable request must build");
assert_eq!(
request.options.connect_timeout,
Some(Duration::from_millis(100))
);
assert_eq!(
request.options.inactivity_timeout,
Some(Duration::from_millis(200))
);
assert_eq!(
request.options.total_timeout,
Some(Duration::from_millis(300))
);
assert_eq!(request.options.redirect_limit, 2);
assert_eq!(
request.options.tls_verification,
TlsVerification::DangerouslyDisableCertificateVerification
);
}
#[test]
fn redirect_policy_is_lazy_and_preserves_head_on_303() {
let post = Request::post("https://example.test/start")
.body(b"payload".to_vec())
.build()
.expect("redirect source must build");
let not_followed = plan_redirect(&post, 302, 0, || {
panic!("a non-followed redirect must not inspect Location")
})
.expect("302 POST must remain a response");
assert!(not_followed.is_none());
let disabled = Request::get("https://example.test/start")
.redirect_limit(0)
.build()
.expect("disabled redirect source must build");
let not_followed = plan_redirect(&disabled, 302, 0, || {
panic!("disabled redirects must not inspect Location")
})
.expect("disabled redirect must remain a response");
assert!(not_followed.is_none());
let head = Request::head("https://example.test/start")
.build()
.expect("HEAD redirect source must build");
let redirected = plan_redirect(&head, 303, 0, || {
Ok(Some("https://example.test/final".to_owned()))
})
.expect("303 HEAD policy must succeed")
.expect("303 HEAD must follow")
.apply(head.clone());
assert_eq!(redirected.method(), &Method::Head);
assert!(redirected.body().is_empty());
let downgrade = plan_redirect(&head, 307, 0, || {
Ok(Some("http://example.test/final".to_owned()))
})
.expect_err("HTTPS downgrade must be blocked");
assert_eq!(downgrade.kind(), ErrorKind::Redirect);
}
}