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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#![forbid(future_incompatible, rust_2018_idioms)]
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, missing_doc_code_examples, unreachable_pub)]
#![cfg_attr(feature = "docs", feature(doc_cfg))]
#![cfg_attr(
not(all(feature = "wasm_client", target_arch = "wasm32")),
forbid(unsafe_code)
)]
use futures::future::BoxFuture;
use futures::io::{AsyncRead, Cursor};
use std::error::Error;
use std::fmt::{self, Debug};
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
#[cfg_attr(feature = "docs", doc(cfg(curl_client)))]
#[cfg(all(feature = "curl_client", not(target_arch = "wasm32")))]
pub mod isahc;
#[cfg_attr(feature = "docs", doc(cfg(wasm_client)))]
#[cfg(all(feature = "wasm_client", target_arch = "wasm32"))]
pub mod wasm;
#[cfg_attr(feature = "docs", doc(cfg(native_client)))]
#[cfg(feature = "native_client")]
pub mod native;
pub type Request = http::Request<Body>;
pub type Response = http::Response<Body>;
pub trait HttpClient: Debug + Unpin + Send + Sync + Clone + 'static {
type Error: Error + Send + Sync;
fn send(&self, req: Request) -> BoxFuture<'static, Result<Response, Self::Error>>;
}
pub struct Body {
reader: Box<dyn AsyncRead + Unpin + Send + 'static>,
}
impl Body {
pub fn empty() -> Self {
Self {
reader: Box::new(futures::io::empty()),
}
}
pub fn from_reader(reader: impl AsyncRead + Unpin + Send + 'static) -> Self {
Self {
reader: Box::new(reader),
}
}
}
impl AsyncRead for Body {
#[allow(missing_doc_code_examples)]
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.reader).poll_read(cx, buf)
}
}
impl fmt::Debug for Body {
#[allow(missing_doc_code_examples)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Body").field("reader", &"<hidden>").finish()
}
}
impl From<Vec<u8>> for Body {
#[allow(missing_doc_code_examples)]
#[inline]
fn from(vec: Vec<u8>) -> Body {
Self {
reader: Box::new(Cursor::new(vec)),
}
}
}
impl<R: AsyncRead + Unpin + Send + 'static> From<Box<R>> for Body {
#[allow(missing_doc_code_examples)]
fn from(reader: Box<R>) -> Self {
Self { reader }
}
}