ureq_proto/client/
recvbody.rs

1use crate::body::BodyReader;
2use crate::{BodyMode, Error};
3
4use super::state::RecvBody;
5use super::{Call, RecvBodyResult};
6
7impl Call<RecvBody> {
8    /// Read the response body from `input` to `output`.
9    ///
10    /// Depending on response headers, we can be in `transfer-encoding: chunked` or not. If we are,
11    /// there will be less `output` bytes than `input`.
12    ///
13    /// The result `(usize, usize)` is `(input consumed, output buffer used)`.
14    pub fn read(&mut self, input: &[u8], output: &mut [u8]) -> Result<(usize, usize), Error> {
15        let rbm = self.inner.state.reader.as_mut().unwrap();
16
17        if rbm.is_ended() {
18            return Ok((0, 0));
19        }
20
21        rbm.read(input, output, self.inner.state.stop_on_chunk_boundary)
22    }
23
24    /// Set if we are stopping on chunk boundaries.
25    ///
26    /// If `false`, we try to fill entire `output` on each read() call.
27    /// Has no meaning unless the response in chunked.
28    ///
29    /// Defaults to `false`
30    pub fn stop_on_chunk_boundary(&mut self, enabled: bool) {
31        self.inner.state.stop_on_chunk_boundary = enabled;
32    }
33
34    /// Tell if the reading is on a chunk boundary.
35    ///
36    /// Used when we want to read exactly chunk-by-chunk.
37    ///
38    /// Only releveant if we first enabled `stop_on_chunk_boundary()`.
39    pub fn is_on_chunk_boundary(&self) -> bool {
40        let rbm = self.inner.state.reader.as_ref().unwrap();
41        rbm.is_on_chunk_boundary()
42    }
43
44    /// Tell which kind of mode the response body is.
45    pub fn body_mode(&self) -> BodyMode {
46        self.inner.state.reader.as_ref().unwrap().body_mode()
47    }
48
49    /// Check if the response body has been fully received.
50    pub fn can_proceed(&self) -> bool {
51        self.is_ended() || self.is_close_delimited()
52    }
53
54    /// Tell if the response is over
55    fn is_ended(&self) -> bool {
56        let rbm = self.inner.state.reader.as_ref().unwrap();
57        rbm.is_ended()
58    }
59
60    /// Tell if we got an end chunk when reading chunked.
61    ///
62    /// A normal chunk ending is:
63    ///
64    /// ```text
65    /// 0\r\n
66    /// \r\n
67    /// ```
68    ///
69    /// However there are cases where the server abruptly does a `FIN` after sending `0\r\n`.
70    /// This means we still got the entire response body, and could use it, but not reuse the connection.
71    ///
72    /// This returns true as soon as we got the `0\r\n`.
73    pub fn is_ended_chunked(&self) -> bool {
74        let rbm = self.inner.state.reader.as_ref().unwrap();
75        rbm.is_ended_chunked()
76    }
77
78    /// Tell if response body is closed delimited
79    ///
80    /// HTTP/1.0 does not have `content-length` to serialize many requests over the same
81    /// socket. Instead it uses socket close to determine the body is finished.
82    fn is_close_delimited(&self) -> bool {
83        let rbm = self.inner.state.reader.as_ref().unwrap();
84        matches!(rbm, BodyReader::CloseDelimited)
85    }
86
87    /// Proceed to the next state.
88    ///
89    /// Returns `None` if we are not fully received the body. It is guaranteed that if `can_proceed()`
90    /// returns `true`, this will return `Some`.
91    pub fn proceed(self) -> Option<RecvBodyResult> {
92        if !self.can_proceed() {
93            return None;
94        }
95
96        Some(if self.inner.is_redirect() {
97            RecvBodyResult::Redirect(Call::wrap(self.inner))
98        } else {
99            RecvBodyResult::Cleanup(Call::wrap(self.inner))
100        })
101    }
102}