| 1 |
|
| 2 |
|
| 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 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 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 |
} |