Skip to content

Commit

Permalink
adjusted config for differentiated http1 and http2 configs
Browse files Browse the repository at this point in the history
  • Loading branch information
tr8dr committed Sep 3, 2024
1 parent 6d93b42 commit 8194652
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ proxy-protocol = ["ppp", "pin-project-lite"]

# optional dependencies
## rustls
arc-swap = { version = "1", optional = true }
bytes = "1"
arc-swap = { version = "1.7", optional = true }
bytes = "1.7"
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
http = "0.2"
http-body = "0.4"
hyper = { version = "0.14.27", features = ["http1", "http2", "server", "runtime"] }
hyper = { version = "1.4.1", features = ["http1", "http2", "server"] }
hyper-util = { version = "0.1.7" }

## openssl
openssl = { version = "0.10", optional = true }
pin-project-lite = { version = "0.2", optional = true }
rustls = { version = "0.21", features = ["dangerous_configuration"], optional = true }
rustls-pemfile = { version = "1", optional = true }
tokio = { version = "1", features = ["macros", "net", "sync"] }
rustls = { version = "0.23", optional = true }
rustls-pemfile = { version = "2.1.3", optional = true }
tokio = { version = "1.40.0", features = ["macros", "net", "sync"] }
tokio-openssl = { version = "0.6", optional = true }
tokio-rustls = { version = "0.24", optional = true }
tower-service = "0.3"
Expand All @@ -42,9 +43,9 @@ tower-service = "0.3"
ppp = { version = "2.2.0", optional = true }

[dev-dependencies]
axum = "0.6"
hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] }
axum = "0.7"
hyper = { version = "1.4.1", features = ["full"] }
tokio = { version = "1.40", features = ["full"] }
tower = { version = "0.4", features = ["util"] }
tower-http = { version = "0.4.4", features = ["add-extension"] }

Expand Down
58 changes: 34 additions & 24 deletions src/http_config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use hyper::server::conn::Http;
use hyper::server::conn::http1::Builder as Http1Config;
use hyper::server::conn::http2::Builder as Http2Config;
use std::time::Duration;
use hyper_util::rt::TokioExecutor;

/// Represents a configuration for the [`Http`] protocol.
/// This allows for detailed customization of various HTTP/1 and HTTP/2 settings.
#[derive(Debug, Clone)]
pub struct HttpConfig {
/// The inner HTTP configuration from the `hyper` crate.
pub(crate) inner: Http,
pub(crate) http1: Http1Config,
pub(crate) http2: Http2Config<TokioExecutor>,
pub http1_only: bool,
pub http2_only: bool,
}

impl Default for HttpConfig {
Expand All @@ -19,7 +24,12 @@ impl Default for HttpConfig {
impl HttpConfig {
/// Creates a new `HttpConfig` with default settings.
pub fn new() -> HttpConfig {
Self { inner: Http::new() }
Self {
http1: Http1Config::new(),
http2: Http2Config::new(),
http1_only: false,
http2_only: false,
}
}

/// Clones the current configuration state and returns it.
Expand All @@ -34,7 +44,7 @@ impl HttpConfig {
///
/// Default is `false`.
pub fn http1_only(&mut self, val: bool) -> &mut Self {
self.inner.http1_only(val);
self.http1_only = true;
self
}

Expand All @@ -47,7 +57,7 @@ impl HttpConfig {
///
/// Default is `false`.
pub fn http1_half_close(&mut self, val: bool) -> &mut Self {
self.inner.http1_half_close(val);
self.http1.half_close(val)
self
}

Expand All @@ -57,7 +67,7 @@ impl HttpConfig {
///
/// Default is true.
pub fn http1_keep_alive(&mut self, val: bool) -> &mut Self {
self.inner.http1_keep_alive(val);
self.http1.keep_alive(val);
self
}

Expand All @@ -68,7 +78,7 @@ impl HttpConfig {
///
/// Default is false.
pub fn http1_title_case_headers(&mut self, enabled: bool) -> &mut Self {
self.inner.http1_title_case_headers(enabled);
self.http1.title_case_headers(enabled);
self
}

Expand All @@ -79,7 +89,7 @@ impl HttpConfig {
///
/// Default is false.
pub fn http1_preserve_header_case(&mut self, enabled: bool) -> &mut Self {
self.inner.http1_preserve_header_case(enabled);
self.http1.preserve_header_case(enabled);
self
}

Expand All @@ -89,7 +99,7 @@ impl HttpConfig {
///
/// Default is None, meaning no timeout.
pub fn http1_header_read_timeout(&mut self, val: Duration) -> &mut Self {
self.inner.http1_header_read_timeout(val);
self.http1.header_read_timeout(val);
self
}

Expand All @@ -101,7 +111,7 @@ impl HttpConfig {
///
/// Default is `auto`, where the best method is determined dynamically.
pub fn http1_writev(&mut self, val: bool) -> &mut Self {
self.inner.http1_writev(val);
self.http1.writev(val);
self
}

Expand All @@ -111,7 +121,7 @@ impl HttpConfig {
///
/// Default is false.
pub fn http2_only(&mut self, val: bool) -> &mut Self {
self.inner.http2_only(val);
self.http2_only = true;
self
}

Expand All @@ -124,7 +134,7 @@ impl HttpConfig {
///
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_INITIAL_WINDOW_SIZE
pub fn http2_initial_stream_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
self.inner.http2_initial_stream_window_size(sz);
self.http2.initial_stream_window_size(sz);
self
}

Expand All @@ -137,7 +147,7 @@ impl HttpConfig {
&mut self,
sz: impl Into<Option<u32>>,
) -> &mut Self {
self.inner.http2_initial_connection_window_size(sz);
self.http2.initial_connection_window_size(sz);
self
}

Expand All @@ -147,15 +157,15 @@ impl HttpConfig {
/// `http2_initial_stream_window_size` and
/// `http2_initial_connection_window_size`.
pub fn http2_adaptive_window(&mut self, enabled: bool) -> &mut Self {
self.inner.http2_adaptive_window(enabled);
self.http2.adaptive_window(enabled);
self
}

/// Enables the [extended CONNECT protocol].
///
/// [extended CONNECT protocol]: https://datatracker.ietf.org/doc/html/rfc8441#section-4
pub fn http2_enable_connect_protocol(&mut self) -> &mut Self {
self.inner.http2_enable_connect_protocol();
self.http2.enable_connect_protocol();
self
}

Expand All @@ -165,7 +175,7 @@ impl HttpConfig {
///
/// If not set, hyper will use a default.
pub fn http2_max_frame_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
self.inner.http2_max_frame_size(sz);
self.http2.max_frame_size(sz);
self
}

Expand All @@ -176,15 +186,15 @@ impl HttpConfig {
///
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_MAX_CONCURRENT_STREAMS
pub fn http2_max_concurrent_streams(&mut self, max: impl Into<Option<u32>>) -> &mut Self {
self.inner.http2_max_concurrent_streams(max);
self.http2.max_concurrent_streams(max);
self
}

/// Sets the max size of received header frames.
///
/// Default is currently ~16MB, but may change.
pub fn http2_max_header_list_size(&mut self, max: u32) -> &mut Self {
self.inner.http2_max_header_list_size(max);
self.http2.max_header_list_size(max);
self
}

Expand All @@ -198,7 +208,7 @@ impl HttpConfig {
&mut self,
max: impl Into<Option<usize>>,
) -> &mut Self {
self.inner.http2_max_pending_accept_reset_streams(max);
self.http2.max_pending_accept_reset_streams(max);
self
}

Expand All @@ -210,7 +220,7 @@ impl HttpConfig {
///
/// The value must be no larger than `u32::MAX`.
pub fn http2_max_send_buf_size(&mut self, max: usize) -> &mut Self {
self.inner.http2_max_send_buf_size(max);
self.http2.max_send_buf_size(max);
self
}

Expand All @@ -224,7 +234,7 @@ impl HttpConfig {
&mut self,
interval: impl Into<Option<Duration>>,
) -> &mut Self {
self.inner.http2_keep_alive_interval(interval);
self.http2.keep_alive_interval(interval);
self
}

Expand All @@ -235,7 +245,7 @@ impl HttpConfig {
///
/// Default is 20 seconds.
pub fn http2_keep_alive_timeout(&mut self, timeout: Duration) -> &mut Self {
self.inner.http2_keep_alive_timeout(timeout);
self.http2.keep_alive_timeout(timeout);
self
}

Expand All @@ -247,7 +257,7 @@ impl HttpConfig {
///
/// The minimum value allowed is 8192. This method panics if the passed `max` is less than the minimum.
pub fn max_buf_size(&mut self, max: usize) -> &mut Self {
self.inner.max_buf_size(max);
self.http1.max_buf_size(max);
self
}

Expand All @@ -257,7 +267,7 @@ impl HttpConfig {
///
/// Default is false.
pub fn pipeline_flush(&mut self, enabled: bool) -> &mut Self {
self.inner.pipeline_flush(enabled);
self.http1.pipeline_flush(enabled);
self
}
}
4 changes: 0 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ use crate::{
};
use futures_util::future::poll_fn;
use http::Request;
use hyper::server::{
accept::Accept as HyperAccept,
conn::{AddrIncoming, AddrStream},
};
#[cfg(feature = "proxy-protocol")]
use std::time::Duration;
use std::{
Expand Down

0 comments on commit 8194652

Please sign in to comment.