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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::{future::poll_fn, io, rc::Rc};
use ntex_h2::{self as h2, client::RecvStream, client::SimpleClient, frame};
use crate::error::Error;
use crate::http::ResponseHead;
use crate::http::body::{Body, BodySize, MessageBody};
use crate::http::header::{self, HeaderMap, HeaderValue};
use crate::http::{Method, Payload, Version, h2::Payload as H2Payload};
use crate::time::{Millis, timeout_checked};
use crate::util::{ByteString, Bytes, Either, select};
use super::ClientRawRequest;
use super::error::{ClientError, ConnectError};
pub(super) async fn send_request(
client: H2Client,
req: ClientRawRequest,
body: Body,
timeout: Millis,
) -> Result<(ResponseHead, Payload), ClientError> {
log::trace!(
"{}: Sending client request: {req:?} {:?}",
client.client.tag(),
body.size()
);
let length = body.size();
let eof = if req.head.method == Method::HEAD {
true
} else {
matches!(
length,
BodySize::None | BodySize::Empty | BodySize::Sized(0)
)
};
// merging headers from head and extra headers.
let empty = HeaderMap::new();
let extra_headers = req.headers.as_ref().unwrap_or(&empty);
let mut hdrs: HeaderMap = req
.head
.headers
.iter()
.filter(|(name, _)| {
// h2 does not user connection headers
!(matches!(*name, &header::CONNECTION | &header::TRANSFER_ENCODING)
|| extra_headers.contains_key(*name))
})
.chain(extra_headers.iter())
.collect();
// Content length
match length {
BodySize::None | BodySize::Stream => (),
BodySize::Empty => {
hdrs.insert(header::CONTENT_LENGTH, HeaderValue::from_static("0"));
}
BodySize::Sized(len) => hdrs.insert(
header::CONTENT_LENGTH,
HeaderValue::try_from(format!("{len}")).unwrap(),
),
}
// send request
let uri = &req.head.uri;
let path = uri.path_and_query().map_or_else(
|| ByteString::from(uri.path()),
|p| ByteString::from(format!("{p}")),
);
let (snd_stream, rcv_stream) = client
.client
.send(req.head.method.clone(), path, hdrs, eof)
.await
.map_err(Error::into_error)?;
// send body
if !eof {
// sending body is async process, we can handle upload and download
// at the same time
crate::rt::spawn(async move {
if let Err(e) = send_body(body, &snd_stream).await {
log::error!("{}: Cannot send body: {e:?}", snd_stream.tag());
snd_stream.reset(frame::Reason::INTERNAL_ERROR);
}
});
}
timeout_checked(timeout, get_response(rcv_stream))
.await
.map_err(|()| ClientError::Timeout)
.and_then(|res| res)
}
async fn get_response(
rcv_stream: RecvStream,
) -> Result<(ResponseHead, Payload), ClientError> {
let h2::Message { stream, kind } = rcv_stream
.recv()
.await
.ok_or(ClientError::Connect(ConnectError::Disconnected(None)))?;
match kind {
h2::MessageKind::Headers {
pseudo,
headers,
eof,
} => {
log::trace!(
"{}: {:?} got response (eof: {eof}): {pseudo:#?}\nheaders: {headers:#?}",
stream.tag(),
stream.id(),
);
match pseudo.status {
Some(status) => {
let mut head = ResponseHead::new(status);
head.headers = headers;
head.version = Version::HTTP_2;
let payload = if eof {
Payload::None
} else {
log::debug!(
"{}: Creating local payload stream for {:?}",
stream.tag(),
stream.id()
);
let (pl, payload) = H2Payload::create(stream.empty_capacity());
crate::rt::spawn(async move {
loop {
let h2::Message { stream, kind } = match select(
rcv_stream.recv(),
poll_fn(|cx| pl.on_cancel(cx.waker())),
)
.await
{
Either::Left(Some(msg)) => msg,
Either::Left(None) => {
pl.feed_eof(Bytes::new());
break;
}
Either::Right(()) => break,
};
match kind {
h2::MessageKind::Data(data, cap) => {
log::trace!(
"{}: Got data chunk for {:?}: {:?}",
stream.tag(),
stream.id(),
data.len()
);
pl.feed_data(data, cap);
}
h2::MessageKind::Eof(item) => {
log::trace!(
"{}: Got payload eof for {:?}: {item:?}",
stream.tag(),
stream.id(),
);
match item {
h2::StreamEof::Data(data) => {
pl.feed_eof(data);
}
h2::StreamEof::Trailers(_) => {
pl.feed_eof(Bytes::new());
}
h2::StreamEof::Error(err) => {
pl.set_error(err.into_error().into());
}
}
}
h2::MessageKind::Disconnect(err) => {
log::trace!(
"{}: Connection is disconnected {err:?}",
stream.tag(),
);
pl.set_error(
io::Error::new(
io::ErrorKind::UnexpectedEof,
err,
)
.into(),
);
}
h2::MessageKind::Headers { .. } => {
pl.set_error(
io::Error::new(
io::ErrorKind::Unsupported,
"unexpected h2 message",
)
.into(),
);
break;
}
}
}
});
Payload::H2(payload)
};
Ok((head, payload))
}
None => Err(ClientError::H2(h2::OperationError::Connection(
h2::ConnectionError::MissingPseudo("Status"),
))),
}
}
_ => Err(ClientError::Error(Rc::new(io::Error::new(
io::ErrorKind::Unsupported,
"unexpected h2 message",
)))),
}
}
async fn send_body(
mut body: Body,
stream: &h2::client::SendStream,
) -> Result<(), ClientError> {
loop {
match poll_fn(|cx| body.poll_next_chunk(cx)).await {
Some(Ok(b)) => {
log::trace!(
"{}: {:?} sending chunk, {} bytes",
stream.tag(),
stream.id(),
b.len()
);
stream
.send_payload(b, false)
.await
.map_err(Error::into_error)?;
}
Some(Err(e)) => return Err(e.into()),
None => {
log::trace!("{}: {:?} eof of send stream ", stream.tag(), stream.id());
stream
.send_payload(Bytes::new(), true)
.await
.map_err(Error::into_error)?;
return Ok(());
}
}
}
}
#[derive(Clone)]
pub(super) struct H2Client {
client: SimpleClient,
}
impl H2Client {
pub(super) fn new(client: SimpleClient) -> Self {
Self { client }
}
pub(super) fn tag(&self) -> &'static str {
self.client.tag()
}
pub(super) fn close(&self) {
self.client.close();
}
pub(super) fn is_closed(&self) -> bool {
self.client.is_closed()
}
}