bitreq 0.3.5

Simple, minimal-dependency 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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
use alloc::collections::BTreeMap;
use core::str;
#[cfg(feature = "async")]
use std::future::Future;
#[cfg(feature = "std")]
use std::io::{self, BufReader, Bytes, Read};

#[cfg(feature = "async")]
use tokio::io::{AsyncRead, AsyncReadExt};

#[cfg(feature = "std")]
use crate::connection::HttpStream;
use crate::Error;

#[cfg(feature = "std")]
const BACKING_READ_BUFFER_LENGTH: usize = 16 * 1024;
#[cfg(feature = "std")]
const MAX_CONTENT_LENGTH: usize = 16 * 1024;

/// An HTTP response.
///
/// Returned by [`Request::send`](struct.Request.html#method.send).
///
/// # Example
///
/// ```no_run
/// # #[cfg(feature = "std")]
/// # fn main() -> Result<(), bitreq::Error> {
/// let response = bitreq::get("http://example.com").send()?;
/// println!("{}", response.as_str()?);
/// # Ok(()) }
/// # #[cfg(not(feature = "std"))]
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { Ok(()) }
/// ```
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Response {
    /// The status code of the response, eg. 404.
    pub status_code: i32,
    /// The reason phrase of the response, eg. "Not Found".
    pub reason_phrase: String,
    /// The headers of the response. The header field names (the
    /// keys) are all lowercase.
    pub headers: BTreeMap<String, String>,
    /// The URL of the resource returned in this response. May differ from the
    /// request URL if it was redirected or typo corrections were applied (e.g.
    /// <http://example.com?foo=bar> would be corrected to
    /// <http://example.com/?foo=bar>).
    pub url: String,

    body: Vec<u8>,
}

impl Response {
    #[cfg(feature = "std")]
    pub(crate) fn create(
        mut parent: ResponseLazy,
        is_head: bool,
        max_body_size: Option<usize>,
    ) -> Result<Response, Error> {
        let mut body = Vec::new();
        if !is_head && parent.status_code != 204 && parent.status_code != 304 {
            for byte in &mut parent {
                let (byte, length) = byte?;
                if max_body_size.is_some_and(|max| body.len().saturating_add(length) > max) {
                    return Err(Error::BodyOverflow);
                }
                body.reserve(length);
                body.push(byte);
            }
        }

        let ResponseLazy { status_code, reason_phrase, headers, url, .. } = parent;

        Ok(Response { status_code, reason_phrase, headers, url, body })
    }

    #[cfg(feature = "async")]
    /// Fully read a [`Response`] from an async stream.
    ///
    /// When this crate was originally made "async", it actually just spawned sync requests on
    /// background threads and waited on their completion rather than actually doing async reads.
    /// In order to avoid changing the API while fixing this, we read the full response but then
    /// return a "lazy" response that has the full contents pre-read.
    pub(crate) async fn create_async<R: AsyncRead + Unpin>(
        stream: R,
        is_head: bool,
        max_headers_size: Option<usize>,
        max_status_line_len: Option<usize>,
        max_body_size: Option<usize>,
    ) -> Result<Response, Error> {
        use HttpStreamState::*;

        let mut stream = tokio::io::BufReader::with_capacity(BACKING_READ_BUFFER_LENGTH, stream);

        let ResponseMetadata {
            status_code,
            reason_phrase,
            mut headers,
            state,
            max_trailing_headers_size,
        } = read_metadata_async(&mut stream, max_headers_size, max_status_line_len).await?;

        let mut body = Vec::new();
        if !is_head && status_code != 204 && status_code != 304 {
            match state {
                EndOnClose => {
                    while let Some(byte_result) = read_until_closed_async(&mut stream).await {
                        let (byte, length) = byte_result?;
                        if max_body_size.is_some_and(|max| body.len().saturating_add(length) > max)
                        {
                            return Err(Error::BodyOverflow);
                        }
                        body.reserve(length);
                        body.push(byte);
                    }
                }
                ContentLength(mut length) => {
                    while let Some(byte_result) =
                        read_with_content_length_async(&mut stream, &mut length).await
                    {
                        let (byte, expected_length) = byte_result?;
                        if max_body_size
                            .is_some_and(|max| body.len().saturating_add(expected_length) > max)
                        {
                            return Err(Error::BodyOverflow);
                        }
                        body.reserve(expected_length);
                        body.push(byte);
                    }
                }
                Chunked(mut expecting_chunks, mut chunk_length, mut content_length) =>
                    while let Some(byte_result) = read_chunked_async(
                        &mut stream,
                        &mut headers,
                        &mut expecting_chunks,
                        &mut chunk_length,
                        &mut content_length,
                        max_trailing_headers_size,
                    )
                    .await
                    {
                        let (byte, length) = byte_result?;
                        if max_body_size.is_some_and(|max| body.len().saturating_add(length) > max)
                        {
                            return Err(Error::BodyOverflow);
                        }
                        body.reserve(length);
                        body.push(byte);
                    },
            }
        }

        Ok(Response { status_code, reason_phrase, headers, url: String::new(), body })
    }

    /// Returns the body as an `&str`.
    ///
    /// # Errors
    ///
    /// Returns
    /// [`InvalidUtf8InBody`](enum.Error.html#variant.InvalidUtf8InBody)
    /// if the body is not UTF-8, with a description as to why the
    /// provided slice is not UTF-8.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(feature = "std")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let url = "http://example.org/";
    /// let response = bitreq::get(url).send()?;
    /// println!("{}", response.as_str()?);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "std"))]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> { Ok(()) }
    /// ```
    pub fn as_str(&self) -> Result<&str, Error> {
        match str::from_utf8(&self.body) {
            Ok(s) => Ok(s),
            Err(err) => Err(Error::InvalidUtf8InBody(err)),
        }
    }

    /// Returns a reference to the contained bytes of the body. If you
    /// want the `Vec<u8>` itself, use
    /// [`into_bytes()`](#method.into_bytes) instead.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(feature = "std")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let url = "http://example.org/";
    /// let response = bitreq::get(url).send()?;
    /// println!("{:?}", response.as_bytes());
    /// # Ok(()) }
    /// # #[cfg(not(feature = "std"))]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> { Ok(()) }
    /// ```
    pub fn as_bytes(&self) -> &[u8] { &self.body }

    /// Turns the `Response` into the inner `Vec<u8>`, the bytes that
    /// make up the response's body. If you just need a `&[u8]`, use
    /// [`as_bytes()`](#method.as_bytes) instead.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(feature = "std")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let url = "http://example.org/";
    /// let response = bitreq::get(url).send()?;
    /// println!("{:?}", response.into_bytes());
    /// // This would error, as into_bytes consumes the Response:
    /// // let x = response.status_code;
    /// # Ok(()) }
    /// # #[cfg(not(feature = "std"))]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> { Ok(()) }
    /// ```
    pub fn into_bytes(self) -> Vec<u8> { self.body }

    /// Converts JSON body to a `struct` using Serde.
    ///
    /// # Errors
    ///
    /// Returns
    /// [`SerdeJsonError`](enum.Error.html#variant.SerdeJsonError) if
    /// Serde runs into a problem, or
    /// [`InvalidUtf8InBody`](enum.Error.html#variant.InvalidUtf8InBody)
    /// if the body is not UTF-8.
    ///
    /// # Example
    /// In case compiler cannot figure out return type you might need to declare it explicitly:
    ///
    /// ```no_run
    /// use serde_json::Value;
    ///
    /// # fn main() -> Result<(), bitreq::Error> {
    /// # let url_to_json_resource = "http://example.org/resource.json";
    /// // Value could be any type that implements Deserialize!
    /// let user = bitreq::get(url_to_json_resource).send()?.json::<Value>()?;
    /// println!("User name is '{}'", user["name"]);
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "json-using-serde")]
    pub fn json<'a, T>(&'a self) -> Result<T, Error>
    where
        T: serde::de::Deserialize<'a>,
    {
        match serde_json::from_slice(self.as_bytes()) {
            Ok(json) => Ok(json),
            Err(err) => Err(Error::SerdeJsonError(err)),
        }
    }
}

/// An HTTP response, which is loaded lazily.
///
/// In comparison to [`Response`](struct.Response.html), this is
/// returned from
/// [`send_lazy()`](struct.Request.html#method.send_lazy), where as
/// [`Response`](struct.Response.html) is returned from
/// [`send()`](struct.Request.html#method.send).
///
/// In practice, "lazy loading" means that the bytes are only loaded
/// as you iterate through them. The bytes are provided in the form of
/// a `Result<(u8, usize), bitreq::Error>`, as the reading operation
/// can fail in various ways. The `u8` is the actual byte that was
/// read, and `usize` is how many bytes we are expecting to read in
/// the future (including this byte). Note, however, that the `usize`
/// can change, particularly when the `Transfer-Encoding` is
/// `chunked`: then it will reflect how many bytes are left of the
/// current chunk. The expected size is capped at 16 KiB to avoid
/// server-side DoS attacks targeted at clients accidentally reserving
/// too much memory.
///
/// # Example
/// ```no_run
/// // This is how the normal Response works behind the scenes, and
/// // how you might use ResponseLazy.
/// # #[cfg(feature = "std")]
/// # fn main() -> Result<(), bitreq::Error> {
/// let response = bitreq::get("http://example.com").send_lazy()?;
/// let mut vec = Vec::new();
/// for result in response {
///     let (byte, length) = result?;
///     vec.reserve(length);
///     vec.push(byte);
/// }
/// # Ok(())
/// # }
/// # #[cfg(not(feature = "std"))]
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { Ok(()) }
/// ```
#[cfg(feature = "std")]
pub struct ResponseLazy {
    /// The status code of the response, eg. 404.
    pub status_code: i32,
    /// The reason phrase of the response, eg. "Not Found".
    pub reason_phrase: String,
    /// The headers of the response. The header field names (the
    /// keys) are all lowercase.
    pub headers: BTreeMap<String, String>,
    /// The URL of the resource returned in this response. May differ from the
    /// request URL if it was redirected or typo corrections were applied (e.g.
    /// <http://example.com?foo=bar> would be corrected to
    /// <http://example.com/?foo=bar>).
    pub url: String,

    stream: HttpStreamBytes,
    state: HttpStreamState,
    max_trailing_headers_size: Option<usize>,
    max_body_size: Option<usize>,
    bytes_read: usize,
}

#[cfg(feature = "std")]
type HttpStreamBytes = Bytes<BufReader<HttpStream>>;

#[cfg(feature = "std")]
impl ResponseLazy {
    pub(crate) fn from_stream(
        stream: HttpStream,
        max_headers_size: Option<usize>,
        max_status_line_len: Option<usize>,
        max_body_size: Option<usize>,
    ) -> Result<ResponseLazy, Error> {
        let mut stream = BufReader::with_capacity(BACKING_READ_BUFFER_LENGTH, stream).bytes();
        let ResponseMetadata {
            status_code,
            reason_phrase,
            headers,
            state,
            max_trailing_headers_size,
        } = read_metadata(&mut stream, max_headers_size, max_status_line_len)?;

        Ok(ResponseLazy {
            status_code,
            reason_phrase,
            headers,
            url: String::new(),
            stream,
            state,
            max_trailing_headers_size,
            max_body_size,
            bytes_read: 0,
        })
    }

    #[cfg(feature = "async")]
    pub(crate) fn dummy_from_response(response: Response) -> ResponseLazy {
        let http_stream = HttpStream::create_buffer(response.body);
        ResponseLazy {
            status_code: response.status_code,
            reason_phrase: response.reason_phrase,
            headers: response.headers,
            url: response.url,
            stream: BufReader::with_capacity(1, http_stream).bytes(),
            state: HttpStreamState::EndOnClose,
            max_trailing_headers_size: None,
            // Body was already fully loaded and size-checked by send_async
            max_body_size: None,
            bytes_read: 0,
        }
    }
}

#[cfg(feature = "std")]
impl Iterator for ResponseLazy {
    type Item = Result<(u8, usize), Error>;

    fn next(&mut self) -> Option<Self::Item> {
        use HttpStreamState::*;
        let result = match self.state {
            EndOnClose => read_until_closed(&mut self.stream),
            ContentLength(ref mut length) => read_with_content_length(&mut self.stream, length),
            Chunked(ref mut expecting_chunks, ref mut length, ref mut content_length) =>
                read_chunked(
                    &mut self.stream,
                    &mut self.headers,
                    expecting_chunks,
                    length,
                    content_length,
                    self.max_trailing_headers_size,
                ),
        };

        // Check body size limit before returning the byte
        if let Some(Ok((_, expected_length))) = &result {
            if self.max_body_size.is_some_and(|max| self.bytes_read + expected_length > max) {
                return Some(Err(Error::BodyOverflow));
            }
            self.bytes_read += 1;
        }

        result
    }
}

#[cfg(feature = "std")]
impl Read for ResponseLazy {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let mut index = 0;
        for res in self {
            // there is no use for the estimated length in the read implementation
            // so it is ignored.
            let (byte, _) = res.map_err(|e| match e {
                Error::IoError(e) => e,
                _ => io::Error::new(io::ErrorKind::Other, e),
            })?;

            buf[index] = byte;
            index += 1;

            // if the buffer is full, it should stop reading
            if index >= buf.len() {
                break;
            }
        }

        // index of the next byte is the number of bytes thats have been read
        Ok(index)
    }
}

#[cfg(feature = "std")]
enum HttpStreamState {
    // No Content-Length, and Transfer-Encoding != chunked, so we just
    // read unti lthe server closes the connection (this should be the
    // fallback, if I read the rfc right).
    EndOnClose,
    // Content-Length was specified, read that amount of bytes
    ContentLength(usize),
    // Transfer-Encoding == chunked, so we need to save two pieces of
    // information: are we expecting more chunks, how much is there
    // left of the current chunk, and how much have we read? The last
    // number is needed in order to provide an accurate Content-Length
    // header after loading all the bytes.
    Chunked(bool, usize, usize),
}

// This struct is just used in the Response and ResponseLazy
// constructors, but not in their structs, for api-cleanliness
// reasons. (Eg. response.status_code is much cleaner than
// response.meta.status_code or similar.)
#[cfg(feature = "std")]
struct ResponseMetadata {
    status_code: i32,
    reason_phrase: String,
    headers: BTreeMap<String, String>,
    state: HttpStreamState,
    max_trailing_headers_size: Option<usize>,
}

macro_rules! maybe_await {
    ($e: expr, await) => {
        $e.await
    };
    ($e: expr,) => {
        $e
    };
}

#[cfg(feature = "async")]
/// We need to mungle [`AsyncRead`] to look like an iterator, which we do here.
trait AsyncIteratorReadExt {
    fn next(&mut self) -> impl Future<Output = Option<Result<u8, io::Error>>>;
}

#[cfg(feature = "async")]
impl<T: AsyncReadExt + Unpin> AsyncIteratorReadExt for T {
    async fn next(&mut self) -> Option<Result<u8, io::Error>> { Some(self.read_u8().await) }
}

macro_rules! define_read_methods {
    (($read_until_closed: ident, $read_with_content_length: ident, $read_trailers: ident, $read_chunked: ident, $read_metadata: ident, $read_line: ident)<$($arg: ident : $($argty: path $(|)?)*),*>, $stream_type: ident $(, $async: tt, $await: tt)?) => {
        $($async)? fn $read_until_closed<$($arg: $($argty +)*),*>(
            bytes: &mut $stream_type,
        ) -> Option<<ResponseLazy as Iterator>::Item> {
            if let Some(byte) = maybe_await!(bytes.next(), $($await)?) {
                match byte {
                    Ok(byte) => Some(Ok((byte, 1))),
                    Err(err) => Some(Err(Error::IoError(err))),
                }
            } else {
                None
            }
        }

        $($async)? fn $read_with_content_length<$($arg: $($argty +)*),*>(
            bytes: &mut $stream_type,
            content_length: &mut usize,
        ) -> Option<<ResponseLazy as Iterator>::Item> {
            if *content_length > 0 {
                *content_length -= 1;

                if let Some(byte) = maybe_await!(bytes.next(), $($await)?) {
                    match byte {
                        // Cap Content-Length to 16KiB, to avoid out-of-memory issues.
                        Ok(byte) => return Some(Ok((byte, (*content_length).min(MAX_CONTENT_LENGTH) + 1))),
                        Err(err) => return Some(Err(Error::IoError(err))),
                    }
                }
            }
            None
        }

        $($async)? fn $read_trailers<$($arg: $($argty +)*),*>(
            bytes: &mut $stream_type,
            headers: &mut BTreeMap<String, String>,
            mut max_headers_size: Option<usize>,
        ) -> Result<(), Error> {
            loop {
                let trailer_line = maybe_await!($read_line(bytes, max_headers_size, Error::HeadersOverflow), $($await)?)?;
                if let Some(ref mut max_headers_size) = max_headers_size {
                    *max_headers_size -= trailer_line.len() + 2;
                }
                if let Some((header, value)) = parse_header(trailer_line) {
                    headers.insert(header, value);
                } else {
                    break;
                }
            }
            Ok(())
        }

        $($async)? fn $read_chunked<$($arg: $($argty +)*),*>(
            bytes: &mut $stream_type,
            headers: &mut BTreeMap<String, String>,
            expecting_more_chunks: &mut bool,
            chunk_length: &mut usize,
            content_length: &mut usize,
            max_trailing_headers_size: Option<usize>,
        ) -> Option<<ResponseLazy as Iterator>::Item> {
            if !*expecting_more_chunks && *chunk_length == 0 {
                return None;
            }

            if *chunk_length == 0 {
                // Max length of the chunk length line is 1KB: not too long to
                // take up much memory, long enough to tolerate some chunk
                // extensions (which are ignored).

                // Get the size of the next chunk
                let length_line = match maybe_await!($read_line(bytes, Some(1024), Error::MalformedChunkLength), $($await)?) {
                    Ok(line) => line,
                    Err(err) => return Some(Err(err)),
                };

                // Note: the trim() and check for empty lines shouldn't be
                // needed according to the RFC, but we might as well, it's a
                // small change and it fixes a few servers.
                let incoming_length = if length_line.is_empty() {
                    0
                } else {
                    let length = if let Some(i) = length_line.find(';') {
                        length_line[..i].trim()
                    } else {
                        length_line.trim()
                    };
                    match usize::from_str_radix(length, 16) {
                        Ok(length) => length,
                        Err(_) => return Some(Err(Error::MalformedChunkLength)),
                    }
                };

                if incoming_length == 0 {
                    if let Err(err) = maybe_await!($read_trailers(bytes, headers, max_trailing_headers_size), $($await)?) {
                        return Some(Err(err));
                    }

                    *expecting_more_chunks = false;
                    headers.insert("content-length".to_string(), (*content_length).to_string());
                    headers.remove("transfer-encoding");
                    return None;
                }
                *chunk_length = incoming_length;
                *content_length = content_length.saturating_add(incoming_length);
            }

            if *chunk_length > 0 {
                *chunk_length -= 1;
                if let Some(byte) = maybe_await!(bytes.next(), $($await)?) {
                    match byte {
                        Ok(byte) => {
                            // If we're at the end of the chunk...
                            if *chunk_length == 0 {
                                //...read the trailing \r\n of the chunk, and
                                // possibly return an error instead.

                                // TODO: Maybe this could be written in a way
                                // that doesn't discard the last ok byte if
                                // the \r\n reading fails?
                                if let Err(err) = maybe_await!($read_line(bytes, Some(2), Error::MalformedChunkEnd), $($await)?) {
                                    return Some(Err(err));
                                }
                            }

                            return Some(Ok((byte, (*chunk_length).min(MAX_CONTENT_LENGTH) + 1)));
                        }
                        Err(err) => return Some(Err(Error::IoError(err))),
                    }
                }
            }

            None
        }

        #[cfg(feature = "std")]
        $($async)? fn $read_metadata<$($arg: $($argty +)*),*>(
            stream: &mut $stream_type,
            mut max_headers_size: Option<usize>,
            max_status_line_len: Option<usize>,
        ) -> Result<ResponseMetadata, Error> {
            let line = maybe_await!($read_line(stream, max_status_line_len, Error::StatusLineOverflow), $($await)?)?;
            let (status_code, reason_phrase) = parse_status_line(&line);

            let mut headers = BTreeMap::new();
            loop {
                let line = maybe_await!($read_line(stream, max_headers_size, Error::HeadersOverflow), $($await)?)?;
                if line.is_empty() {
                    // Body starts here
                    break;
                }
                if let Some(ref mut max_headers_size) = max_headers_size {
                    *max_headers_size -= line.len() + 2;
                }
                if let Some(header) = parse_header(line) {
                    headers.insert(header.0, header.1);
                }
            }

            let mut chunked = false;
            let mut content_length = None;
            for (header, value) in &headers {
                // Handle the Transfer-Encoding header
                if header.to_lowercase().trim() == "transfer-encoding"
                    && value.to_lowercase().trim() == "chunked"
                {
                    chunked = true;
                }

                // Handle the Content-Length header
                if header.to_lowercase().trim() == "content-length" {
                    match str::parse::<usize>(value.trim()) {
                        Ok(length) => content_length = Some(length),
                        Err(_) => return Err(Error::MalformedContentLength),
                    }
                }
            }

            let state = if chunked {
                HttpStreamState::Chunked(true, 0, 0)
            } else if let Some(length) = content_length {
                HttpStreamState::ContentLength(length)
            } else {
                HttpStreamState::EndOnClose
            };

            Ok(ResponseMetadata {
                status_code,
                reason_phrase,
                headers,
                state,
                max_trailing_headers_size: max_headers_size,
            })
        }

        #[cfg(feature = "std")]
        $($async)? fn $read_line<$($arg: $($argty +)*),*>(
            stream: &mut $stream_type,
            max_len: Option<usize>,
            overflow_error: Error,
        ) -> Result<String, Error> {
            let mut bytes = Vec::with_capacity(32);
            while let Some(byte) = maybe_await!(stream.next(), $($await)?) {
                match byte {
                    Ok(byte) => {
                        if let Some(max_len) = max_len {
                            if bytes.len() >= max_len {
                                return Err(overflow_error);
                            }
                        }
                        if byte == b'\n' {
                            if let Some(b'\r') = bytes.last() {
                                bytes.pop();
                            }
                            break;
                        } else {
                            bytes.push(byte);
                        }
                    }
                    Err(err) => return Err(Error::IoError(err)),
                }
            }
            String::from_utf8(bytes).map_err(|_error| Error::InvalidUtf8InResponse)
        }
    }
}

#[cfg(feature = "std")]
define_read_methods!((read_until_closed, read_with_content_length, read_trailers, read_chunked, read_metadata, read_line)<>, HttpStreamBytes);
#[cfg(feature = "async")]
define_read_methods!((read_until_closed_async, read_with_content_length_async, read_trailers_async, read_chunked_async, read_metadata_async, read_line_async)<R: AsyncRead | Unpin>, R, async, await);

#[cfg(feature = "std")]
fn parse_status_line(line: &str) -> (i32, String) {
    // sample status line format
    // HTTP/1.1 200 OK
    let mut status_code = String::with_capacity(3);
    let mut reason_phrase = String::with_capacity(2);

    let mut spaces = 0;

    for c in line.chars() {
        if spaces >= 2 {
            reason_phrase.push(c);
        }

        if c == ' ' {
            spaces += 1;
        } else if spaces == 1 {
            status_code.push(c);
        }
    }

    if let Ok(status_code) = status_code.parse::<i32>() {
        return (status_code, reason_phrase);
    }

    (503, "Server did not provide a status line".to_string())
}

#[cfg(feature = "std")]
fn parse_header(mut line: String) -> Option<(String, String)> {
    if let Some(location) = line.find(':') {
        // Trim the first character of the header if it is a space,
        // otherwise return everything after the ':'. This should
        // preserve the behavior in versions <=2.0.1 in most cases
        // (namely, ones where it was valid), where the first
        // character after ':' was always cut off.
        let value = if let Some(sp) = line.get(location + 1..location + 2) {
            if sp == " " {
                line[location + 2..].to_string()
            } else {
                line[location + 1..].to_string()
            }
        } else {
            line[location + 1..].to_string()
        };

        line.truncate(location);
        // Headers should be ascii, I'm pretty sure. If not, please open an issue.
        line.make_ascii_lowercase();
        return Some((line, value));
    }
    None
}