use std::{net::IpAddr, time::Duration};
use crate::{Error, Result};
const MIN_HTTP2_FRAME_SIZE: u32 = 16 * 1024;
const MAX_HTTP2_FRAME_SIZE: u32 = (1 << 24) - 1;
const MIN_HTTP1_BUFFER_SIZE: usize = 8 * 1024;
const DEFAULT_HTTP1_BUFFER_SIZE: usize = 8 * 1024 + 4 * 1024 * 100;
const DEFAULT_HTTP1_HEADERS: usize = 100;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Settings {
local_address: Option<IpAddr>,
nodelay: bool,
keepalive: Option<Duration>,
http1: Http1,
http2: Http2,
}
impl Settings {
#[must_use]
pub fn local_address(mut self, address: impl Into<Option<IpAddr>>) -> Self {
self.local_address = address.into();
self
}
#[must_use]
pub const fn nodelay(mut self, enabled: bool) -> Self {
self.nodelay = enabled;
self
}
#[must_use]
pub fn keepalive(mut self, idle: impl Into<Option<Duration>>) -> Self {
self.keepalive = idle.into();
self
}
#[must_use]
pub const fn http2(mut self, http2: Http2) -> Self {
self.http2 = http2;
self
}
#[must_use]
pub const fn http1(mut self, http1: Http1) -> Self {
self.http1 = http1;
self
}
pub(crate) const fn local(self) -> Option<IpAddr> {
self.local_address
}
pub(crate) const fn uses_nodelay(self) -> bool {
self.nodelay
}
pub(crate) const fn keepalive_idle(self) -> Option<Duration> {
self.keepalive
}
pub(crate) const fn http2_policy(self) -> Http2 {
self.http2
}
pub(crate) const fn http1_policy(self) -> Http1 {
self.http1
}
pub(crate) fn validate(self) -> Result<()> {
if self.keepalive.is_some_and(|duration| duration.is_zero()) {
return Err(Error::request(
"client TCP keepalive duration must be greater than zero",
));
}
self.http1.validate()?;
self.http2.validate()
}
}
impl Default for Settings {
fn default() -> Self {
Self {
local_address: None,
nodelay: true,
keepalive: None,
http1: Http1::default(),
http2: Http2::default(),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Http1 {
max_headers: usize,
max_buffer_size: usize,
}
impl Http1 {
#[must_use]
pub const fn max_headers(mut self, headers: usize) -> Self {
self.max_headers = headers;
self
}
#[must_use]
pub const fn max_buffer_size(mut self, bytes: usize) -> Self {
self.max_buffer_size = bytes;
self
}
pub(crate) fn apply(self, builder: &mut hyper::client::conn::http1::Builder) {
builder
.writev(true)
.max_headers(self.max_headers)
.max_buf_size(self.max_buffer_size);
}
fn validate(self) -> Result<()> {
if self.max_headers == 0 {
return Err(Error::request(
"client HTTP/1 header limit must be greater than zero",
));
}
if self.max_buffer_size < MIN_HTTP1_BUFFER_SIZE {
return Err(Error::request(
"client HTTP/1 buffer limit must be at least 8192 bytes",
));
}
Ok(())
}
}
impl Default for Http1 {
fn default() -> Self {
Self {
max_headers: DEFAULT_HTTP1_HEADERS,
max_buffer_size: DEFAULT_HTTP1_BUFFER_SIZE,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Http2 {
enabled: bool,
prior_knowledge: bool,
adaptive_window: bool,
stream_window: Option<u32>,
connection_window: Option<u32>,
max_frame_size: Option<u32>,
max_header_list_size: Option<u32>,
max_send_buffer_size: Option<usize>,
keepalive_interval: Option<Duration>,
keepalive_timeout: Option<Duration>,
keepalive_while_idle: bool,
}
impl Http2 {
#[must_use]
pub const fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
if !enabled {
self.prior_knowledge = false;
}
self
}
#[must_use]
pub const fn prior_knowledge(mut self, enabled: bool) -> Self {
self.prior_knowledge = enabled;
if enabled {
self.enabled = true;
}
self
}
#[must_use]
pub const fn adaptive_window(mut self, enabled: bool) -> Self {
self.adaptive_window = enabled;
self
}
#[must_use]
pub fn stream_window(mut self, bytes: impl Into<Option<u32>>) -> Self {
self.stream_window = bytes.into();
self
}
#[must_use]
pub fn connection_window(mut self, bytes: impl Into<Option<u32>>) -> Self {
self.connection_window = bytes.into();
self
}
#[must_use]
pub fn max_frame_size(mut self, bytes: impl Into<Option<u32>>) -> Self {
self.max_frame_size = bytes.into();
self
}
#[must_use]
pub fn max_header_list_size(mut self, bytes: impl Into<Option<u32>>) -> Self {
self.max_header_list_size = bytes.into();
self
}
#[must_use]
pub fn max_send_buffer_size(mut self, bytes: impl Into<Option<usize>>) -> Self {
self.max_send_buffer_size = bytes.into();
self
}
#[must_use]
pub fn keepalive_interval(mut self, interval: impl Into<Option<Duration>>) -> Self {
self.keepalive_interval = interval.into();
self
}
#[must_use]
pub fn keepalive_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
self.keepalive_timeout = timeout.into();
self
}
#[must_use]
pub const fn keepalive_while_idle(mut self, enabled: bool) -> Self {
self.keepalive_while_idle = enabled;
self
}
pub(crate) const fn uses_prior_knowledge(self) -> bool {
self.prior_knowledge
}
pub(crate) const fn is_enabled(self) -> bool {
self.enabled
}
pub(crate) fn apply(self, builder: &mut hyper::client::conn::http2::Builder<crate::Executor>) {
builder.adaptive_window(self.adaptive_window);
builder.initial_stream_window_size(self.stream_window);
builder.initial_connection_window_size(self.connection_window);
builder.max_frame_size(self.max_frame_size);
if let Some(size) = self.max_header_list_size {
builder.max_header_list_size(size);
}
if let Some(size) = self.max_send_buffer_size {
builder.max_send_buf_size(size);
}
builder.keep_alive_interval(self.keepalive_interval);
if let Some(timeout) = self.keepalive_timeout {
builder.keep_alive_timeout(timeout);
}
builder.keep_alive_while_idle(self.keepalive_while_idle);
}
fn validate(self) -> Result<()> {
if self.stream_window.is_some_and(|window| window == 0)
|| self.connection_window.is_some_and(|window| window == 0)
{
return Err(Error::request(
"client HTTP/2 flow-control windows must be greater than zero",
));
}
if self
.max_frame_size
.is_some_and(|size| !(MIN_HTTP2_FRAME_SIZE..=MAX_HTTP2_FRAME_SIZE).contains(&size))
{
return Err(Error::request(
"client HTTP/2 frame size is outside the protocol range",
));
}
if self.max_header_list_size == Some(0) {
return Err(Error::request(
"client HTTP/2 header-list limit must be greater than zero",
));
}
if self
.max_send_buffer_size
.is_some_and(|size| size == 0 || size > u32::MAX as usize)
{
return Err(Error::request(
"client HTTP/2 send-buffer limit must fit in a nonzero u32",
));
}
for duration in [self.keepalive_interval, self.keepalive_timeout] {
if duration.is_some_and(|duration| duration.is_zero()) {
return Err(Error::request(
"client HTTP/2 keepalive durations must be greater than zero",
));
}
}
Ok(())
}
}
impl Default for Http2 {
fn default() -> Self {
Self {
enabled: true,
prior_knowledge: false,
adaptive_window: false,
stream_window: None,
connection_window: None,
max_frame_size: None,
max_header_list_size: None,
max_send_buffer_size: None,
keepalive_interval: None,
keepalive_timeout: None,
keepalive_while_idle: false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_favor_small_writes_without_forcing_keepalive() {
let settings = Settings::default();
assert!(settings.uses_nodelay());
assert_eq!(settings.keepalive_idle(), None);
assert!(!settings.http2_policy().uses_prior_knowledge());
}
#[test]
fn invalid_socket_and_protocol_values_are_rejected() {
assert!(
Settings::default()
.keepalive(Duration::ZERO)
.validate()
.unwrap_err()
.is_request()
);
assert!(
Settings::default()
.http1(Http1::default().max_headers(0))
.validate()
.unwrap_err()
.is_request()
);
assert!(
Settings::default()
.http1(Http1::default().max_buffer_size(MIN_HTTP1_BUFFER_SIZE - 1))
.validate()
.unwrap_err()
.is_request()
);
assert!(
Settings::default()
.http2(Http2::default().max_frame_size(1024))
.validate()
.unwrap_err()
.is_request()
);
assert!(
Settings::default()
.http2(Http2::default().max_header_list_size(0))
.validate()
.unwrap_err()
.is_request()
);
assert!(
Settings::default()
.http2(Http2::default().max_send_buffer_size(0))
.validate()
.unwrap_err()
.is_request()
);
}
}