Debian Codegraph @

crt/s2n/bindings/rust/standard/s2n-tls-hyper/tests/common/echo.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use bytes::Bytes;
5 use http::{Request, Response};
6 use http_body_util::{combinators::BoxBody, BodyExt};
7 use hyper::service::service_fn;
8 use hyper_util::rt::{TokioExecutor, TokioIo};
9 use s2n_tls::connection::Builder;
10 use s2n_tls_tokio::TlsAcceptor;
11 use std::{error::Error, future::Future};
12 use tokio::net::TcpListener;
13
14 pub async fn echo(
15 req: Request<hyper::body::Incoming>,
16 ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
17 Ok(Response::new(req.into_body().boxed()))
18 }
19
20 pub async fn serve_echo<B>(
21 tcp_listener: TcpListener,
22 builder: B,
23 ) -> Result<(), Box<dyn Error + Send + Sync>>
24 where
25 B: Builder,
26 <B as Builder>::Output: Unpin + Send + Sync + 'static,
27 {
28 let (tcp_stream, _) = tcp_listener.accept().await?;
29 let acceptor = TlsAcceptor::new(builder);
30 let tls_stream = acceptor.accept(tcp_stream).await?;
31 let io = TokioIo::new(tls_stream);
32
33 let server = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new());
34 if let Err(err) = server.serve_connection(io, service_fn(echo)).await {
35 // The hyper client doesn't gracefully terminate by waiting for the server's shutdown.
36 // Instead, the client sends its shutdown and then immediately closes the socket. This can
37 // cause a NotConnected error to be emitted when the server attempts to send its shutdown.
38 //
39 // For now, NotConnected errors are ignored. After the hyper client can be configured to
40 // gracefully shutdown, this exception can be removed:
41 // https://github.com/aws/s2n-tls/issues/4855
42 //
43 // Also, it's possible that a NotConnected error could occur during some operation other
44 // than a shutdown. Ideally, these NotConnected errors wouldn't be ignored. However, it's
45 // not currently possible to distinguish between shutdown vs non-shutdown errors:
46 // https://github.com/aws/s2n-tls/issues/4856
47 if let Some(hyper_err) = err.downcast_ref::<hyper::Error>() {
48 if let Some(source) = hyper_err.source() {
49 if let Some(io_err) = source.downcast_ref::<tokio::io::Error>() {
50 if io_err.kind() == tokio::io::ErrorKind::NotConnected {
51 return Ok(());
52 }
53 }
54 }
55 }
56
57 return Err(err);
58 }
59
60 Ok(())
61 }
62
63 pub async fn make_echo_request<B, F, Fut>(
64 server_builder: B,
65 send_client_request: F,
66 ) -> Result<(), Box<dyn Error + Send + Sync>>
67 where
68 B: Builder + Send + Sync + 'static,
69 <B as Builder>::Output: Unpin + Send + Sync + 'static,
70 F: FnOnce(u16) -> Fut,
71 Fut: Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'static,
72 {
73 let listener = TcpListener::bind("127.0.0.1:0").await?;
74 let addr = listener.local_addr()?;
75
76 let mut tasks = tokio::task::JoinSet::new();
77 tasks.spawn(serve_echo(listener, server_builder));
78 tasks.spawn(send_client_request(addr.port()));
79
80 while let Some(res) = tasks.join_next().await {
81 res.unwrap()?;
82 }
83
84 Ok(())
85 }