From 016f861ff679b5198d4c3a97b0206f7db55cb2d6 Mon Sep 17 00:00:00 2001 From: hatoo Date: Wed, 4 Dec 2024 16:23:35 +0900 Subject: [PATCH] fix https example --- examples/https.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/https.rs b/examples/https.rs index 1c61ebb..bfadd0b 100644 --- a/examples/https.rs +++ b/examples/https.rs @@ -3,7 +3,7 @@ use std::{path::PathBuf, sync::Arc}; use clap::{Args, Parser}; use http_mitm_proxy::{DefaultClient, MitmProxy}; use hyper::service::service_fn; -use hyper_util::rt::TokioIo; +use hyper_util::rt::{TokioExecutor, TokioIo}; use moka::sync::Cache; use rustls::{pki_types::PrivatePkcs8KeyDer, ServerConfig}; use tokio::net::TcpListener; @@ -129,13 +129,23 @@ async fn main() { ); let stream = tls_acceptor.accept(stream).await.unwrap(); - hyper::server::conn::http1::Builder::new() - .preserve_header_case(true) - .title_case_headers(true) - .serve_connection(TokioIo::new(stream), service) - .with_upgrades() - .await - .unwrap(); + + if stream.get_ref().1.alpn_protocol() == Some(b"h2") { + // HTTP/2 + hyper::server::conn::http2::Builder::new(TokioExecutor::new()) + .serve_connection(TokioIo::new(stream), service) + .await + .unwrap(); + } else { + // HTTP/1.1 + hyper::server::conn::http1::Builder::new() + .preserve_header_case(true) + .title_case_headers(true) + .serve_connection(TokioIo::new(stream), service) + .with_upgrades() + .await + .unwrap(); + } }); } };