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
use std::{
    io,
    pin::Pin,
    task::{Context, Poll},
};

use async_http_codec::BodyDecodeState;
use futures::{future::FusedFuture, ready, AsyncRead, Future};

use crate::Transport;

pub use self::error::HttpError;

#[cfg(target_arch = "wasm32")]
mod request_wasm;
#[cfg(not(target_arch = "wasm32"))]
mod response_native;

#[cfg(target_arch = "wasm32")]
mod response_wasm;
#[cfg(target_arch = "wasm32")]
type ResponseReadInner = response_wasm::ResponseRead;
#[cfg(not(target_arch = "wasm32"))]
mod request_native;
#[cfg(not(target_arch = "wasm32"))]
type ResponseReadInner = response_native::ResponseRead;

mod common;
mod error;

pub struct RequestSend<'a> {
    inner: request_native::RequestSend<'a>,
}

impl RequestSend<'_> {
    pub fn new(request: &http::Request<impl AsRef<[u8]>>) -> RequestSend<'_> {
        #[cfg(target_arch = "wasm32")]
        todo!();
        #[cfg(not(target_arch = "wasm32"))]
        {
            let inner = request_native::RequestSend::new(request);
            RequestSend { inner }
        }
    }
    #[cfg(not(target_arch = "wasm32"))]
    pub fn new_with_client_config(request: &http::Request<impl AsRef<[u8]>>, client_config: std::sync::Arc<rustls::ClientConfig>) -> RequestSend<'_> {
        let inner = request_native::RequestSend::new_with_client_config(request, client_config);
        RequestSend { inner }
    }
}

impl Future for RequestSend<'_> {
    type Output = Result<http::Response<ResponseRead>, HttpError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let response = ready!(self.inner.poll(cx))?;
        Ok(response.map(|inner| ResponseRead { inner })).into()
    }
}

impl FusedFuture for RequestSend<'_> {
    fn is_terminated(&self) -> bool {
        self.inner.is_terminated()
    }
}

pub struct ResponseRead {
    inner: ResponseReadInner,
}

impl ResponseRead {
    pub(crate) fn into_inner(self) -> Result<(BodyDecodeState, Transport), HttpError> {
        self.inner.into_inner()
    }
}

impl AsyncRead for ResponseRead {
    fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
        Pin::new(&mut self.inner).poll_read(cx, buf)
    }
}