ureq_proto/client/await100.rs
1use http::StatusCode;
2
3use crate::body::BodyWriter;
4use crate::parser::try_parse_response;
5use crate::Error;
6
7use super::state::Await100;
8use super::{Await100Result, Call};
9use crate::CloseReason;
10
11impl Call<Await100> {
12 /// Attempt to read a 100-continue response.
13 ///
14 /// Tries to interpret bytes sent by the server as a 100-continue response. The expect-100 mechanic
15 /// means we hope the server will give us an indication on whether to upload a potentially big
16 /// request body, before we start doing it.
17 ///
18 /// * If the server supports expect-100, it will respond `HTTP/1.1 100 Continue\r\n\r\n`, or
19 /// some other response code (such as 403) if we are not allowed to post the body.
20 /// * If the server does not support expect-100, it will not respond at all, in which case
21 /// we will proceed to sending the request body after some timeout.
22 ///
23 /// The results are:
24 ///
25 /// * `Ok(0)` - not enough data yet, continue waiting (or `proceed()` if you think we waited enough)
26 /// * `Ok(n)` - `n` number of input bytes were consumed. Call `proceed()` next
27 /// * `Err(e)` - some error that is not recoverable
28 pub fn try_read_100(&mut self, input: &[u8]) -> Result<usize, Error> {
29 // Try parsing a status line without any headers. The line we are looking for is:
30 //
31 // HTTP/1.1 100 Continue\r\n\r\n
32 //
33 // There should be no headers.
34 match try_parse_response::<0>(input) {
35 Ok(v) => match v {
36 Some((input_used, response)) => {
37 self.inner.await_100_continue = false;
38
39 if response.status() == StatusCode::CONTINUE {
40 // should_send_body ought to be true since initialization.
41 assert!(self.inner.state.writer.has_body());
42 Ok(input_used)
43 } else {
44 // We encountered a status line, without headers, but it wasn't 100,
45 // so we should not continue to send the body. Furthermore we mustn't
46 // reuse the connection.
47 // https://curl.se/mail/lib-2004-08/0002.html
48 self.inner.close_reason.push(CloseReason::Not100Continue);
49 self.inner.state.writer = BodyWriter::new_none();
50 Ok(0)
51 }
52 }
53 // Not enough input yet.
54 None => Ok(0),
55 },
56 Err(e) => {
57 self.inner.await_100_continue = false;
58
59 if e == Error::HttpParseTooManyHeaders {
60 // We encountered headers after the status line. That means the server did
61 // not send 100-continue, and also continued to produce an answer before we
62 // sent the body. Regardless of what the answer is, we must not send the body.
63 // A 200-answer would be nonsensical given we haven't yet sent the body.
64 //
65 // We do however want to receive the response to be able to provide
66 // the Response<()> to the user. Hence this is not considered an error.
67 self.inner.close_reason.push(CloseReason::Not100Continue);
68 self.inner.state.writer = BodyWriter::new_none();
69 Ok(0)
70 } else {
71 Err(e)
72 }
73 }
74 }
75 }
76
77 /// Tell if there is any point in waiting for more data from the server.
78 ///
79 /// Becomes `false` as soon as `try_read_100()` got enough data to determine what to do next.
80 /// This might become `false` even if `try_read_100` returns `Ok(0)`.
81 ///
82 /// If this returns `false`, the user should continue with `proceed()`.
83 pub fn can_keep_await_100(&self) -> bool {
84 self.inner.await_100_continue
85 }
86
87 /// Proceed to the next state.
88 pub fn proceed(self) -> Result<Await100Result, Error> {
89 // We can always proceed out of Await100
90
91 if self.inner.state.writer.has_body() {
92 // TODO(martin): do i need this?
93 // call.inner.call.analyze_request()?;
94 let call = Call::wrap(self.inner);
95 Ok(Await100Result::SendBody(call))
96 } else {
97 Ok(Await100Result::RecvResponse(Call::wrap(self.inner)))
98 }
99 }
100}