File: echo.rs

package info (click to toggle)
aws-crt-python 0.28.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,428 kB
  • sloc: ansic: 437,955; python: 27,657; makefile: 5,855; sh: 4,289; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (85 lines) | stat: -rw-r--r-- 3,181 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use bytes::Bytes;
use http::{Request, Response};
use http_body_util::{combinators::BoxBody, BodyExt};
use hyper::service::service_fn;
use hyper_util::rt::{TokioExecutor, TokioIo};
use s2n_tls::connection::Builder;
use s2n_tls_tokio::TlsAcceptor;
use std::{error::Error, future::Future};
use tokio::net::TcpListener;

pub async fn echo(
    req: Request<hyper::body::Incoming>,
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
    Ok(Response::new(req.into_body().boxed()))
}

pub async fn serve_echo<B>(
    tcp_listener: TcpListener,
    builder: B,
) -> Result<(), Box<dyn Error + Send + Sync>>
where
    B: Builder,
    <B as Builder>::Output: Unpin + Send + Sync + 'static,
{
    let (tcp_stream, _) = tcp_listener.accept().await?;
    let acceptor = TlsAcceptor::new(builder);
    let tls_stream = acceptor.accept(tcp_stream).await?;
    let io = TokioIo::new(tls_stream);

    let server = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new());
    if let Err(err) = server.serve_connection(io, service_fn(echo)).await {
        // The hyper client doesn't gracefully terminate by waiting for the server's shutdown.
        // Instead, the client sends its shutdown and then immediately closes the socket. This can
        // cause a NotConnected error to be emitted when the server attempts to send its shutdown.
        //
        // For now, NotConnected errors are ignored. After the hyper client can be configured to
        // gracefully shutdown, this exception can be removed:
        // https://github.com/aws/s2n-tls/issues/4855
        //
        // Also, it's possible that a NotConnected error could occur during some operation other
        // than a shutdown. Ideally, these NotConnected errors wouldn't be ignored. However, it's
        // not currently possible to distinguish between shutdown vs non-shutdown errors:
        // https://github.com/aws/s2n-tls/issues/4856
        if let Some(hyper_err) = err.downcast_ref::<hyper::Error>() {
            if let Some(source) = hyper_err.source() {
                if let Some(io_err) = source.downcast_ref::<tokio::io::Error>() {
                    if io_err.kind() == tokio::io::ErrorKind::NotConnected {
                        return Ok(());
                    }
                }
            }
        }

        return Err(err);
    }

    Ok(())
}

pub async fn make_echo_request<B, F, Fut>(
    server_builder: B,
    send_client_request: F,
) -> Result<(), Box<dyn Error + Send + Sync>>
where
    B: Builder + Send + Sync + 'static,
    <B as Builder>::Output: Unpin + Send + Sync + 'static,
    F: FnOnce(u16) -> Fut,
    Fut: Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'static,
{
    let listener = TcpListener::bind("127.0.0.1:0").await?;
    let addr = listener.local_addr()?;

    let mut tasks = tokio::task::JoinSet::new();
    tasks.spawn(serve_echo(listener, server_builder));
    tasks.spawn(send_client_request(addr.port()));

    while let Some(res) = tasks.join_next().await {
        res.unwrap()?;
    }

    Ok(())
}