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
#![warn(missing_debug_implementations, rust_2018_idioms)]

pub mod flow;
pub mod http;
pub mod prepend_io_stream;

use futures_io::{AsyncRead, AsyncWrite};

pub use crate::http::*;
pub use flow::{HandshakeOutcome, ResponseParts};
pub use prepend_io_stream::PrependIoStream as Stream;
pub use std::io::Result;

pub async fn handshake_and_wrap<ARW>(
    mut stream: ARW,
    host: &str,
    port: u16,
    request_headers: &HeaderMap,
    read_buf: &mut [u8],
) -> Result<Outcome<Stream<ARW>>>
where
    ARW: AsyncRead + AsyncWrite + Unpin,
{
    let HandshakeOutcome {
        response_parts,
        data_after_handshake,
    } = flow::handshake(&mut stream, host, port, request_headers, read_buf).await?;

    Ok(Outcome {
        response_parts,
        stream: Stream::from_vec(stream, Some(data_after_handshake.into())),
    })
}

#[derive(Debug)]
pub struct Outcome<T> {
    pub response_parts: ResponseParts,
    pub stream: T,
}

impl<T> AsRef<T> for Outcome<T> {
    fn as_ref(&self) -> &T {
        &self.stream
    }
}