Skip to content

Commit dfd8c22

Browse files
committed
enables incoming http2 stream
Signed-off-by: Aminu Oluwaseun Joshua <[email protected]>
1 parent 04a3e8e commit dfd8c22

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

crates/trigger-http/src/server.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use http::{
1414
use http_body_util::BodyExt;
1515
use hyper::{
1616
body::{Bytes, Incoming},
17-
server::conn::http1,
17+
server::conn::{http1, http2},
1818
service::service_fn,
1919
};
20-
use hyper_util::rt::TokioIo;
20+
use hyper_util::rt::{TokioExecutor, TokioIo};
2121
use spin_app::{APP_DESCRIPTION_KEY, APP_NAME_KEY};
2222
use spin_factor_outbound_http::{OutboundHttpFactor, SelfRequestOrigin};
2323
use spin_factors::RuntimeFactors;
@@ -206,7 +206,7 @@ impl<F: RuntimeFactors> HttpServer<F> {
206206
loop {
207207
let (stream, client_addr) = listener.accept().await?;
208208
self.clone()
209-
.serve_connection(stream, Scheme::HTTP, client_addr);
209+
.http1_serve_connection(stream, Scheme::HTTP, client_addr);
210210
}
211211
}
212212

@@ -220,9 +220,15 @@ impl<F: RuntimeFactors> HttpServer<F> {
220220
loop {
221221
let (stream, client_addr) = listener.accept().await?;
222222
match acceptor.accept(stream).await {
223-
Ok(stream) => self
224-
.clone()
225-
.serve_connection(stream, Scheme::HTTPS, client_addr),
223+
Ok(stream) => {
224+
if stream.get_ref().1.alpn_protocol() == Some(b"h2") {
225+
self.clone()
226+
.http2_serve_connection(stream, Scheme::HTTPS, client_addr)
227+
} else {
228+
self.clone()
229+
.http1_serve_connection(stream, Scheme::HTTPS, client_addr)
230+
}
231+
}
226232
Err(err) => tracing::error!(?err, "Failed to start TLS session"),
227233
}
228234
}
@@ -403,7 +409,7 @@ impl<F: RuntimeFactors> HttpServer<F> {
403409
.body(body::empty())?)
404410
}
405411

406-
fn serve_connection<S: AsyncRead + AsyncWrite + Unpin + Send + 'static>(
412+
fn http1_serve_connection<S: AsyncRead + AsyncWrite + Unpin + Send + 'static>(
407413
self: Arc<Self>,
408414
stream: S,
409415
server_scheme: Scheme,
@@ -424,7 +430,32 @@ impl<F: RuntimeFactors> HttpServer<F> {
424430
)
425431
.await
426432
{
427-
tracing::warn!("Error serving HTTP connection: {err:?}");
433+
tracing::warn!("Error serving HTTP1 connection: {err:?}");
434+
}
435+
});
436+
}
437+
438+
fn http2_serve_connection<S: AsyncRead + AsyncWrite + Unpin + Send + 'static>(
439+
self: Arc<Self>,
440+
stream: S,
441+
server_scheme: Scheme,
442+
client_addr: SocketAddr,
443+
) {
444+
task::spawn(async move {
445+
if let Err(err) = http2::Builder::new(TokioExecutor::new())
446+
.serve_connection(
447+
TokioIo::new(stream),
448+
service_fn(move |request| {
449+
self.clone().instrumented_service_fn(
450+
server_scheme.clone(),
451+
client_addr,
452+
request,
453+
)
454+
}),
455+
)
456+
.await
457+
{
458+
tracing::warn!("Error serving HTTP2 connection: {err:?}");
428459
}
429460
});
430461
}

0 commit comments

Comments
 (0)