use std::time::Duration;
pub use http2::frame::{
ExperimentalSettings, Priorities, PrioritiesBuilder, Priority, PseudoId, PseudoOrder, Setting,
SettingId, SettingsOrder, SettingsOrderBuilder, StreamDependency, StreamId,
};
use super::proto;
const DEFAULT_CONN_WINDOW_SIZE: u32 = 1024 * 1024 * 5; const DEFAULT_WINDOW_SIZE: u32 = 1024 * 1024 * 2; const DEFAULT_MAX_SEND_BUF_SIZE: usize = 1024 * 1024;
const DEFAULT_INITIAL_MAX_SEND_STREAMS: usize = 100;
#[must_use]
#[derive(Debug)]
pub struct Http2OptionsBuilder {
opts: Http2Options,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[non_exhaustive]
pub struct Http2Options {
pub adaptive_window: bool,
pub initial_stream_id: Option<u32>,
pub initial_conn_window_size: u32,
pub initial_window_size: u32,
pub initial_max_send_streams: usize,
pub max_frame_size: Option<u32>,
pub keep_alive_interval: Option<Duration>,
pub keep_alive_timeout: Duration,
pub keep_alive_while_idle: bool,
pub max_concurrent_reset_streams: Option<usize>,
pub max_send_buffer_size: usize,
pub max_concurrent_streams: Option<u32>,
pub max_header_list_size: Option<u32>,
pub max_pending_accept_reset_streams: Option<usize>,
pub enable_push: Option<bool>,
pub header_table_size: Option<u32>,
pub enable_connect_protocol: Option<bool>,
pub no_rfc7540_priorities: Option<bool>,
pub headers_pseudo_order: Option<PseudoOrder>,
pub headers_stream_dependency: Option<StreamDependency>,
pub experimental_settings: Option<ExperimentalSettings>,
pub settings_order: Option<SettingsOrder>,
pub priorities: Option<Priorities>,
}
impl Http2OptionsBuilder {
#[inline]
pub fn initial_window_size(mut self, sz: impl Into<Option<u32>>) -> Self {
if let Some(sz) = sz.into() {
self.opts.adaptive_window = false;
self.opts.initial_window_size = sz;
}
self
}
#[inline]
pub fn initial_connection_window_size(mut self, sz: impl Into<Option<u32>>) -> Self {
if let Some(sz) = sz.into() {
self.opts.adaptive_window = false;
self.opts.initial_conn_window_size = sz;
}
self
}
#[inline]
pub fn initial_max_send_streams(mut self, initial: impl Into<Option<usize>>) -> Self {
if let Some(initial) = initial.into() {
self.opts.initial_max_send_streams = initial;
}
self
}
#[inline]
pub fn initial_stream_id(mut self, id: impl Into<Option<u32>>) -> Self {
if let Some(id) = id.into() {
self.opts.initial_stream_id = Some(id);
}
self
}
#[inline]
pub fn adaptive_window(mut self, enabled: bool) -> Self {
use proto::h2::SPEC_WINDOW_SIZE;
self.opts.adaptive_window = enabled;
if enabled {
self.opts.initial_window_size = SPEC_WINDOW_SIZE;
self.opts.initial_conn_window_size = SPEC_WINDOW_SIZE;
}
self
}
#[inline]
pub fn max_frame_size(mut self, sz: impl Into<Option<u32>>) -> Self {
if let Some(sz) = sz.into() {
self.opts.max_frame_size = Some(sz);
}
self
}
#[inline]
pub fn max_header_list_size(mut self, max: u32) -> Self {
self.opts.max_header_list_size = Some(max);
self
}
#[inline]
pub fn header_table_size(mut self, size: impl Into<Option<u32>>) -> Self {
if let Some(size) = size.into() {
self.opts.header_table_size = Some(size);
}
self
}
#[inline]
pub fn max_concurrent_streams(mut self, max: impl Into<Option<u32>>) -> Self {
if let Some(max) = max.into() {
self.opts.max_concurrent_streams = Some(max);
}
self
}
#[inline]
pub fn keep_alive_interval(mut self, interval: impl Into<Option<Duration>>) -> Self {
self.opts.keep_alive_interval = interval.into();
self
}
#[inline]
pub fn keep_alive_timeout(mut self, timeout: Duration) -> Self {
self.opts.keep_alive_timeout = timeout;
self
}
#[inline]
pub fn keep_alive_while_idle(mut self, enabled: bool) -> Self {
self.opts.keep_alive_while_idle = enabled;
self
}
#[inline]
pub fn enable_push(mut self, opt: bool) -> Self {
self.opts.enable_push = Some(opt);
self
}
#[inline]
pub fn enable_connect_protocol(mut self, opt: bool) -> Self {
self.opts.enable_connect_protocol = Some(opt);
self
}
#[inline]
pub fn no_rfc7540_priorities(mut self, opt: bool) -> Self {
self.opts.no_rfc7540_priorities = Some(opt);
self
}
#[inline]
pub fn max_concurrent_reset_streams(mut self, max: usize) -> Self {
self.opts.max_concurrent_reset_streams = Some(max);
self
}
#[inline]
pub fn max_send_buf_size(mut self, max: usize) -> Self {
assert!(max <= u32::MAX as usize);
self.opts.max_send_buffer_size = max;
self
}
#[inline]
pub fn max_pending_accept_reset_streams(mut self, max: impl Into<Option<usize>>) -> Self {
if let Some(max) = max.into() {
self.opts.max_pending_accept_reset_streams = Some(max);
}
self
}
#[inline]
pub fn headers_stream_dependency<T>(mut self, stream_dependency: T) -> Self
where
T: Into<Option<StreamDependency>>,
{
if let Some(stream_dependency) = stream_dependency.into() {
self.opts.headers_stream_dependency = Some(stream_dependency);
}
self
}
#[inline]
pub fn headers_pseudo_order<T>(mut self, headers_pseudo_order: T) -> Self
where
T: Into<Option<PseudoOrder>>,
{
if let Some(headers_pseudo_order) = headers_pseudo_order.into() {
self.opts.headers_pseudo_order = Some(headers_pseudo_order);
}
self
}
#[inline]
pub fn experimental_settings<T>(mut self, experimental_settings: T) -> Self
where
T: Into<Option<ExperimentalSettings>>,
{
if let Some(experimental_settings) = experimental_settings.into() {
self.opts.experimental_settings = Some(experimental_settings);
}
self
}
#[inline]
pub fn settings_order<T>(mut self, settings_order: T) -> Self
where
T: Into<Option<SettingsOrder>>,
{
if let Some(settings_order) = settings_order.into() {
self.opts.settings_order = Some(settings_order);
}
self
}
#[inline]
pub fn priorities<T>(mut self, priorities: T) -> Self
where
T: Into<Option<Priorities>>,
{
if let Some(priorities) = priorities.into() {
self.opts.priorities = Some(priorities);
}
self
}
#[inline]
pub fn build(self) -> Http2Options {
self.opts
}
}
impl Http2Options {
pub fn builder() -> Http2OptionsBuilder {
Http2OptionsBuilder {
opts: Http2Options::default(),
}
}
}
impl Default for Http2Options {
#[inline]
fn default() -> Self {
Http2Options {
adaptive_window: false,
initial_stream_id: None,
initial_conn_window_size: DEFAULT_CONN_WINDOW_SIZE,
initial_window_size: DEFAULT_WINDOW_SIZE,
initial_max_send_streams: DEFAULT_INITIAL_MAX_SEND_STREAMS,
max_frame_size: None,
max_header_list_size: None,
keep_alive_interval: None,
keep_alive_timeout: Duration::from_secs(20),
keep_alive_while_idle: false,
max_concurrent_reset_streams: None,
max_send_buffer_size: DEFAULT_MAX_SEND_BUF_SIZE,
max_pending_accept_reset_streams: None,
header_table_size: None,
max_concurrent_streams: None,
enable_push: None,
enable_connect_protocol: None,
no_rfc7540_priorities: None,
experimental_settings: None,
settings_order: None,
headers_pseudo_order: None,
headers_stream_dependency: None,
priorities: None,
}
}
}