/rust/registry/src/index.crates.io-1949cf8c6b5b557f/ureq-proto-0.6.0/src/client/recvresp.rs
Line | Count | Source |
1 | | use http::{HeaderName, HeaderValue, Response, StatusCode, Version, header}; |
2 | | |
3 | | use crate::Error; |
4 | | use crate::body::BodyReader; |
5 | | use crate::ext::HeaderIterExt; |
6 | | use crate::parser::{try_parse_partial_response, try_parse_response}; |
7 | | use crate::util::log_data; |
8 | | |
9 | | use super::MAX_RESPONSE_HEADERS; |
10 | | use super::state::RecvResponse; |
11 | | use super::{Call, CloseReason, RecvResponseResult}; |
12 | | |
13 | | impl Call<RecvResponse> { |
14 | | /// Try reading a response from the input. |
15 | | /// |
16 | | /// * `allow_partial_redirect` - if `true`, we can accept to find the `Location` header |
17 | | /// and proceed without reading the entire header. This is useful for broken servers that |
18 | | /// don't send an entire \r\n at the end of the preamble. |
19 | | /// |
20 | | /// The `(usize, Option<Response()>)` is `(input amount consumed, response`). |
21 | | /// |
22 | | /// Notice that it's possible that we get an `input amount consumed` despite not returning |
23 | | /// a `Some(Response)`. This can happen if the server returned a 100-continue, and due to |
24 | | /// timing reasons we did not receive it while we were in the `Await100` call state. This |
25 | | /// "spurios" 100 will be discarded before we parse the actual response. |
26 | 0 | pub fn try_response( |
27 | 0 | &mut self, |
28 | 0 | input: &[u8], |
29 | 0 | allow_partial_redirect: bool, |
30 | 0 | ) -> Result<(usize, Option<Response<()>>), Error> { |
31 | 0 | let maybe_response = self.do_try_response(input, allow_partial_redirect)?; |
32 | | |
33 | 0 | let (input_used, response) = match maybe_response { |
34 | 0 | Some(v) => v, |
35 | | // Not enough input for a full response yet |
36 | 0 | None => return Ok((0, None)), |
37 | | }; |
38 | | |
39 | | // Handle all 1xx informational responses according to RFC 9110. |
40 | | // These can happen in several scenarios: |
41 | | // 1. A "delayed" 100-continue when we used Expect: 100-continue but the server |
42 | | // did not produce the response in time while we were in the Await100 state. |
43 | | // 2. An unsolicited 1xx response from a server that sends it regardless of |
44 | | // whether the client requested it (e.g., on a regular GET request). |
45 | | // 3. Other informational responses like 102 Processing or 103 Early Hints. |
46 | | // |
47 | | // According to RFC 9110, all 1xx responses are informational and should be |
48 | | // consumed by clients, with the exception of 101 Switching Protocols. |
49 | 0 | let status = response.status(); |
50 | | |
51 | 0 | if status == StatusCode::CONTINUE { |
52 | | // Consume 100 Continue and clear the await flag for delayed 100-continue |
53 | 0 | self.inner.await_100_continue = false; |
54 | 0 | return Ok((input_used, None)); |
55 | 0 | } else if status == StatusCode::SWITCHING_PROTOCOLS { |
56 | 0 | // 101 is special: mark connection so it won't be returned to pool, |
57 | 0 | // but return the response to the caller (don't consume it). |
58 | 0 | self.inner.close_reason.push(CloseReason::ProtocolSwitch); |
59 | 0 | // Fall through to return the response |
60 | 0 | } else if status.is_informational() { |
61 | | // Other 1xx (102 Processing, 103 Early Hints, etc.): consume and wait for final response |
62 | 0 | return Ok((input_used, None)); |
63 | 0 | } |
64 | | |
65 | 0 | self.inner.status = Some(response.status()); |
66 | | // We want the last Location header. |
67 | 0 | self.inner.location = response |
68 | 0 | .headers() |
69 | 0 | .get_all(header::LOCATION) |
70 | 0 | .into_iter() |
71 | 0 | .next_back() |
72 | 0 | .cloned(); |
73 | | |
74 | 0 | if response.headers().iter().has(header::CONNECTION, "close") { |
75 | 0 | self.inner |
76 | 0 | .close_reason |
77 | 0 | .push(CloseReason::ServerConnectionClose); |
78 | 0 | } |
79 | | |
80 | 0 | Ok((input_used, Some(response))) |
81 | 0 | } |
82 | | |
83 | | /// Try reading response headers |
84 | | /// |
85 | | /// A response is only possible once the `input` holds all the HTTP response |
86 | | /// headers. Before that this returns `None`. When the response is succesfully read, |
87 | | /// the return value `(usize, Response<()>)` contains how many bytes were consumed |
88 | | /// of the `input`. |
89 | 0 | fn do_try_response( |
90 | 0 | &mut self, |
91 | 0 | input: &[u8], |
92 | 0 | allow_partial_redirect: bool, |
93 | 0 | ) -> Result<Option<(usize, Response<()>)>, Error> { |
94 | | // ~3k for 100 headers |
95 | 0 | let (input_used, response) = match try_parse_response::<MAX_RESPONSE_HEADERS>(input)? { |
96 | 0 | Some(v) => v, |
97 | | None => { |
98 | | // The caller decides whether to allow a partial parse. |
99 | 0 | if !allow_partial_redirect { |
100 | 0 | return Ok(None); |
101 | 0 | } |
102 | | |
103 | | // TODO(martin): I don't like this code. The mission is to be correct HTTP/1.1 |
104 | | // and this is a hack to allow for broken servers. |
105 | | // |
106 | | // As a special case, to handle broken servers that does a redirect without |
107 | | // the final trailing \r\n, we try parsing the response as partial, and |
108 | | // if it is a redirect, we can allow the request to continue. |
109 | 0 | let Some(mut r) = try_parse_partial_response::<MAX_RESPONSE_HEADERS>(input)? else { |
110 | 0 | return Ok(None); |
111 | | }; |
112 | | |
113 | | // A redirection must have a location header. |
114 | 0 | let is_complete_redirection = |
115 | 0 | r.status().is_redirection() && r.headers().contains_key(header::LOCATION); |
116 | | |
117 | 0 | if !is_complete_redirection { |
118 | 0 | return Ok(None); |
119 | 0 | } |
120 | | |
121 | | // Insert a synthetic connection: close, since the connection is |
122 | | // not valid after using a partial request. |
123 | 0 | debug!("Partial redirection response, insert fake connection: close"); |
124 | 0 | r.headers_mut() |
125 | 0 | .insert(header::CONNECTION, HeaderValue::from_static("close")); |
126 | | |
127 | 0 | (input.len(), r) |
128 | | } |
129 | | }; |
130 | | |
131 | 0 | log_data(&input[..input_used]); |
132 | | |
133 | 0 | let http10 = response.version() == Version::HTTP_10; |
134 | 0 | let status = response.status().as_u16(); |
135 | | |
136 | 0 | if status == StatusCode::CONTINUE { |
137 | | // There should be no headers for this response. |
138 | 0 | if !response.headers().is_empty() { |
139 | 0 | return Err(Error::HeadersWith100); |
140 | 0 | } |
141 | | |
142 | 0 | return Ok(Some((input_used, response))); |
143 | 0 | } |
144 | | |
145 | 0 | let header_lookup = |name: HeaderName| { |
146 | 0 | let header = response.headers().get(name)?; |
147 | 0 | header.to_str().ok() |
148 | 0 | }; |
149 | | |
150 | 0 | let force_recv = self.inner.force_recv_body; |
151 | 0 | let recv_body_mode = BodyReader::for_response( |
152 | 0 | http10, |
153 | 0 | self.inner.request.method(), |
154 | 0 | status, |
155 | 0 | force_recv, |
156 | 0 | &header_lookup, |
157 | 0 | )?; |
158 | | |
159 | 0 | self.inner.state.reader = Some(recv_body_mode); |
160 | | |
161 | 0 | Ok(Some((input_used, response))) |
162 | 0 | } |
163 | | |
164 | | /// Tell if we have finished receiving the response. |
165 | 0 | pub fn can_proceed(&self) -> bool { |
166 | 0 | self.inner.state.reader.is_some() |
167 | 0 | } |
168 | | |
169 | | /// Tell if response body is closed delimited |
170 | | /// |
171 | | /// HTTP/1.0 does not have `content-length` to serialize many requests over the same |
172 | | /// socket. Instead it uses socket close to determine the body is finished. |
173 | 0 | fn is_close_delimited(&self) -> bool { |
174 | 0 | let rbm = self.inner.state.reader.as_ref().unwrap(); |
175 | 0 | matches!(rbm, BodyReader::CloseDelimited) |
176 | 0 | } |
177 | | |
178 | | /// Proceed to the next state. |
179 | | /// |
180 | | /// This returns `None` if we have not finished receiving the response. It is guaranteed that if |
181 | | /// `can_proceed()` returns true, this will return `Some`. |
182 | 0 | pub fn proceed(mut self) -> Option<RecvResponseResult> { |
183 | 0 | if !self.can_proceed() { |
184 | 0 | return None; |
185 | 0 | } |
186 | | |
187 | 0 | let has_response_body = self.inner.state.need_response_body(); |
188 | | |
189 | 0 | if has_response_body { |
190 | 0 | if self.is_close_delimited() { |
191 | 0 | self.inner |
192 | 0 | .close_reason |
193 | 0 | .push(CloseReason::CloseDelimitedBody); |
194 | 0 | } |
195 | | |
196 | 0 | Some(RecvResponseResult::RecvBody(Call::wrap(self.inner))) |
197 | | } else { |
198 | 0 | Some(if self.inner.is_redirect() { |
199 | 0 | RecvResponseResult::Redirect(Call::wrap(self.inner)) |
200 | | } else { |
201 | 0 | RecvResponseResult::Cleanup(Call::wrap(self.inner)) |
202 | | }) |
203 | | } |
204 | 0 | } |
205 | | |
206 | | /// Convert the state to receive a body despite method. |
207 | | /// |
208 | | /// Methods like HEAD and CONNECT should not have attached bodies. |
209 | | /// Some broken APIs use bodies anyway and this is an escape hatch to |
210 | | /// interoperate with such services. |
211 | 0 | pub fn force_recv_body(&mut self) { |
212 | 0 | self.inner.force_recv_body = true; |
213 | 0 | } |
214 | | } |