hreq 0.8.0

hreq is a user first async http client
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use crate::body_codec::BodyImpl;
use crate::body_send::BodySender;
use crate::bw::BandwidthMonitor;
use crate::head_ext::HeaderMapExt;
use crate::params::HReqParams;
use crate::uninit::UninitBuf;
use crate::uri_ext::HostPort;
use crate::uri_ext::MethodExt;
use crate::Body;
use crate::Error;
use crate::AGENT_IDENT;
use bytes::Bytes;
use futures_util::ready;
use h2;
use h2::client::SendRequest as H2SendRequest;
use hreq_h1 as h1;
use hreq_h1::client::SendRequest as H1SendRequest;
use once_cell::sync::Lazy;
use std::fmt;
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;

static ID_COUNTER: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));
const START_BUF_SIZE: usize = 16_384;
const MAX_BUF_SIZE: usize = 2 * 1024 * 1024;

// #[derive(Clone)]
pub struct Connection {
    id: usize,
    host_port: HostPort,
    inner: Inner,
    unfinished_reqs: Arc<()>,
    bw: Option<BandwidthMonitor>,
}

enum Inner {
    H1(H1SendRequest),
    H2(H2SendRequest<Bytes>),
}

impl Connection {
    pub(crate) fn new_h1(host_port: HostPort, conn: H1SendRequest) -> Self {
        Self::new(host_port, Inner::H1(conn), None)
    }

    pub(crate) fn new_h2(
        host_port: HostPort,
        conn: H2SendRequest<Bytes>,
        bw: BandwidthMonitor,
    ) -> Self {
        Self::new(host_port, Inner::H2(conn), Some(bw))
    }

    fn new(host_port: HostPort, inner: Inner, bw: Option<BandwidthMonitor>) -> Self {
        Connection {
            id: ID_COUNTER.fetch_add(1, Ordering::Relaxed),
            host_port,
            inner: inner,
            unfinished_reqs: Arc::new(()),
            bw,
        }
    }

    pub(crate) fn id(&self) -> usize {
        self.id
    }

    pub(crate) fn host_port(&self) -> &HostPort {
        &self.host_port
    }

    pub(crate) fn is_http2(&self) -> bool {
        match self.inner {
            Inner::H1(_) => false,
            Inner::H2(_) => true,
        }
    }

    pub(crate) fn unfinished_requests(&self) -> usize {
        Arc::strong_count(&self.unfinished_reqs) - 1 // -1 for self
    }

    pub async fn send_request(
        &mut self,
        req: http::Request<Body>,
        body_buffer: &mut BodyBuf,
    ) -> Result<http::Response<Body>, Error> {
        // up the arc-counter on unfinished reqs
        let unfin = self.unfinished_reqs.clone();

        let (mut parts, mut body) = req.into_parts();

        let params = parts.extensions.get::<HReqParams>().unwrap();
        let deadline = params.deadline();

        // resolve deferred body codecs because content-encoding and content-type are settled.
        if body.is_configurable() {
            body.configure(&params, &parts.headers, false);

            // for small request bodies we try to fully buffer the incoming data.
            if params.prebuffer {
                body.attempt_prebuffer().await?;
            }
        }

        configure_request(&mut parts, &body, self.is_http2());

        let req = http::Request::from_parts(parts, body);

        trace!(
            "{} {} {} {} {:?}",
            self.inner,
            self.host_port(),
            req.method(),
            req.uri(),
            req.headers()
        );

        // every request gets their own copy to track received bytes
        let bw = self.bw.clone();

        // send request against a deadline
        let response = deadline
            .race(send_req(req, body_buffer, &self.inner, unfin, bw))
            .await?;

        Ok(response)
    }
}

/// Ensure correct content-length, transfer-encoding, user-agent, accept and content-type headers.
pub(crate) fn configure_request(parts: &mut http::request::Parts, body: &Body, is_http2: bool) {
    if let Some(len) = body.content_encoded_length() {
        // the body indicates a length (for sure).
        // we don't want to set content-length: 0 unless we know it's
        // a method that really has a body. also we never override
        // a user set content-length header.
        let user_set_length = parts.headers.get("content-length").is_some();

        if !user_set_length && (len > 0 || parts.method.indicates_body()) {
            parts.headers.set("content-length", len.to_string());
        }
    } else if !is_http2 && body.is_definitely_a_body() {
        // body does not indicate a length (like from a reader),
        // but there definitely is a body.
        if parts.headers.get("transfer-encoding").is_none() {
            parts.headers.set("transfer-encoding", "chunked");
        }
    }

    if parts.headers.get("user-agent").is_none() {
        parts.headers.set("user-agent", &*AGENT_IDENT);
    }

    if parts.headers.get("accept").is_none() {
        parts.headers.set("accept", "*/*");
    }

    if parts.headers.get("content-type").is_none() {
        if let Some(ctype) = body.content_type() {
            parts.headers.set("content-type", ctype);
        }
    }
}

async fn send_req(
    req: http::Request<Body>,
    body_buffer: &mut BodyBuf,
    proto: &Inner,
    unfin: Arc<()>,
    bw: Option<BandwidthMonitor>,
) -> Result<http::Response<Body>, Error> {
    let params = req.extensions().get::<HReqParams>().unwrap().clone();

    let (parts, mut body_read) = req.into_parts();
    let req = http::Request::from_parts(parts, ());

    let no_body = body_read.is_definitely_no_body() && body_buffer.len() == 0;

    let (mut res_fut, mut body_send) = proto.do_send(req, no_body).await?;
    let mut early_response = None;

    // this buffer should probably be less than h2 window size
    let mut buf = UninitBuf::with_capacity(START_BUF_SIZE, MAX_BUF_SIZE);

    if !no_body {
        let mut use_body_buf = true;

        loop {
            buf.clear();

            match TryOnceFuture(&mut res_fut).await {
                TryOnce::Pending => {
                    // early response did not happen, keep sending body
                }
                TryOnce::Ready(v) => {
                    // TODO: For now we assume an early response means aborting the
                    // body sending. This is not true for expect 100-continue.
                    early_response = Some(v);
                    break;
                }
            }

            let mut amount_read = 0;

            // use buffered body (from a potential earlier 307/308 redirect)
            if use_body_buf {
                let n = buf.read_from_sync(body_buffer)?;
                if n == 0 {
                    // no more buffer to use
                    use_body_buf = false;
                } else {
                    amount_read = n;
                }
            }

            // read new body data
            if !use_body_buf {
                let n = buf.read_from_async(&mut body_read).await?;

                // Append read data to the body_buffer in case of 307/308 redirect.
                // The body_buffer might be inert and no bytes are retained.break
                //
                // TODO: For bodies constructed from String, Vec, File etc, there is
                // no need to retain the bytes in a buffer. We should make something in
                // Body that allows us to reset it back to starting position when possible.
                body_buffer.append(&buf[..n]);

                amount_read = n;
            }

            if amount_read == 0 {
                break;
            }

            // Ship it to they underlying http1.1/http2 layer.
            body_send.send_data(&buf[0..amount_read]).await?;
        }

        // pass the body back with the buffer
        body_buffer.return_body = Some(body_read);

        body_send.send_end().await?;
    }

    let (mut parts, mut res_body) = if let Some(res) = early_response {
        res?
    } else {
        res_fut.await?
    };

    parts.extensions.insert(params.clone());
    res_body.set_unfinished_recs(unfin);
    res_body.set_bw_monitor(bw);
    res_body.configure(&params, &parts.headers, true);

    Ok(http::Response::from_parts(parts, res_body))
}

impl Inner {
    // Generalised sending of request
    async fn do_send(
        &self,
        req: http::Request<()>,
        no_body: bool,
    ) -> Result<(ResponseFuture, BodySender), Error> {
        Ok(match self {
            Inner::H1(h1) => {
                let mut h1 = h1.clone();
                let (fut, send_body) = h1.send_request(req, no_body)?;
                (ResponseFuture::H1(fut), BodySender::H1(send_body))
            }
            Inner::H2(h2) => {
                let mut h2 = h2.clone().ready().await?;
                let (fut, send_body) = h2.send_request(req, no_body)?;
                (ResponseFuture::H2(fut), BodySender::H2(send_body))
            }
        })
    }
}

/// Generalisation over response future
enum ResponseFuture {
    H1(h1::client::ResponseFuture),
    H2(h2::client::ResponseFuture),
}

impl Future for ResponseFuture {
    type Output = Result<(http::response::Parts, Body), Error>;
    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let this = self.get_mut();
        match this {
            ResponseFuture::H1(f) => {
                let (p, b) = ready!(Pin::new(f).poll(cx))?.into_parts();
                let b = Body::new(BodyImpl::Http1(b), None, false);
                Ok((p, b)).into()
            }
            ResponseFuture::H2(f) => {
                let (p, b) = ready!(Pin::new(f).poll(cx))?.into_parts();
                let b = Body::new(BodyImpl::Http2(b), None, false);
                Ok((p, b)).into()
            }
        }
    }
}

/// When polling the wrapped future will never return Poll::Pending, but instead
/// TryOnce::Pending. This is useful in an `async fn` where we don't have access
/// to the Context to do a manual poll.
struct TryOnceFuture<F>(F);

impl<F> Future for TryOnceFuture<F>
where
    Self: Unpin,
    F: Future + Unpin,
{
    type Output = TryOnce<F>;
    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let this = self.get_mut();
        match Pin::new(&mut this.0).poll(cx) {
            Poll::Pending => TryOnce::Pending,
            Poll::Ready(v) => TryOnce::Ready(v),
        }
        .into()
    }
}

enum TryOnce<F>
where
    F: Future,
{
    Pending,
    Ready(F::Output),
}

/// Body buffer, used to retain a sent body for cases where we want to handle 307/308 redirects.
/// The buffer is always present, but might be inert if the internal vec is `None`.
pub struct BodyBuf {
    vec: Option<Vec<u8>>,
    read_idx: usize,
    // Hack to allow us passing the original body back to the agent.
    //
    // TODO can we find some more elegant way of passing this back?
    return_body: Option<Body>,
}

impl BodyBuf {
    pub fn new(size: usize) -> BodyBuf {
        let vec = if size == 0 {
            None
        } else {
            Some(Vec::with_capacity(size))
        };
        BodyBuf {
            vec,
            read_idx: 0,
            return_body: None,
        }
    }

    /// Reset the body buffer back to 0 optionally retaining the data that has been appended.
    ///
    /// NB: Returning a Option<Body> here is a hack that allows us to pass the original body
    /// back to the Agent in case we need it for the next request.
    pub fn reset(&mut self, keep_data: bool) -> Option<Body> {
        trace!(
            "BodyBuf reset keep_data: {}, len: {}",
            keep_data,
            self.len()
        );
        self.read_idx = 0;
        if keep_data {
            self.return_body.take()
        } else {
            if let Some(vec) = &mut self.vec {
                vec.resize(0, 0);
            }
            self.return_body = None;
            None
        }
    }

    /// Append more data to this buffer. If the amount of data to append is more than the
    /// buffer capacity, the buffer is cleared and no data is retained anymore.
    fn append(&mut self, buf: &[u8]) {
        if let Some(vec) = &mut self.vec {
            let remaining = vec.capacity() - vec.len();
            if buf.len() > remaining {
                self.vec = None;
                debug!("No capacity left in BodyBuf");
                return;
            }
            vec.extend_from_slice(buf);
            trace!("BodyBuf appended: {}/{}", vec.len(), vec.capacity());
        }
    }

    /// Current amount of retained bytes.
    fn len(&self) -> usize {
        self.vec.as_ref().map(|v| v.len()).unwrap_or(0)
    }
}

impl io::Read for BodyBuf {
    /// Read from this buffer without dropping any data.
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        Ok(if let Some(vec) = &mut self.vec {
            let amt = (&vec[self.read_idx..]).read(buf).unwrap();

            self.read_idx += amt;

            amt
        } else {
            0
        })
    }
}

impl fmt::Display for Inner {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Inner::H1(_) => write!(f, "Http1"),
            Inner::H2(_) => write!(f, "Http2"),
        }
    }
}

impl PartialEq for Connection {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}
impl Eq for Connection {}