hpx 2.4.9

High Performance 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
#![allow(clippy::items_after_test_module)]

use std::{
    fmt,
    pin::Pin,
    task::{Context, Poll, ready},
};

use bytes::Bytes;
use http_body::{Body as HttpBody, SizeHint};
use http_body_util::{BodyExt, combinators::BoxBody};
use pin_project_lite::pin_project;
#[cfg(feature = "stream")]
use {tokio::fs::File, tokio_util::io::ReaderStream};

use super::http::InnerResponseBody;
use crate::error::{BoxError, Error};

pin_project! {
    /// An request body.
    pub struct Body {
        #[pin]
        inner: BodyInner,
    }
}

pin_project! {
    /// A stable, erased response body type for client middleware boundaries.
    pub struct ClientResponseBody {
        #[pin]
        inner: ClientResponseBodyInner,
    }
}

pin_project! {
    #[project = BodyInnerProj]
    enum BodyInner {
        Reusable {
            bytes: Bytes,
        },
        Client {
            #[pin]
            body: Pin<Box<ClientResponseBody>>,
        },
        Boxed {
            #[pin]
            body: BoxBody<Bytes, BoxError>,
        },
    }
}

pin_project! {
    #[project = ClientResponseBodyInnerProj]
    enum ClientResponseBodyInner {
        Reusable {
            bytes: Bytes,
        },
        Inner {
            #[pin]
            body: InnerResponseBody,
        },
        Boxed {
            #[pin]
            body: BoxBody<Bytes, BoxError>,
        },
    }
}

pin_project! {
    /// We can't use `map_frame()` because that loses the hint data (for good reason).
    /// But we aren't transforming the data.
    struct IntoBytesBody<B> {
        #[pin]
        inner: B,
    }
}

// ===== impl Body =====

impl Body {
    /// Returns a reference to the internal data of the `Body`.
    ///
    /// `None` is returned, if the underlying data is a stream.
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match &self.inner {
            BodyInner::Reusable { bytes } => Some(bytes.as_ref()),
            BodyInner::Client { body } => body.as_ref().get_ref().as_bytes(),
            BodyInner::Boxed { .. } => None,
        }
    }

    /// Wrap a [`HttpBody`] in a box inside `Body`.
    ///
    /// # Example
    ///
    /// ```
    /// # use hpx::Body;
    /// # use futures_util;
    /// # fn main() {
    /// let content = "hello,world!".to_string();
    ///
    /// let body = Body::wrap(content);
    /// # }
    /// ```
    pub fn wrap<B>(inner: B) -> Body
    where
        B: HttpBody + Send + Sync + 'static,
        B::Data: Into<Bytes>,
        B::Error: Into<BoxError>,
    {
        Body::boxed(IntoBytesBody { inner }.map_err(Into::into).boxed())
    }

    /// Wrap a futures `Stream` in a box inside `Body`.
    ///
    /// # Example
    ///
    /// ```
    /// # use hpx::Body;
    /// # use futures_util;
    /// # fn main() {
    /// let chunks: Vec<Result<_, ::std::io::Error>> = vec![Ok("hello"), Ok(" "), Ok("world")];
    ///
    /// let stream = futures_util::stream::iter(chunks);
    ///
    /// let body = Body::wrap_stream(stream);
    /// # }
    /// ```
    ///
    /// # Optional
    ///
    /// This requires the `stream` feature to be enabled.
    #[cfg(feature = "stream")]
    #[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
    pub fn wrap_stream<S>(stream: S) -> Body
    where
        S: futures_util::stream::TryStream + Send + 'static,
        S::Error: Into<BoxError>,
        Bytes: From<S::Ok>,
    {
        Body::stream(stream)
    }

    #[cfg(any(feature = "stream", feature = "multipart"))]
    pub(crate) fn stream<S>(stream: S) -> Body
    where
        S: futures_util::stream::TryStream + Send + 'static,
        S::Error: Into<BoxError>,
        Bytes: From<S::Ok>,
    {
        use futures_util::TryStreamExt;
        use http_body::Frame;
        use http_body_util::StreamBody;
        use sync_wrapper::SyncStream;

        let body = StreamBody::new(SyncStream::new(
            stream
                .map_ok(Bytes::from)
                .map_ok(Frame::data)
                .map_err(Into::into),
        ));
        Body::boxed(body.boxed())
    }

    #[inline]
    pub(crate) fn empty() -> Body {
        Body::reusable(Bytes::new())
    }

    #[inline]
    pub(crate) fn reusable(chunk: Bytes) -> Body {
        Body {
            inner: BodyInner::Reusable { bytes: chunk },
        }
    }

    #[inline]
    fn boxed(body: BoxBody<Bytes, BoxError>) -> Body {
        Body {
            inner: BodyInner::Boxed { body },
        }
    }

    #[cfg(feature = "multipart")]
    pub(crate) fn content_length(&self) -> Option<u64> {
        match &self.inner {
            BodyInner::Reusable { bytes } => Some(bytes.len() as u64),
            BodyInner::Client { body } => body.size_hint().exact(),
            BodyInner::Boxed { body } => body.size_hint().exact(),
        }
    }

    pub(crate) fn try_clone(&self) -> Option<Body> {
        match &self.inner {
            BodyInner::Reusable { bytes } => Some(Body::reusable(bytes.clone())),
            BodyInner::Client { body } => body.as_ref().get_ref().try_clone().map(Body::from),
            BodyInner::Boxed { .. } => None,
        }
    }

    /// Convert the body into a mutable [`bytes::BytesMut`] if possible.
    ///
    /// This is useful for zero-copy operations where you need a mutable buffer,
    /// such as with SIMD JSON parsing.
    ///
    /// Returns `Some(BytesMut)` if the body is a reusable `Bytes` chunk,
    /// `None` if the body is a streaming body.
    ///
    /// # Example
    ///
    /// ```
    /// use bytes::BytesMut;
    /// use hpx::Body;
    ///
    /// let body = Body::from("hello world");
    /// if let Some(bytes_mut) = body.into_bytes_mut() {
    ///     // Use mutable bytes for zero-copy operations
    ///     println!("Got {} bytes", bytes_mut.len());
    /// }
    /// ```
    pub fn into_bytes_mut(self) -> Option<bytes::BytesMut> {
        match self.inner {
            BodyInner::Reusable { bytes } => {
                // Try to convert without copying if possible
                match bytes.try_into_mut() {
                    Ok(bytes_mut) => Some(bytes_mut),
                    Err(bytes) => Some(bytes::BytesMut::from(bytes.as_ref())),
                }
            }
            BodyInner::Client { .. } => None,
            BodyInner::Boxed { .. } => None,
        }
    }

    /// Get an [`tokio::io::AsyncRead`] adapter for the body.
    ///
    /// This enables efficient streaming of the body using standard async I/O traits.
    ///
    /// # Example
    ///
    /// ```
    /// use hpx::Body;
    /// use tokio::io::AsyncReadExt;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let body = Body::from("hello world");
    /// let mut reader = body.reader();
    ///
    /// let mut buffer = Vec::new();
    /// reader.read_to_end(&mut buffer).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Optional
    ///
    /// This requires the optional `stream` feature to be enabled.
    #[cfg(feature = "stream")]
    #[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
    pub fn reader(self) -> impl tokio::io::AsyncRead {
        use futures_util::TryStreamExt;

        tokio_util::io::StreamReader::new(
            http_body_util::BodyDataStream::new(self).map_err(std::io::Error::other),
        )
    }
}

impl Default for Body {
    #[inline]
    fn default() -> Body {
        Body::empty()
    }
}

impl fmt::Debug for Body {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.inner {
            BodyInner::Reusable { bytes } => f.debug_tuple("Body").field(bytes).finish(),
            BodyInner::Client { .. } => f.write_str("Body(ClientResponseBody(..))"),
            BodyInner::Boxed { .. } => f.write_str("Body(..)"),
        }
    }
}

impl ClientResponseBody {
    /// Wrap any compatible HTTP body into the stable client response body type.
    pub fn wrap<B>(inner: B) -> Self
    where
        B: HttpBody + Send + Sync + 'static,
        B::Data: Into<Bytes>,
        B::Error: Into<BoxError>,
    {
        Self::boxed(IntoBytesBody { inner }.map_err(Into::into).boxed())
    }

    #[inline]
    pub(crate) fn from_inner_response(body: InnerResponseBody) -> Self {
        Self {
            inner: ClientResponseBodyInner::Inner { body },
        }
    }

    #[inline]
    fn boxed(body: BoxBody<Bytes, BoxError>) -> Self {
        Self {
            inner: ClientResponseBodyInner::Boxed { body },
        }
    }

    #[inline]
    fn reusable(bytes: Bytes) -> Self {
        Self {
            inner: ClientResponseBodyInner::Reusable { bytes },
        }
    }

    #[inline]
    fn as_bytes(&self) -> Option<&[u8]> {
        match &self.inner {
            ClientResponseBodyInner::Reusable { bytes } => Some(bytes.as_ref()),
            ClientResponseBodyInner::Inner { .. } | ClientResponseBodyInner::Boxed { .. } => None,
        }
    }

    #[inline]
    fn into_bytes_mut(self) -> Option<bytes::BytesMut> {
        match self.inner {
            ClientResponseBodyInner::Reusable { bytes } => match bytes.try_into_mut() {
                Ok(bytes_mut) => Some(bytes_mut),
                Err(bytes) => Some(bytes::BytesMut::from(bytes.as_ref())),
            },
            ClientResponseBodyInner::Inner { .. } | ClientResponseBodyInner::Boxed { .. } => None,
        }
    }

    #[inline]
    fn try_clone(&self) -> Option<Self> {
        match &self.inner {
            ClientResponseBodyInner::Reusable { bytes } => Some(Self::reusable(bytes.clone())),
            ClientResponseBodyInner::Inner { .. } | ClientResponseBodyInner::Boxed { .. } => None,
        }
    }
}

impl fmt::Debug for ClientResponseBody {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("ClientResponseBody(..)")
    }
}

impl Default for ClientResponseBody {
    fn default() -> Self {
        Self::from(Bytes::new())
    }
}

impl From<BoxBody<Bytes, BoxError>> for ClientResponseBody {
    fn from(body: BoxBody<Bytes, BoxError>) -> Self {
        Self::boxed(body)
    }
}

impl From<Bytes> for ClientResponseBody {
    fn from(bytes: Bytes) -> Self {
        Self::reusable(bytes)
    }
}

impl From<Vec<u8>> for ClientResponseBody {
    fn from(vec: Vec<u8>) -> Self {
        Self::from(Bytes::from(vec))
    }
}

impl From<String> for ClientResponseBody {
    fn from(string: String) -> Self {
        Self::from(Bytes::from(string))
    }
}

impl From<&'static [u8]> for ClientResponseBody {
    fn from(bytes: &'static [u8]) -> Self {
        Self::from(Bytes::from_static(bytes))
    }
}

impl From<&'static str> for ClientResponseBody {
    fn from(string: &'static str) -> Self {
        Self::from(string.as_bytes())
    }
}

impl From<BoxBody<Bytes, BoxError>> for Body {
    #[inline]
    fn from(body: BoxBody<Bytes, BoxError>) -> Self {
        Self::boxed(body)
    }
}

impl From<ClientResponseBody> for Body {
    #[inline]
    fn from(body: ClientResponseBody) -> Self {
        Self {
            inner: BodyInner::Client {
                body: Box::pin(body),
            },
        }
    }
}

impl From<Bytes> for Body {
    #[inline]
    fn from(bytes: Bytes) -> Body {
        Body::reusable(bytes)
    }
}

impl From<Vec<u8>> for Body {
    #[inline]
    fn from(vec: Vec<u8>) -> Body {
        Body::reusable(vec.into())
    }
}

impl From<&'static [u8]> for Body {
    #[inline]
    fn from(s: &'static [u8]) -> Body {
        Body::reusable(Bytes::from_static(s))
    }
}

impl From<String> for Body {
    #[inline]
    fn from(s: String) -> Body {
        Body::reusable(s.into())
    }
}

impl From<&'static str> for Body {
    #[inline]
    fn from(s: &'static str) -> Body {
        s.as_bytes().into()
    }
}

#[cfg(feature = "stream")]
impl From<File> for Body {
    #[inline]
    fn from(file: File) -> Body {
        Body::wrap_stream(ReaderStream::new(file))
    }
}

impl HttpBody for Body {
    type Data = Bytes;
    type Error = Error;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        cx: &mut Context,
    ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
        match self.as_mut().project().inner.project() {
            BodyInnerProj::Reusable { bytes } => {
                let out = bytes.split_off(0);
                if out.is_empty() {
                    Poll::Ready(None)
                } else {
                    Poll::Ready(Some(Ok(http_body::Frame::data(out))))
                }
            }
            BodyInnerProj::Client { body } => {
                let mut body = body.get_mut().as_mut();
                Poll::Ready(ready!(body.as_mut().poll_frame(cx)).map(|opt_chunk| {
                    opt_chunk.map_err(|err| match err.downcast::<Error>() {
                        Ok(err) => *err,
                        Err(err) => Error::body(err),
                    })
                }))
            }
            BodyInnerProj::Boxed { body } => {
                Poll::Ready(ready!(body.poll_frame(cx)).map(|opt_chunk| {
                    opt_chunk.map_err(|err| match err.downcast::<Error>() {
                        Ok(err) => *err,
                        Err(err) => Error::body(err),
                    })
                }))
            }
        }
    }

    #[inline]
    fn size_hint(&self) -> SizeHint {
        match &self.inner {
            BodyInner::Reusable { bytes } => SizeHint::with_exact(bytes.len() as u64),
            BodyInner::Client { body } => body.size_hint(),
            BodyInner::Boxed { body } => body.size_hint(),
        }
    }

    #[inline]
    fn is_end_stream(&self) -> bool {
        match &self.inner {
            BodyInner::Reusable { bytes } => bytes.is_empty(),
            BodyInner::Client { body } => body.is_end_stream(),
            BodyInner::Boxed { body } => body.is_end_stream(),
        }
    }
}

impl HttpBody for ClientResponseBody {
    type Data = Bytes;
    type Error = BoxError;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        cx: &mut Context,
    ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
        match self.as_mut().project().inner.project() {
            ClientResponseBodyInnerProj::Reusable { bytes } => {
                let out = bytes.split_off(0);
                if out.is_empty() {
                    Poll::Ready(None)
                } else {
                    Poll::Ready(Some(Ok(http_body::Frame::data(out))))
                }
            }
            ClientResponseBodyInnerProj::Inner { body } => body.poll_frame(cx),
            ClientResponseBodyInnerProj::Boxed { body } => body.poll_frame(cx),
        }
    }

    fn size_hint(&self) -> SizeHint {
        match &self.inner {
            ClientResponseBodyInner::Reusable { bytes } => SizeHint::with_exact(bytes.len() as u64),
            ClientResponseBodyInner::Inner { body } => body.size_hint(),
            ClientResponseBodyInner::Boxed { body } => body.size_hint(),
        }
    }

    fn is_end_stream(&self) -> bool {
        match &self.inner {
            ClientResponseBodyInner::Reusable { bytes } => bytes.is_empty(),
            ClientResponseBodyInner::Inner { body } => body.is_end_stream(),
            ClientResponseBodyInner::Boxed { body } => body.is_end_stream(),
        }
    }
}

// ===== impl IntoBytesBody =====

impl<B> HttpBody for IntoBytesBody<B>
where
    B: HttpBody,
    B::Data: Into<Bytes>,
{
    type Data = Bytes;
    type Error = B::Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        cx: &mut Context,
    ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
        match ready!(self.project().inner.poll_frame(cx)) {
            Some(Ok(f)) => Poll::Ready(Some(Ok(f.map_data(Into::into)))),
            Some(Err(e)) => Poll::Ready(Some(Err(e))),
            None => Poll::Ready(None),
        }
    }

    #[inline]
    fn size_hint(&self) -> SizeHint {
        self.inner.size_hint()
    }

    #[inline]
    fn is_end_stream(&self) -> bool {
        self.inner.is_end_stream()
    }
}

#[cfg(test)]
mod tests {
    use http_body::Body as _;

    use super::Body;

    #[test]
    fn test_as_bytes() {
        let test_data = b"Test body";
        let body = Body::from(&test_data[..]);
        assert_eq!(body.as_bytes(), Some(&test_data[..]));
    }

    #[test]
    fn body_exact_length() {
        let empty_body = Body::empty();
        assert!(empty_body.is_end_stream());
        assert_eq!(empty_body.size_hint().exact(), Some(0));

        let bytes_body = Body::reusable("abc".into());
        assert!(!bytes_body.is_end_stream());
        assert_eq!(bytes_body.size_hint().exact(), Some(3));

        // can delegate even when wrapped
        let stream_body = Body::wrap(empty_body);
        assert!(stream_body.is_end_stream());
        assert_eq!(stream_body.size_hint().exact(), Some(0));
    }
}

// ===== AsSendBody trait =====

/// A trait for types that can be converted into an HTTP request body
/// with optional size information.
///
/// Implementations return `Some(len)` when the body size is known
/// ahead of time (allowing `Content-Length` to be set), or `None` when
/// the size is unknown (requiring `Transfer-Encoding: chunked`).
pub trait AsSendBody {
    /// Convert this value into a [`Body`].
    fn into_send_body(self) -> Body;

    /// Returns the content length if known ahead of time.
    fn content_length(&self) -> Option<u64> {
        None
    }
}

impl AsSendBody for Body {
    #[inline]
    fn into_send_body(self) -> Body {
        self
    }

    #[inline]
    fn content_length(&self) -> Option<u64> {
        self.size_hint().exact()
    }
}

impl AsSendBody for Bytes {
    #[inline]
    fn into_send_body(self) -> Body {
        Body::from(self)
    }

    #[inline]
    fn content_length(&self) -> Option<u64> {
        Some(self.len() as u64)
    }
}

impl AsSendBody for Vec<u8> {
    #[inline]
    fn into_send_body(self) -> Body {
        Body::from(self)
    }

    #[inline]
    fn content_length(&self) -> Option<u64> {
        Some(self.len() as u64)
    }
}

impl AsSendBody for String {
    #[inline]
    fn into_send_body(self) -> Body {
        Body::from(self)
    }

    #[inline]
    fn content_length(&self) -> Option<u64> {
        Some(self.len() as u64)
    }
}

impl AsSendBody for &'static str {
    #[inline]
    fn into_send_body(self) -> Body {
        Body::from(self)
    }

    #[inline]
    fn content_length(&self) -> Option<u64> {
        Some(self.len() as u64)
    }
}

impl AsSendBody for &'static [u8] {
    #[inline]
    fn into_send_body(self) -> Body {
        Body::from(self)
    }

    #[inline]
    fn content_length(&self) -> Option<u64> {
        Some(self.len() as u64)
    }
}

impl AsSendBody for () {
    #[inline]
    fn into_send_body(self) -> Body {
        Body::empty()
    }

    #[inline]
    fn content_length(&self) -> Option<u64> {
        Some(0)
    }
}

// Note: &str and &[u8] impls are intentionally omitted to avoid
// conflicting implementations with &'static str and &'static [u8].
// Use .to_owned() or Body::from() directly for non-static references.