/rust/registry/src/index.crates.io-1949cf8c6b5b557f/ureq-proto-0.6.5/src/client/prepare.rs
Line | Count | Source |
1 | | use http::{HeaderMap, HeaderName, HeaderValue, Method, Request, Uri, Version, header}; |
2 | | |
3 | | use crate::body::BodyWriter; |
4 | | use crate::client::amended::AmendedRequest; |
5 | | use crate::ext::{HeaderIterExt, MethodExt}; |
6 | | use crate::{ArrayVec, Error}; |
7 | | |
8 | | use super::state::{Prepare, SendRequest}; |
9 | | use super::{BodyState, Call, CloseReason, Inner}; |
10 | | |
11 | | impl Call<Prepare> { |
12 | | /// Create a new Call instance from an HTTP request. |
13 | | /// |
14 | | /// This initializes a new Call state machine in the Prepare state, |
15 | | /// setting up the necessary internal state based on the request properties. |
16 | 0 | pub fn new(request: Request<()>) -> Result<Self, Error> { |
17 | 0 | let mut close_reason = ArrayVec::from_fn(|_| CloseReason::ClientConnectionClose); |
18 | | |
19 | 0 | if request.version() == Version::HTTP_10 { |
20 | | // request.analyze() in CallHolder::new() ensures the only versions are HTTP 1.0 and 1.1 |
21 | 0 | close_reason.push(CloseReason::CloseDelimitedBody) |
22 | 0 | } |
23 | | |
24 | 0 | if request.headers().iter().has(header::CONNECTION, "close") { |
25 | 0 | close_reason.push(CloseReason::ClientConnectionClose); |
26 | 0 | } |
27 | | |
28 | 0 | let need_request_body = request.method().need_request_body(); |
29 | 0 | let await_100_continue = request.headers().iter().has_expect_100(); |
30 | | |
31 | 0 | let request = AmendedRequest::new(request); |
32 | | |
33 | 0 | let default_body_mode = if need_request_body { |
34 | 0 | BodyWriter::new_chunked() |
35 | | } else { |
36 | 0 | BodyWriter::new_none() |
37 | | }; |
38 | | |
39 | 0 | let inner = Inner { |
40 | 0 | request, |
41 | 0 | analyzed: false, |
42 | 0 | state: BodyState { |
43 | 0 | writer: default_body_mode, |
44 | 0 | ..Default::default() |
45 | 0 | }, |
46 | 0 | close_reason, |
47 | 0 | force_send_body: false, |
48 | 0 | force_recv_body: false, |
49 | 0 | await_100_continue, |
50 | 0 | status: None, |
51 | 0 | location: None, |
52 | 0 | }; |
53 | | |
54 | 0 | Ok(Call::wrap(inner)) |
55 | 0 | } |
56 | | |
57 | | /// Inspect call method |
58 | 0 | pub fn method(&self) -> &Method { |
59 | 0 | self.inner.request.method() |
60 | 0 | } |
61 | | |
62 | | /// Inspect call URI |
63 | 0 | pub fn uri(&self) -> &Uri { |
64 | 0 | self.inner.request.uri() |
65 | 0 | } |
66 | | |
67 | | /// Inspect call HTTP version |
68 | 0 | pub fn version(&self) -> Version { |
69 | 0 | self.inner.request.version() |
70 | 0 | } |
71 | | |
72 | | /// Inspect call headers |
73 | 0 | pub fn headers(&self) -> &HeaderMap { |
74 | 0 | self.inner.request.original_request_headers() |
75 | 0 | } |
76 | | |
77 | | /// Set whether to allow non-standard HTTP methods. |
78 | | /// |
79 | | /// By default the methods are limited by the HTTP version. |
80 | 0 | pub fn allow_non_standard_methods(&mut self, v: bool) { |
81 | 0 | self.inner.state.allow_non_standard_methods = v; |
82 | 0 | } |
83 | | |
84 | | /// Add more headers to the call |
85 | 0 | pub fn header<K, V>(&mut self, key: K, value: V) -> Result<(), Error> |
86 | 0 | where |
87 | 0 | HeaderName: TryFrom<K>, |
88 | 0 | <HeaderName as TryFrom<K>>::Error: Into<http::Error>, |
89 | 0 | HeaderValue: TryFrom<V>, |
90 | 0 | <HeaderValue as TryFrom<V>>::Error: Into<http::Error>, |
91 | | { |
92 | 0 | self.inner.request.set_header(key, value) |
93 | 0 | } Unexecuted instantiation: <ureq_proto::client::Call<ureq_proto::client::state::Prepare>>::header::<http::header::name::HeaderName, http::header::value::HeaderValue> Unexecuted instantiation: <ureq_proto::client::Call<ureq_proto::client::state::Prepare>>::header::<_, _> |
94 | | |
95 | | /// Convert the state to send body despite method. |
96 | | /// |
97 | | /// Methods like GET, HEAD and DELETE should not have a request body. |
98 | | /// Some broken APIs use bodies anyway, and this is an escape hatch to |
99 | | /// interoperate with such services. |
100 | 0 | pub fn force_send_body(&mut self) { |
101 | 0 | self.inner.force_send_body = true; |
102 | 0 | } |
103 | | |
104 | | /// Continue to the next call state. |
105 | 0 | pub fn proceed(self) -> Call<SendRequest> { |
106 | 0 | Call::wrap(self.inner) |
107 | 0 | } |
108 | | } |