use core::time::Duration;
pub const DEFAULT_MAX_RESPONSE_BODY_SIZE: usize = 10 * 1024 * 1024;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[allow(clippy::struct_excessive_bools)]
pub struct Config {
user_agent: alloc::string::String,
max_redirects: u32,
http_status_as_error: bool,
max_response_header_size: usize,
max_response_body_size: usize,
timeout_connect: Option<Duration>,
timeout_read: Option<Duration>,
timeout_write: Option<Duration>,
accept: alloc::string::String,
https_only: bool,
assume_tls_socket: bool,
max_idle_per_host: usize,
max_idle_age: Duration,
}
impl Default for Config {
fn default() -> Self {
Self {
user_agent: alloc::string::String::from(concat!("barehttp/", env!("CARGO_PKG_VERSION"))),
max_redirects: 10,
http_status_as_error: true,
max_response_header_size: 64 * 1024,
max_response_body_size: DEFAULT_MAX_RESPONSE_BODY_SIZE,
timeout_connect: Some(Duration::from_secs(10)),
timeout_read: Some(Duration::from_secs(30)),
timeout_write: Some(Duration::from_secs(30)),
accept: alloc::string::String::from("*/*"),
https_only: false,
assume_tls_socket: false,
max_idle_per_host: 3,
max_idle_age: Duration::from_secs(15),
}
}
}
impl Config {
#[must_use]
pub fn builder() -> ConfigBuilder {
ConfigBuilder { config: Self::default() }
}
#[must_use]
pub fn user_agent(&self) -> &str {
&self.user_agent
}
#[must_use]
pub const fn max_redirects(&self) -> u32 {
self.max_redirects
}
#[must_use]
pub const fn http_status_as_error(&self) -> bool {
self.http_status_as_error
}
#[must_use]
pub const fn max_response_header_size(&self) -> usize {
self.max_response_header_size
}
#[must_use]
pub const fn max_response_body_size(&self) -> usize {
self.max_response_body_size
}
#[must_use]
pub const fn timeout_connect(&self) -> Option<Duration> {
self.timeout_connect
}
#[must_use]
pub const fn timeout_read(&self) -> Option<Duration> {
self.timeout_read
}
#[must_use]
pub const fn timeout_write(&self) -> Option<Duration> {
self.timeout_write
}
#[must_use]
pub fn accept(&self) -> &str {
&self.accept
}
#[must_use]
pub const fn https_only(&self) -> bool {
self.https_only
}
#[must_use]
pub const fn assume_tls_socket(&self) -> bool {
self.assume_tls_socket
}
#[must_use]
pub const fn max_idle_per_host(&self) -> usize {
self.max_idle_per_host
}
#[must_use]
pub const fn max_idle_age(&self) -> Duration {
self.max_idle_age
}
pub(crate) const fn set_timeout_connect(
&mut self,
v: Option<Duration>,
) {
self.timeout_connect = v;
}
pub(crate) const fn set_timeout_read(
&mut self,
v: Option<Duration>,
) {
self.timeout_read = v;
}
pub(crate) const fn set_timeout_write(
&mut self,
v: Option<Duration>,
) {
self.timeout_write = v;
}
pub(crate) const fn set_max_response_body_size(
&mut self,
v: usize,
) {
self.max_response_body_size = v;
}
}
#[must_use = "builders do nothing unless you call `.build()`"]
#[derive(Debug, Clone)]
pub struct ConfigBuilder {
config: Config,
}
impl ConfigBuilder {
#[must_use]
pub fn build(self) -> Config {
self.config
}
#[must_use]
pub fn user_agent(
mut self,
v: impl AsRef<str>,
) -> Self {
self.config.user_agent = alloc::string::String::from(v.as_ref());
self
}
#[must_use]
pub const fn max_redirects(
mut self,
v: u32,
) -> Self {
self.config.max_redirects = v;
self
}
#[must_use]
pub const fn http_status_as_error(
mut self,
v: bool,
) -> Self {
self.config.http_status_as_error = v;
self
}
#[must_use]
pub const fn max_response_header_size(
mut self,
v: usize,
) -> Self {
self.config.max_response_header_size = v;
self
}
#[must_use]
pub const fn max_response_body_size(
mut self,
v: usize,
) -> Self {
self.config.max_response_body_size = v;
self
}
#[must_use]
pub const fn timeout_connect(
mut self,
v: Option<Duration>,
) -> Self {
self.config.timeout_connect = v;
self
}
#[must_use]
pub const fn timeout_read(
mut self,
v: Option<Duration>,
) -> Self {
self.config.timeout_read = v;
self
}
#[must_use]
pub const fn timeout_write(
mut self,
v: Option<Duration>,
) -> Self {
self.config.timeout_write = v;
self
}
#[must_use]
pub fn accept(
mut self,
v: impl AsRef<str>,
) -> Self {
self.config.accept = alloc::string::String::from(v.as_ref());
self
}
#[must_use]
pub const fn https_only(
mut self,
v: bool,
) -> Self {
self.config.https_only = v;
self
}
#[must_use]
pub const fn assume_tls_socket(
mut self,
v: bool,
) -> Self {
self.config.assume_tls_socket = v;
self
}
#[must_use]
pub const fn max_idle_per_host(
mut self,
v: usize,
) -> Self {
self.config.max_idle_per_host = v;
self
}
#[must_use]
pub const fn max_idle_age(
mut self,
v: Duration,
) -> Self {
self.config.max_idle_age = v;
self
}
}