Skip to content

Commit 016f861

Browse files
committed
fix https example
1 parent 7d8c2ce commit 016f861

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

examples/https.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{path::PathBuf, sync::Arc};
33
use clap::{Args, Parser};
44
use http_mitm_proxy::{DefaultClient, MitmProxy};
55
use hyper::service::service_fn;
6-
use hyper_util::rt::TokioIo;
6+
use hyper_util::rt::{TokioExecutor, TokioIo};
77
use moka::sync::Cache;
88
use rustls::{pki_types::PrivatePkcs8KeyDer, ServerConfig};
99
use tokio::net::TcpListener;
@@ -129,13 +129,23 @@ async fn main() {
129129
);
130130

131131
let stream = tls_acceptor.accept(stream).await.unwrap();
132-
hyper::server::conn::http1::Builder::new()
133-
.preserve_header_case(true)
134-
.title_case_headers(true)
135-
.serve_connection(TokioIo::new(stream), service)
136-
.with_upgrades()
137-
.await
138-
.unwrap();
132+
133+
if stream.get_ref().1.alpn_protocol() == Some(b"h2") {
134+
// HTTP/2
135+
hyper::server::conn::http2::Builder::new(TokioExecutor::new())
136+
.serve_connection(TokioIo::new(stream), service)
137+
.await
138+
.unwrap();
139+
} else {
140+
// HTTP/1.1
141+
hyper::server::conn::http1::Builder::new()
142+
.preserve_header_case(true)
143+
.title_case_headers(true)
144+
.serve_connection(TokioIo::new(stream), service)
145+
.with_upgrades()
146+
.await
147+
.unwrap();
148+
}
139149
});
140150
}
141151
};

0 commit comments

Comments
 (0)