Debian Codegraph @

crt/s2n/bindings/rust/standard/s2n-tls-hyper/src/stream.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 hyper::rt::{Read, ReadBufCursor, Write};
5 use hyper_util::{
6 client::legacy::connect::{Connected, Connection as HyperConnection},
7 rt::TokioIo,
8 };
9 use s2n_tls::connection;
10 use s2n_tls_tokio::TlsStream;
11 use std::{
12 io::Error,
13 pin::Pin,
14 task::{Context, Poll},
15 };
16
17 /// `MaybeHttpsStream` is a wrapper over a hyper TCP stream, `Transport`, allowing for TLS to be
18 /// negotiated over the TCP stream via the `Https` type. The `Http` type bypasses TLS to optionally
19 /// allow for communication with HTTP endpoints over plain TCP.
20 ///
21 /// This struct is used to implement `tower_service::Service` for `HttpsConnector`, and shouldn't
22 /// need to be used directly.
23 pub enum MaybeHttpsStream<Transport, Builder>
24 where
25 Transport: Read + Write + Unpin,
26 Builder: connection::Builder,
27 <Builder as connection::Builder>::Output: Unpin,
28 {
29 // `Transport` is the underlying hyper TCP stream, which is wrapped in a `TokioIo` type in order
30 // to make it compatible with tokio (implementing AsyncRead and AsyncWrite). This allows the TCP
31 // stream to be provided to the `s2n_tls_tokio::TlsStream`.
32 //
33 // `MaybeHttpsStream` MUST implement hyper's `Read` and `Write` traits. So, the `TlsStream` is
34 // wrapped in an additional `TokioIo` type, which already implements the conversion from hyper's
35 // traits to tokio's. This allows the `Read` and `Write` implementations for `MaybeHttpsStream`
36 // to simply call the `TokioIo` `poll` functions.
37 Https(TokioIo<TlsStream<TokioIo<Transport>, Builder::Output>>),
38 Http(Transport),
39 }
40
41 impl<Transport, Builder> HyperConnection for MaybeHttpsStream<Transport, Builder>
42 where
43 Transport: Read + Write + HyperConnection + Unpin,
44 Builder: connection::Builder,
45 <Builder as connection::Builder>::Output: Unpin,
46 {
47 fn connected(&self) -> Connected {
48 match self {
49 Self::Https(stream) => {
50 let connected = stream.inner().get_ref().connected();
51 let conn = stream.inner().as_ref();
52 match conn.application_protocol() {
53 // Inform hyper that HTTP/2 was negotiated in the ALPN.
54 Some(b"h2") => connected.negotiated_h2(),
55 _ => connected,
56 }
57 }
58 Self::Http(stream) => stream.connected(),
59 }
60 }
61 }
62
63 impl<Transport, Builder> Read for MaybeHttpsStream<Transport, Builder>
64 where
65 Transport: Read + Write + Unpin,
66 Builder: connection::Builder,
67 <Builder as connection::Builder>::Output: Unpin,
68 {
69 fn poll_read(
70 self: Pin<&mut Self>,
71 cx: &mut Context<'_>,
72 buf: ReadBufCursor<'_>,
73 ) -> Poll<Result<(), Error>> {
74 match Pin::get_mut(self) {
75 Self::Https(stream) => Pin::new(stream).poll_read(cx, buf),
76 Self::Http(stream) => Pin::new(stream).poll_read(cx, buf),
77 }
78 }
79 }
80
81 impl<Transport, Builder> Write for MaybeHttpsStream<Transport, Builder>
82 where
83 Transport: Read + Write + Unpin,
84 Builder: connection::Builder,
85 <Builder as connection::Builder>::Output: Unpin,
86 {
87 fn poll_write(
88 self: Pin<&mut Self>,
89 cx: &mut Context<'_>,
90 buf: &[u8],
91 ) -> Poll<Result<usize, Error>> {
92 match Pin::get_mut(self) {
93 Self::Https(stream) => Pin::new(stream).poll_write(cx, buf),
94 Self::Http(stream) => Pin::new(stream).poll_write(cx, buf),
95 }
96 }
97
98 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
99 match Pin::get_mut(self) {
100 Self::Https(stream) => Pin::new(stream).poll_flush(cx),
101 Self::Http(stream) => Pin::new(stream).poll_flush(cx),
102 }
103 }
104
105 fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
106 match Pin::get_mut(self) {
107 Self::Https(stream) => Pin::new(stream).poll_shutdown(cx),
108 Self::Http(stream) => Pin::new(stream).poll_shutdown(cx),
109 }
110 }
111 }