Skip to content

Commit 6d258d7

Browse files
committed
feat(conn/auto): allow serving connection with upgrade after config
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.
1 parent 16daef6 commit 6d258d7

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/server/conn/auto.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,27 @@ impl<E> Http1Builder<'_, E> {
665665
{
666666
self.inner.serve_connection(io, service).await
667667
}
668+
669+
/// Bind a connection together with a [`Service`], with the ability to
670+
/// handle HTTP upgrades. This requires that the IO object implements
671+
/// `Send`.
672+
#[cfg(feature = "http2")]
673+
pub fn serve_connection_with_upgrades<I, S, B>(
674+
&self,
675+
io: I,
676+
service: S,
677+
) -> UpgradeableConnection<'_, I, S, E>
678+
where
679+
S: Service<Request<Incoming>, Response = Response<B>>,
680+
S::Future: 'static,
681+
S::Error: Into<Box<dyn StdError + Send + Sync>>,
682+
B: Body + 'static,
683+
B::Error: Into<Box<dyn StdError + Send + Sync>>,
684+
I: Read + Write + Unpin + Send + 'static,
685+
E: HttpServerConnExec<S::Future, B>,
686+
{
687+
self.inner.serve_connection_with_upgrades(io, service)
688+
}
668689
}
669690

670691
/// Http2 part of builder.
@@ -824,6 +845,26 @@ impl<E> Http2Builder<'_, E> {
824845
{
825846
self.inner.serve_connection(io, service).await
826847
}
848+
849+
/// Bind a connection together with a [`Service`], with the ability to
850+
/// handle HTTP upgrades. This requires that the IO object implements
851+
/// `Send`.
852+
pub fn serve_connection_with_upgrades<I, S, B>(
853+
&self,
854+
io: I,
855+
service: S,
856+
) -> UpgradeableConnection<'_, I, S, E>
857+
where
858+
S: Service<Request<Incoming>, Response = Response<B>>,
859+
S::Future: 'static,
860+
S::Error: Into<Box<dyn StdError + Send + Sync>>,
861+
B: Body + 'static,
862+
B::Error: Into<Box<dyn StdError + Send + Sync>>,
863+
I: Read + Write + Unpin + Send + 'static,
864+
E: HttpServerConnExec<S::Future, B>,
865+
{
866+
self.inner.serve_connection_with_upgrades(io, service)
867+
}
827868
}
828869

829870
#[cfg(test)]
@@ -971,7 +1012,9 @@ mod tests {
9711012
let stream = TokioIo::new(stream);
9721013
tokio::task::spawn(async move {
9731014
let _ = auto::Builder::new(TokioExecutor::new())
974-
.serve_connection(stream, service_fn(hello))
1015+
.http2()
1016+
.max_header_list_size(4096)
1017+
.serve_connection_with_upgrades(stream, service_fn(hello))
9751018
.await;
9761019
});
9771020
}

0 commit comments

Comments
 (0)