Skip to content

Commit

Permalink
feat(auto): allow serving connection with upgrades after config (#113)
Browse files Browse the repository at this point in the history
When using hyper_util::server::conn::auto::Builder, if one wants to set
http1 or http2 options one has to specialize the struct into
Http1Builder or Http2Builder with .http1()/.http2() methods.

However, once the struct has been specialized there is no way to go back
to the inner Builder to call serve_connection_with_upgrades(): one would
need to either add make inner public, add an inner() method to get it
back, or add a stub that just calls the method on inner like we have
with serve_connection().

Since we already had one for serve_connection(), add an indentical one for
serve_connection_with_upgrades()
Note that it does not make sense for serve_connection_with_upgrades() to
be called without the http feature (I think?), so it has only been implemented
for http1 if the http2 feature is set.
  • Loading branch information
martinetd committed Apr 15, 2024
1 parent 7bae87f commit f87fe0d
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/server/conn/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,27 @@ impl<E> Http1Builder<'_, E> {
{
self.inner.serve_connection(io, service).await
}

/// Bind a connection together with a [`Service`], with the ability to
/// handle HTTP upgrades. This requires that the IO object implements
/// `Send`.
#[cfg(feature = "http2")]
pub fn serve_connection_with_upgrades<I, S, B>(
&self,
io: I,
service: S,
) -> UpgradeableConnection<'_, I, S, E>
where
S: Service<Request<Incoming>, Response = Response<B>>,
S::Future: 'static,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Body + 'static,
B::Error: Into<Box<dyn StdError + Send + Sync>>,
I: Read + Write + Unpin + Send + 'static,
E: HttpServerConnExec<S::Future, B>,
{
self.inner.serve_connection_with_upgrades(io, service)
}
}

/// Http2 part of builder.
Expand Down Expand Up @@ -824,6 +845,26 @@ impl<E> Http2Builder<'_, E> {
{
self.inner.serve_connection(io, service).await
}

/// Bind a connection together with a [`Service`], with the ability to
/// handle HTTP upgrades. This requires that the IO object implements
/// `Send`.
pub fn serve_connection_with_upgrades<I, S, B>(
&self,
io: I,
service: S,
) -> UpgradeableConnection<'_, I, S, E>
where
S: Service<Request<Incoming>, Response = Response<B>>,
S::Future: 'static,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Body + 'static,
B::Error: Into<Box<dyn StdError + Send + Sync>>,
I: Read + Write + Unpin + Send + 'static,
E: HttpServerConnExec<S::Future, B>,
{
self.inner.serve_connection_with_upgrades(io, service)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -971,7 +1012,9 @@ mod tests {
let stream = TokioIo::new(stream);
tokio::task::spawn(async move {
let _ = auto::Builder::new(TokioExecutor::new())
.serve_connection(stream, service_fn(hello))
.http2()
.max_header_list_size(4096)
.serve_connection_with_upgrades(stream, service_fn(hello))
.await;
});
}
Expand Down

0 comments on commit f87fe0d

Please sign in to comment.