wreq-rb 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Cargo.lock +2688 -0
- data/Cargo.toml +6 -0
- data/README.md +179 -0
- data/ext/wreq_rb/Cargo.toml +39 -0
- data/ext/wreq_rb/extconf.rb +22 -0
- data/ext/wreq_rb/src/client.rs +565 -0
- data/ext/wreq_rb/src/error.rs +25 -0
- data/ext/wreq_rb/src/lib.rs +20 -0
- data/ext/wreq_rb/src/response.rs +132 -0
- data/lib/wreq-rb/version.rb +5 -0
- data/lib/wreq-rb.rb +17 -0
- data/patches/0001-add-transfer-size-tracking.patch +292 -0
- data/vendor/wreq/Cargo.toml +306 -0
- data/vendor/wreq/LICENSE +202 -0
- data/vendor/wreq/README.md +122 -0
- data/vendor/wreq/examples/cert_store.rs +77 -0
- data/vendor/wreq/examples/connect_via_lower_priority_tokio_runtime.rs +258 -0
- data/vendor/wreq/examples/emulation.rs +118 -0
- data/vendor/wreq/examples/form.rs +14 -0
- data/vendor/wreq/examples/http1_websocket.rs +37 -0
- data/vendor/wreq/examples/http2_websocket.rs +45 -0
- data/vendor/wreq/examples/json_dynamic.rs +41 -0
- data/vendor/wreq/examples/json_typed.rs +47 -0
- data/vendor/wreq/examples/keylog.rs +16 -0
- data/vendor/wreq/examples/request_with_emulation.rs +115 -0
- data/vendor/wreq/examples/request_with_interface.rs +37 -0
- data/vendor/wreq/examples/request_with_local_address.rs +16 -0
- data/vendor/wreq/examples/request_with_proxy.rs +13 -0
- data/vendor/wreq/examples/request_with_redirect.rs +22 -0
- data/vendor/wreq/examples/request_with_version.rs +15 -0
- data/vendor/wreq/examples/tor_socks.rs +24 -0
- data/vendor/wreq/examples/unix_socket.rs +33 -0
- data/vendor/wreq/src/client/body.rs +304 -0
- data/vendor/wreq/src/client/conn/conn.rs +231 -0
- data/vendor/wreq/src/client/conn/connector.rs +549 -0
- data/vendor/wreq/src/client/conn/http.rs +1023 -0
- data/vendor/wreq/src/client/conn/proxy/socks.rs +233 -0
- data/vendor/wreq/src/client/conn/proxy/tunnel.rs +260 -0
- data/vendor/wreq/src/client/conn/proxy.rs +39 -0
- data/vendor/wreq/src/client/conn/tls_info.rs +98 -0
- data/vendor/wreq/src/client/conn/uds.rs +44 -0
- data/vendor/wreq/src/client/conn/verbose.rs +149 -0
- data/vendor/wreq/src/client/conn.rs +323 -0
- data/vendor/wreq/src/client/core/body/incoming.rs +485 -0
- data/vendor/wreq/src/client/core/body/length.rs +118 -0
- data/vendor/wreq/src/client/core/body.rs +34 -0
- data/vendor/wreq/src/client/core/common/buf.rs +149 -0
- data/vendor/wreq/src/client/core/common/rewind.rs +141 -0
- data/vendor/wreq/src/client/core/common/watch.rs +76 -0
- data/vendor/wreq/src/client/core/common.rs +3 -0
- data/vendor/wreq/src/client/core/conn/http1.rs +342 -0
- data/vendor/wreq/src/client/core/conn/http2.rs +307 -0
- data/vendor/wreq/src/client/core/conn.rs +11 -0
- data/vendor/wreq/src/client/core/dispatch.rs +299 -0
- data/vendor/wreq/src/client/core/error.rs +435 -0
- data/vendor/wreq/src/client/core/ext.rs +201 -0
- data/vendor/wreq/src/client/core/http1.rs +178 -0
- data/vendor/wreq/src/client/core/http2.rs +483 -0
- data/vendor/wreq/src/client/core/proto/h1/conn.rs +988 -0
- data/vendor/wreq/src/client/core/proto/h1/decode.rs +1170 -0
- data/vendor/wreq/src/client/core/proto/h1/dispatch.rs +684 -0
- data/vendor/wreq/src/client/core/proto/h1/encode.rs +580 -0
- data/vendor/wreq/src/client/core/proto/h1/io.rs +879 -0
- data/vendor/wreq/src/client/core/proto/h1/role.rs +694 -0
- data/vendor/wreq/src/client/core/proto/h1.rs +104 -0
- data/vendor/wreq/src/client/core/proto/h2/client.rs +650 -0
- data/vendor/wreq/src/client/core/proto/h2/ping.rs +539 -0
- data/vendor/wreq/src/client/core/proto/h2.rs +379 -0
- data/vendor/wreq/src/client/core/proto/headers.rs +138 -0
- data/vendor/wreq/src/client/core/proto.rs +58 -0
- data/vendor/wreq/src/client/core/rt/bounds.rs +57 -0
- data/vendor/wreq/src/client/core/rt/timer.rs +150 -0
- data/vendor/wreq/src/client/core/rt/tokio.rs +99 -0
- data/vendor/wreq/src/client/core/rt.rs +25 -0
- data/vendor/wreq/src/client/core/upgrade.rs +267 -0
- data/vendor/wreq/src/client/core.rs +16 -0
- data/vendor/wreq/src/client/emulation.rs +161 -0
- data/vendor/wreq/src/client/http/client/error.rs +142 -0
- data/vendor/wreq/src/client/http/client/exec.rs +29 -0
- data/vendor/wreq/src/client/http/client/extra.rs +77 -0
- data/vendor/wreq/src/client/http/client/lazy.rs +79 -0
- data/vendor/wreq/src/client/http/client/pool.rs +1105 -0
- data/vendor/wreq/src/client/http/client/util.rs +104 -0
- data/vendor/wreq/src/client/http/client.rs +1003 -0
- data/vendor/wreq/src/client/http/future.rs +99 -0
- data/vendor/wreq/src/client/http.rs +1629 -0
- data/vendor/wreq/src/client/layer/config/options.rs +156 -0
- data/vendor/wreq/src/client/layer/config.rs +116 -0
- data/vendor/wreq/src/client/layer/cookie.rs +161 -0
- data/vendor/wreq/src/client/layer/decoder.rs +139 -0
- data/vendor/wreq/src/client/layer/redirect/future.rs +270 -0
- data/vendor/wreq/src/client/layer/redirect/policy.rs +63 -0
- data/vendor/wreq/src/client/layer/redirect.rs +145 -0
- data/vendor/wreq/src/client/layer/retry/classify.rs +105 -0
- data/vendor/wreq/src/client/layer/retry/scope.rs +51 -0
- data/vendor/wreq/src/client/layer/retry.rs +151 -0
- data/vendor/wreq/src/client/layer/timeout/body.rs +233 -0
- data/vendor/wreq/src/client/layer/timeout/future.rs +90 -0
- data/vendor/wreq/src/client/layer/timeout.rs +177 -0
- data/vendor/wreq/src/client/layer.rs +15 -0
- data/vendor/wreq/src/client/multipart.rs +717 -0
- data/vendor/wreq/src/client/request.rs +818 -0
- data/vendor/wreq/src/client/response.rs +534 -0
- data/vendor/wreq/src/client/ws/json.rs +99 -0
- data/vendor/wreq/src/client/ws/message.rs +453 -0
- data/vendor/wreq/src/client/ws.rs +714 -0
- data/vendor/wreq/src/client.rs +27 -0
- data/vendor/wreq/src/config.rs +140 -0
- data/vendor/wreq/src/cookie.rs +579 -0
- data/vendor/wreq/src/dns/gai.rs +249 -0
- data/vendor/wreq/src/dns/hickory.rs +78 -0
- data/vendor/wreq/src/dns/resolve.rs +180 -0
- data/vendor/wreq/src/dns.rs +69 -0
- data/vendor/wreq/src/error.rs +502 -0
- data/vendor/wreq/src/ext.rs +398 -0
- data/vendor/wreq/src/hash.rs +143 -0
- data/vendor/wreq/src/header.rs +506 -0
- data/vendor/wreq/src/into_uri.rs +187 -0
- data/vendor/wreq/src/lib.rs +586 -0
- data/vendor/wreq/src/proxy/mac.rs +82 -0
- data/vendor/wreq/src/proxy/matcher.rs +806 -0
- data/vendor/wreq/src/proxy/uds.rs +66 -0
- data/vendor/wreq/src/proxy/win.rs +31 -0
- data/vendor/wreq/src/proxy.rs +569 -0
- data/vendor/wreq/src/redirect.rs +575 -0
- data/vendor/wreq/src/retry.rs +198 -0
- data/vendor/wreq/src/sync.rs +129 -0
- data/vendor/wreq/src/tls/conn/cache.rs +123 -0
- data/vendor/wreq/src/tls/conn/cert_compression.rs +125 -0
- data/vendor/wreq/src/tls/conn/ext.rs +82 -0
- data/vendor/wreq/src/tls/conn/macros.rs +34 -0
- data/vendor/wreq/src/tls/conn/service.rs +138 -0
- data/vendor/wreq/src/tls/conn.rs +681 -0
- data/vendor/wreq/src/tls/keylog/handle.rs +64 -0
- data/vendor/wreq/src/tls/keylog.rs +99 -0
- data/vendor/wreq/src/tls/options.rs +464 -0
- data/vendor/wreq/src/tls/x509/identity.rs +122 -0
- data/vendor/wreq/src/tls/x509/parser.rs +71 -0
- data/vendor/wreq/src/tls/x509/store.rs +228 -0
- data/vendor/wreq/src/tls/x509.rs +68 -0
- data/vendor/wreq/src/tls.rs +154 -0
- data/vendor/wreq/src/trace.rs +55 -0
- data/vendor/wreq/src/util.rs +122 -0
- data/vendor/wreq/tests/badssl.rs +228 -0
- data/vendor/wreq/tests/brotli.rs +350 -0
- data/vendor/wreq/tests/client.rs +1098 -0
- data/vendor/wreq/tests/connector_layers.rs +227 -0
- data/vendor/wreq/tests/cookie.rs +306 -0
- data/vendor/wreq/tests/deflate.rs +347 -0
- data/vendor/wreq/tests/emulation.rs +260 -0
- data/vendor/wreq/tests/gzip.rs +347 -0
- data/vendor/wreq/tests/layers.rs +261 -0
- data/vendor/wreq/tests/multipart.rs +165 -0
- data/vendor/wreq/tests/proxy.rs +438 -0
- data/vendor/wreq/tests/redirect.rs +629 -0
- data/vendor/wreq/tests/retry.rs +135 -0
- data/vendor/wreq/tests/support/delay_server.rs +117 -0
- data/vendor/wreq/tests/support/error.rs +16 -0
- data/vendor/wreq/tests/support/layer.rs +183 -0
- data/vendor/wreq/tests/support/mod.rs +9 -0
- data/vendor/wreq/tests/support/server.rs +232 -0
- data/vendor/wreq/tests/timeouts.rs +281 -0
- data/vendor/wreq/tests/unix_socket.rs +135 -0
- data/vendor/wreq/tests/upgrade.rs +98 -0
- data/vendor/wreq/tests/zstd.rs +559 -0
- metadata +225 -0
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
use std::{
|
|
2
|
+
fmt::{self, Write as _},
|
|
3
|
+
mem::MaybeUninit,
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
use bytes::{Bytes, BytesMut};
|
|
7
|
+
use http::{
|
|
8
|
+
Method, StatusCode, Version,
|
|
9
|
+
header::{self, Entry, HeaderMap, HeaderName, HeaderValue},
|
|
10
|
+
};
|
|
11
|
+
use smallvec::{SmallVec, smallvec, smallvec_inline};
|
|
12
|
+
|
|
13
|
+
use crate::{
|
|
14
|
+
client::core::{
|
|
15
|
+
self, Error,
|
|
16
|
+
body::DecodedLength,
|
|
17
|
+
error::Parse,
|
|
18
|
+
ext::ReasonPhrase,
|
|
19
|
+
proto::{
|
|
20
|
+
BodyLength, MessageHead, RequestHead, RequestLine,
|
|
21
|
+
h1::{Encode, Encoder, Http1Transaction, ParseContext, ParseResult, ParsedMessage},
|
|
22
|
+
headers,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
config::RequestConfig,
|
|
26
|
+
header::OrigHeaderMap,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/// totally scientific
|
|
30
|
+
const AVERAGE_HEADER_SIZE: usize = 30;
|
|
31
|
+
pub(crate) const DEFAULT_MAX_HEADERS: usize = 100;
|
|
32
|
+
|
|
33
|
+
macro_rules! header_name {
|
|
34
|
+
($bytes:expr) => {{
|
|
35
|
+
{
|
|
36
|
+
match HeaderName::from_bytes($bytes) {
|
|
37
|
+
Ok(name) => name,
|
|
38
|
+
Err(e) => maybe_panic!(e),
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
macro_rules! header_value {
|
|
45
|
+
($bytes:expr) => {{
|
|
46
|
+
{
|
|
47
|
+
#[allow(unsafe_code)]
|
|
48
|
+
unsafe {
|
|
49
|
+
HeaderValue::from_maybe_shared_unchecked($bytes)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
macro_rules! maybe_panic {
|
|
56
|
+
($($arg:tt)*) => ({
|
|
57
|
+
let _err = ($($arg)*);
|
|
58
|
+
if cfg!(debug_assertions) {
|
|
59
|
+
panic!("{:?}", _err);
|
|
60
|
+
} else {
|
|
61
|
+
error!("Internal core error, please report {:?}", _err);
|
|
62
|
+
return Err(Parse::Internal)
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
pub(super) fn parse_headers<T>(
|
|
68
|
+
bytes: &mut BytesMut,
|
|
69
|
+
prev_len: Option<usize>,
|
|
70
|
+
ctx: ParseContext<'_>,
|
|
71
|
+
) -> ParseResult<T::Incoming>
|
|
72
|
+
where
|
|
73
|
+
T: Http1Transaction,
|
|
74
|
+
{
|
|
75
|
+
// If the buffer is empty, don't bother entering the span, it's just noise.
|
|
76
|
+
if bytes.is_empty() {
|
|
77
|
+
return Ok(None);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
trace_span!("parse_headers");
|
|
81
|
+
|
|
82
|
+
if let Some(prev_len) = prev_len {
|
|
83
|
+
if !is_complete_fast(bytes, prev_len) {
|
|
84
|
+
return Ok(None);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
T::parse(bytes, ctx)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// A fast scan for the end of a message.
|
|
92
|
+
/// Used when there was a partial read, to skip full parsing on a
|
|
93
|
+
/// a slow connection.
|
|
94
|
+
fn is_complete_fast(bytes: &[u8], prev_len: usize) -> bool {
|
|
95
|
+
let start = prev_len.saturating_sub(3);
|
|
96
|
+
let bytes = &bytes[start..];
|
|
97
|
+
|
|
98
|
+
for (i, b) in bytes.iter().copied().enumerate() {
|
|
99
|
+
if b == b'\r' {
|
|
100
|
+
if bytes[i + 1..].chunks(3).next() == Some(&b"\n\r\n"[..]) {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
} else if b == b'\n' && bytes.get(i + 1) == Some(&b'\n') {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
false
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
pub(super) fn encode_headers<T>(
|
|
112
|
+
enc: Encode<'_, T::Outgoing>,
|
|
113
|
+
dst: &mut Vec<u8>,
|
|
114
|
+
) -> core::Result<Encoder>
|
|
115
|
+
where
|
|
116
|
+
T: Http1Transaction,
|
|
117
|
+
{
|
|
118
|
+
trace_span!("encode_headers");
|
|
119
|
+
T::encode(enc, dst)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
pub(crate) enum Client {}
|
|
123
|
+
|
|
124
|
+
impl Http1Transaction for Client {
|
|
125
|
+
type Incoming = StatusCode;
|
|
126
|
+
type Outgoing = RequestLine;
|
|
127
|
+
#[cfg(feature = "tracing")]
|
|
128
|
+
const LOG: &'static str = "{role=client}";
|
|
129
|
+
|
|
130
|
+
fn parse(buf: &mut BytesMut, ctx: ParseContext<'_>) -> ParseResult<StatusCode> {
|
|
131
|
+
debug_assert!(!buf.is_empty(), "parse called with empty buf");
|
|
132
|
+
|
|
133
|
+
// Loop to skip information status code headers (100 Continue, etc).
|
|
134
|
+
loop {
|
|
135
|
+
let mut headers_indices: SmallVec<[MaybeUninit<HeaderIndices>; DEFAULT_MAX_HEADERS]> =
|
|
136
|
+
match ctx.h1_max_headers {
|
|
137
|
+
Some(cap) => smallvec![MaybeUninit::uninit(); cap],
|
|
138
|
+
None => smallvec_inline![MaybeUninit::uninit(); DEFAULT_MAX_HEADERS],
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
let (len, status, reason, version, headers_len) = {
|
|
142
|
+
let mut headers: SmallVec<
|
|
143
|
+
[MaybeUninit<httparse::Header<'_>>; DEFAULT_MAX_HEADERS],
|
|
144
|
+
> = match ctx.h1_max_headers {
|
|
145
|
+
Some(cap) => smallvec![MaybeUninit::uninit(); cap],
|
|
146
|
+
None => smallvec_inline![MaybeUninit::uninit(); DEFAULT_MAX_HEADERS],
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
trace!(bytes = buf.len(), "Response.parse");
|
|
150
|
+
|
|
151
|
+
let mut res = httparse::Response::new(&mut []);
|
|
152
|
+
let bytes = buf.as_ref();
|
|
153
|
+
match ctx.h1_parser_config.parse_response_with_uninit_headers(
|
|
154
|
+
&mut res,
|
|
155
|
+
bytes,
|
|
156
|
+
&mut headers,
|
|
157
|
+
) {
|
|
158
|
+
Ok(httparse::Status::Complete(len)) => {
|
|
159
|
+
trace!("Response.parse Complete({})", len);
|
|
160
|
+
let status = StatusCode::from_u16(res.code.unwrap())?;
|
|
161
|
+
|
|
162
|
+
let reason = {
|
|
163
|
+
let reason = res.reason.unwrap();
|
|
164
|
+
// Only save the reason phrase if it isn't the canonical reason
|
|
165
|
+
if Some(reason) != status.canonical_reason() {
|
|
166
|
+
Some(Bytes::copy_from_slice(reason.as_bytes()))
|
|
167
|
+
} else {
|
|
168
|
+
None
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
let version = if res.version.unwrap() == 1 {
|
|
173
|
+
Version::HTTP_11
|
|
174
|
+
} else {
|
|
175
|
+
Version::HTTP_10
|
|
176
|
+
};
|
|
177
|
+
record_header_indices(bytes, res.headers, &mut headers_indices)?;
|
|
178
|
+
let headers_len = res.headers.len();
|
|
179
|
+
(len, status, reason, version, headers_len)
|
|
180
|
+
}
|
|
181
|
+
Ok(httparse::Status::Partial) => return Ok(None),
|
|
182
|
+
Err(httparse::Error::Version) if ctx.h09_responses => {
|
|
183
|
+
trace!("Response.parse accepted HTTP/0.9 response");
|
|
184
|
+
|
|
185
|
+
(0, StatusCode::OK, None, Version::HTTP_09, 0)
|
|
186
|
+
}
|
|
187
|
+
Err(e) => return Err(e.into()),
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
let mut slice = buf.split_to(len);
|
|
192
|
+
|
|
193
|
+
if ctx
|
|
194
|
+
.h1_parser_config
|
|
195
|
+
.obsolete_multiline_headers_in_responses_are_allowed()
|
|
196
|
+
{
|
|
197
|
+
for header in &mut headers_indices[..headers_len] {
|
|
198
|
+
// SAFETY: array is valid up to `headers_len`
|
|
199
|
+
#[allow(unsafe_code)]
|
|
200
|
+
let header = unsafe { header.assume_init_mut() };
|
|
201
|
+
Client::obs_fold_line(&mut slice, header);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let slice = slice.freeze();
|
|
206
|
+
|
|
207
|
+
let mut headers = ctx.cached_headers.take().unwrap_or_default();
|
|
208
|
+
|
|
209
|
+
let mut keep_alive = version == Version::HTTP_11;
|
|
210
|
+
|
|
211
|
+
headers.reserve(headers_len);
|
|
212
|
+
for header in &headers_indices[..headers_len] {
|
|
213
|
+
// SAFETY: array is valid up to `headers_len`
|
|
214
|
+
#[allow(unsafe_code)]
|
|
215
|
+
let header = unsafe { header.assume_init_ref() };
|
|
216
|
+
let name = header_name!(&slice[header.name.0..header.name.1]);
|
|
217
|
+
let value = header_value!(slice.slice(header.value.0..header.value.1));
|
|
218
|
+
|
|
219
|
+
if let header::CONNECTION = name {
|
|
220
|
+
// keep_alive was previously set to default for Version
|
|
221
|
+
if keep_alive {
|
|
222
|
+
// HTTP/1.1
|
|
223
|
+
keep_alive = !headers::connection_close(&value);
|
|
224
|
+
} else {
|
|
225
|
+
// HTTP/1.0
|
|
226
|
+
keep_alive = headers::connection_keep_alive(&value);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
headers.append(name, value);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
let mut extensions = http::Extensions::default();
|
|
234
|
+
|
|
235
|
+
if let Some(reason) = reason {
|
|
236
|
+
// Safety: httparse ensures that only valid reason phrase bytes are present in this
|
|
237
|
+
// field.
|
|
238
|
+
let reason = ReasonPhrase::from_bytes_unchecked(reason);
|
|
239
|
+
extensions.insert(reason);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
let head = MessageHead {
|
|
243
|
+
version,
|
|
244
|
+
subject: status,
|
|
245
|
+
headers,
|
|
246
|
+
extensions,
|
|
247
|
+
};
|
|
248
|
+
if let Some((decode, is_upgrade)) = Client::decoder(&head, ctx.req_method)? {
|
|
249
|
+
return Ok(Some(ParsedMessage {
|
|
250
|
+
head,
|
|
251
|
+
decode,
|
|
252
|
+
expect_continue: false,
|
|
253
|
+
// a client upgrade means the connection can't be used
|
|
254
|
+
// again, as it is definitely upgrading.
|
|
255
|
+
keep_alive: keep_alive && !is_upgrade,
|
|
256
|
+
wants_upgrade: is_upgrade,
|
|
257
|
+
}));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Parsing a 1xx response could have consumed the buffer, check if
|
|
261
|
+
// it is empty now...
|
|
262
|
+
if buf.is_empty() {
|
|
263
|
+
return Ok(None);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
fn encode(msg: Encode<'_, Self::Outgoing>, dst: &mut Vec<u8>) -> core::Result<Encoder> {
|
|
269
|
+
trace!(
|
|
270
|
+
"Client::encode method={:?}, body={:?}",
|
|
271
|
+
msg.head.subject.0, msg.body
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
*msg.req_method = Some(msg.head.subject.0.clone());
|
|
275
|
+
|
|
276
|
+
let body = Client::set_length(msg.head, msg.body);
|
|
277
|
+
|
|
278
|
+
let init_cap = 30 + msg.head.headers.len() * AVERAGE_HEADER_SIZE;
|
|
279
|
+
dst.reserve(init_cap);
|
|
280
|
+
|
|
281
|
+
extend(dst, msg.head.subject.0.as_str().as_bytes());
|
|
282
|
+
extend(dst, b" ");
|
|
283
|
+
//TODO: add API to http::Uri to encode without std::fmt
|
|
284
|
+
let _ = write!(FastWrite(dst), "{} ", msg.head.subject.1);
|
|
285
|
+
|
|
286
|
+
match msg.head.version {
|
|
287
|
+
Version::HTTP_10 => extend(dst, b"HTTP/1.0"),
|
|
288
|
+
Version::HTTP_11 => extend(dst, b"HTTP/1.1"),
|
|
289
|
+
Version::HTTP_2 => {
|
|
290
|
+
debug!("request with HTTP2 version coerced to HTTP/1.1");
|
|
291
|
+
extend(dst, b"HTTP/1.1");
|
|
292
|
+
}
|
|
293
|
+
other => panic!("unexpected request version: {other:?}"),
|
|
294
|
+
}
|
|
295
|
+
extend(dst, b"\r\n");
|
|
296
|
+
|
|
297
|
+
if let Some(orig_headers) = RequestConfig::<OrigHeaderMap>::get(&msg.head.extensions) {
|
|
298
|
+
write_headers_original_case(&mut msg.head.headers, orig_headers, dst);
|
|
299
|
+
} else {
|
|
300
|
+
write_headers(&msg.head.headers, dst);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
extend(dst, b"\r\n");
|
|
304
|
+
msg.head.headers.clear(); //TODO: remove when switching to drain()
|
|
305
|
+
|
|
306
|
+
Ok(body)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
fn on_error(_err: &Error) -> Option<MessageHead<Self::Outgoing>> {
|
|
310
|
+
// we can't tell the server about any errors it creates
|
|
311
|
+
None
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
fn is_client() -> bool {
|
|
315
|
+
true
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
impl Client {
|
|
320
|
+
/// Returns Some(length, wants_upgrade) if successful.
|
|
321
|
+
///
|
|
322
|
+
/// Returns None if this message head should be skipped (like a 100 status).
|
|
323
|
+
fn decoder(
|
|
324
|
+
inc: &MessageHead<StatusCode>,
|
|
325
|
+
method: &mut Option<Method>,
|
|
326
|
+
) -> Result<Option<(DecodedLength, bool)>, Parse> {
|
|
327
|
+
// According to https://tools.ietf.org/html/rfc7230#section-3.3.3
|
|
328
|
+
// 1. HEAD responses, and Status 1xx, 204, and 304 cannot have a body.
|
|
329
|
+
// 2. Status 2xx to a CONNECT cannot have a body.
|
|
330
|
+
// 3. Transfer-Encoding: chunked has a chunked body.
|
|
331
|
+
// 4. If multiple differing Content-Length headers or invalid, close connection.
|
|
332
|
+
// 5. Content-Length header has a sized body.
|
|
333
|
+
// 6. (irrelevant to Response)
|
|
334
|
+
// 7. Read till EOF.
|
|
335
|
+
|
|
336
|
+
match inc.subject.as_u16() {
|
|
337
|
+
101 => {
|
|
338
|
+
return Ok(Some((DecodedLength::ZERO, true)));
|
|
339
|
+
}
|
|
340
|
+
100 | 102..=199 => {
|
|
341
|
+
trace!("ignoring informational response: {}", inc.subject.as_u16());
|
|
342
|
+
return Ok(None);
|
|
343
|
+
}
|
|
344
|
+
204 | 304 => return Ok(Some((DecodedLength::ZERO, false))),
|
|
345
|
+
_ => (),
|
|
346
|
+
}
|
|
347
|
+
match *method {
|
|
348
|
+
Some(Method::HEAD) => {
|
|
349
|
+
return Ok(Some((DecodedLength::ZERO, false)));
|
|
350
|
+
}
|
|
351
|
+
Some(Method::CONNECT) => {
|
|
352
|
+
if let 200..=299 = inc.subject.as_u16() {
|
|
353
|
+
return Ok(Some((DecodedLength::ZERO, true)));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
Some(_) => {}
|
|
357
|
+
None => {
|
|
358
|
+
trace!("Client::decoder is missing the Method");
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if inc.headers.contains_key(header::TRANSFER_ENCODING) {
|
|
363
|
+
// https://tools.ietf.org/html/rfc7230#section-3.3.3
|
|
364
|
+
// If Transfer-Encoding header is present, and 'chunked' is
|
|
365
|
+
// not the final encoding, and this is a Request, then it is
|
|
366
|
+
// malformed. A server should respond with 400 Bad Request.
|
|
367
|
+
return if inc.version == Version::HTTP_10 {
|
|
368
|
+
debug!("HTTP/1.0 cannot have Transfer-Encoding header");
|
|
369
|
+
Err(Parse::transfer_encoding_unexpected())
|
|
370
|
+
} else if headers::transfer_encoding_is_chunked(&inc.headers) {
|
|
371
|
+
Ok(Some((DecodedLength::CHUNKED, false)))
|
|
372
|
+
} else {
|
|
373
|
+
trace!("not chunked, read till eof");
|
|
374
|
+
Ok(Some((DecodedLength::CLOSE_DELIMITED, false)))
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if let Some(len) = headers::content_length_parse_all(&inc.headers) {
|
|
379
|
+
return Ok(Some((DecodedLength::checked_new(len)?, false)));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if inc.headers.contains_key(header::CONTENT_LENGTH) {
|
|
383
|
+
debug!("illegal Content-Length header");
|
|
384
|
+
return Err(Parse::content_length_invalid());
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
trace!("neither Transfer-Encoding nor Content-Length");
|
|
388
|
+
Ok(Some((DecodedLength::CLOSE_DELIMITED, false)))
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
fn set_length(head: &mut RequestHead, body: Option<BodyLength>) -> Encoder {
|
|
392
|
+
let body = if let Some(body) = body {
|
|
393
|
+
body
|
|
394
|
+
} else {
|
|
395
|
+
head.headers.remove(header::TRANSFER_ENCODING);
|
|
396
|
+
return Encoder::length(0);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// HTTP/1.0 doesn't know about chunked
|
|
400
|
+
let can_chunked = head.version == Version::HTTP_11;
|
|
401
|
+
let headers = &mut head.headers;
|
|
402
|
+
|
|
403
|
+
// If the user already set specific headers, we should respect them, regardless
|
|
404
|
+
// of what the Body knows about itself. They set them for a reason.
|
|
405
|
+
|
|
406
|
+
// Because of the borrow checker, we can't check the for an existing
|
|
407
|
+
// Content-Length header while holding an `Entry` for the Transfer-Encoding
|
|
408
|
+
// header, so unfortunately, we must do the check here, first.
|
|
409
|
+
|
|
410
|
+
let existing_con_len = headers::content_length_parse_all(headers);
|
|
411
|
+
let mut should_remove_con_len = false;
|
|
412
|
+
|
|
413
|
+
if !can_chunked {
|
|
414
|
+
// Chunked isn't legal, so if it is set, we need to remove it.
|
|
415
|
+
if headers.remove(header::TRANSFER_ENCODING).is_some() {
|
|
416
|
+
trace!("removing illegal transfer-encoding header");
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return if let Some(len) = existing_con_len {
|
|
420
|
+
Encoder::length(len)
|
|
421
|
+
} else if let BodyLength::Known(len) = body {
|
|
422
|
+
set_content_length(headers, len)
|
|
423
|
+
} else {
|
|
424
|
+
// HTTP/1.0 client requests without a content-length
|
|
425
|
+
// cannot have any body at all.
|
|
426
|
+
Encoder::length(0)
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// If the user set a transfer-encoding, respect that. Let's just
|
|
431
|
+
// make sure `chunked` is the final encoding.
|
|
432
|
+
let encoder = match headers.entry(header::TRANSFER_ENCODING) {
|
|
433
|
+
Entry::Occupied(te) => {
|
|
434
|
+
should_remove_con_len = true;
|
|
435
|
+
if headers::is_chunked(te.iter()) {
|
|
436
|
+
Some(Encoder::chunked())
|
|
437
|
+
} else {
|
|
438
|
+
warn!("user provided transfer-encoding does not end in 'chunked'");
|
|
439
|
+
|
|
440
|
+
// There's a Transfer-Encoding, but it doesn't end in 'chunked'!
|
|
441
|
+
// An example that could trigger this:
|
|
442
|
+
//
|
|
443
|
+
// Transfer-Encoding: gzip
|
|
444
|
+
//
|
|
445
|
+
// This can be bad, depending on if this is a request or a
|
|
446
|
+
// response.
|
|
447
|
+
//
|
|
448
|
+
// - A request is illegal if there is a `Transfer-Encoding` but it doesn't end
|
|
449
|
+
// in `chunked`.
|
|
450
|
+
// - A response that has `Transfer-Encoding` but doesn't end in `chunked` isn't
|
|
451
|
+
// illegal, it just forces this to be close-delimited.
|
|
452
|
+
//
|
|
453
|
+
// We can try to repair this, by adding `chunked` ourselves.
|
|
454
|
+
|
|
455
|
+
headers::add_chunked(te);
|
|
456
|
+
Some(Encoder::chunked())
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
Entry::Vacant(te) => {
|
|
460
|
+
if let Some(len) = existing_con_len {
|
|
461
|
+
Some(Encoder::length(len))
|
|
462
|
+
} else if let BodyLength::Unknown = body {
|
|
463
|
+
// GET, HEAD, and CONNECT almost never have bodies.
|
|
464
|
+
//
|
|
465
|
+
// So instead of sending a "chunked" body with a 0-chunk,
|
|
466
|
+
// assume no body here. If you *must* send a body,
|
|
467
|
+
// set the headers explicitly.
|
|
468
|
+
match head.subject.0 {
|
|
469
|
+
Method::GET | Method::HEAD | Method::CONNECT => Some(Encoder::length(0)),
|
|
470
|
+
_ => {
|
|
471
|
+
te.insert(HeaderValue::from_static("chunked"));
|
|
472
|
+
Some(Encoder::chunked())
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
} else {
|
|
476
|
+
None
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
let encoder = encoder.map(|enc| {
|
|
482
|
+
if enc.is_chunked() {
|
|
483
|
+
// Parse Trailer header values into HeaderNames.
|
|
484
|
+
// Each Trailer header value may contain comma-separated names.
|
|
485
|
+
// HeaderName normalizes to lowercase, enabling case-insensitive matching.
|
|
486
|
+
let allowed_trailer_fields: Vec<HeaderName> = headers
|
|
487
|
+
.get_all(header::TRAILER)
|
|
488
|
+
.iter()
|
|
489
|
+
.filter_map(|hv| hv.to_str().ok())
|
|
490
|
+
.flat_map(|s| s.split(','))
|
|
491
|
+
.filter_map(|s| HeaderName::from_bytes(s.trim().as_bytes()).ok())
|
|
492
|
+
.collect();
|
|
493
|
+
|
|
494
|
+
if !allowed_trailer_fields.is_empty() {
|
|
495
|
+
return enc.into_chunked_with_trailing_fields(allowed_trailer_fields);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
enc
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
// This is because we need a second mutable borrow to remove
|
|
503
|
+
// content-length header.
|
|
504
|
+
if let Some(encoder) = encoder {
|
|
505
|
+
if should_remove_con_len && existing_con_len.is_some() {
|
|
506
|
+
headers.remove(header::CONTENT_LENGTH);
|
|
507
|
+
}
|
|
508
|
+
return encoder;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// User didn't set transfer-encoding, AND we know body length,
|
|
512
|
+
// so we can just set the Content-Length automatically.
|
|
513
|
+
|
|
514
|
+
let len = if let BodyLength::Known(len) = body {
|
|
515
|
+
len
|
|
516
|
+
} else {
|
|
517
|
+
unreachable!("BodyLength::Unknown would set chunked");
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
set_content_length(headers, len)
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
fn obs_fold_line(all: &mut [u8], idx: &mut HeaderIndices) {
|
|
524
|
+
// If the value has obs-folded text, then in-place shift the bytes out
|
|
525
|
+
// of here.
|
|
526
|
+
//
|
|
527
|
+
// https://httpwg.org/specs/rfc9112.html#line.folding
|
|
528
|
+
//
|
|
529
|
+
// > A user agent that receives an obs-fold MUST replace each received
|
|
530
|
+
// > obs-fold with one or more SP octets prior to interpreting the
|
|
531
|
+
// > field value.
|
|
532
|
+
//
|
|
533
|
+
// This means strings like "\r\n\t foo" must replace the "\r\n\t " with
|
|
534
|
+
// a single space.
|
|
535
|
+
|
|
536
|
+
let buf = &mut all[idx.value.0..idx.value.1];
|
|
537
|
+
|
|
538
|
+
// look for a newline, otherwise bail out
|
|
539
|
+
let first_nl = match buf.iter().position(|b| *b == b'\n') {
|
|
540
|
+
Some(i) => i,
|
|
541
|
+
None => return,
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
// not on standard slices because whatever, sigh
|
|
545
|
+
fn trim_start(mut s: &[u8]) -> &[u8] {
|
|
546
|
+
while let [first, rest @ ..] = s {
|
|
547
|
+
if first.is_ascii_whitespace() {
|
|
548
|
+
s = rest;
|
|
549
|
+
} else {
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
s
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
fn trim_end(mut s: &[u8]) -> &[u8] {
|
|
557
|
+
while let [rest @ .., last] = s {
|
|
558
|
+
if last.is_ascii_whitespace() {
|
|
559
|
+
s = rest;
|
|
560
|
+
} else {
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
s
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
fn trim(s: &[u8]) -> &[u8] {
|
|
568
|
+
trim_start(trim_end(s))
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// TODO(perf): we could do the moves in-place, but this is so uncommon
|
|
572
|
+
// that it shouldn't matter.
|
|
573
|
+
let mut unfolded = trim_end(&buf[..first_nl]).to_vec();
|
|
574
|
+
for line in buf[first_nl + 1..].split(|b| *b == b'\n') {
|
|
575
|
+
unfolded.push(b' ');
|
|
576
|
+
unfolded.extend_from_slice(trim(line));
|
|
577
|
+
}
|
|
578
|
+
buf[..unfolded.len()].copy_from_slice(&unfolded);
|
|
579
|
+
idx.value.1 = idx.value.0 + unfolded.len();
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
fn set_content_length(headers: &mut HeaderMap, len: u64) -> Encoder {
|
|
584
|
+
// At this point, there should not be a valid Content-Length
|
|
585
|
+
// header. However, since we'll be indexing in anyways, we can
|
|
586
|
+
// warn the user if there was an existing illegal header.
|
|
587
|
+
//
|
|
588
|
+
// Or at least, we can in theory. It's actually a little bit slower,
|
|
589
|
+
// so perhaps only do that while the user is developing/testing.
|
|
590
|
+
|
|
591
|
+
if cfg!(debug_assertions) {
|
|
592
|
+
match headers.entry(header::CONTENT_LENGTH) {
|
|
593
|
+
Entry::Occupied(mut cl) => {
|
|
594
|
+
// Internal sanity check, we should have already determined
|
|
595
|
+
// that the header was illegal before calling this function.
|
|
596
|
+
debug_assert!(headers::content_length_parse_all_values(cl.iter()).is_none());
|
|
597
|
+
// Uh oh, the user set `Content-Length` headers, but set bad ones.
|
|
598
|
+
// This would be an illegal message anyways, so let's try to repair
|
|
599
|
+
// with our known good length.
|
|
600
|
+
error!("user provided content-length header was invalid");
|
|
601
|
+
|
|
602
|
+
cl.insert(HeaderValue::from(len));
|
|
603
|
+
Encoder::length(len)
|
|
604
|
+
}
|
|
605
|
+
Entry::Vacant(cl) => {
|
|
606
|
+
cl.insert(HeaderValue::from(len));
|
|
607
|
+
Encoder::length(len)
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
} else {
|
|
611
|
+
headers.insert(header::CONTENT_LENGTH, HeaderValue::from(len));
|
|
612
|
+
Encoder::length(len)
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
#[derive(Clone, Copy)]
|
|
617
|
+
struct HeaderIndices {
|
|
618
|
+
name: (usize, usize),
|
|
619
|
+
value: (usize, usize),
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
fn record_header_indices(
|
|
623
|
+
bytes: &[u8],
|
|
624
|
+
headers: &[httparse::Header<'_>],
|
|
625
|
+
indices: &mut [MaybeUninit<HeaderIndices>],
|
|
626
|
+
) -> Result<(), Parse> {
|
|
627
|
+
let bytes_ptr = bytes.as_ptr() as usize;
|
|
628
|
+
|
|
629
|
+
for (header, indices) in headers.iter().zip(indices.iter_mut()) {
|
|
630
|
+
if header.name.len() >= (1 << 16) {
|
|
631
|
+
debug!("header name larger than 64kb: {:?}", header.name);
|
|
632
|
+
return Err(Parse::TooLarge);
|
|
633
|
+
}
|
|
634
|
+
let name_start = header.name.as_ptr() as usize - bytes_ptr;
|
|
635
|
+
let name_end = name_start + header.name.len();
|
|
636
|
+
let value_start = header.value.as_ptr() as usize - bytes_ptr;
|
|
637
|
+
let value_end = value_start + header.value.len();
|
|
638
|
+
|
|
639
|
+
indices.write(HeaderIndices {
|
|
640
|
+
name: (name_start, name_end),
|
|
641
|
+
value: (value_start, value_end),
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
Ok(())
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
pub(crate) fn write_headers(headers: &HeaderMap, dst: &mut Vec<u8>) {
|
|
649
|
+
for (name, value) in headers {
|
|
650
|
+
extend(dst, name.as_ref());
|
|
651
|
+
extend(dst, b": ");
|
|
652
|
+
extend(dst, value.as_bytes());
|
|
653
|
+
extend(dst, b"\r\n");
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
fn write_headers_original_case(
|
|
658
|
+
headers: &mut HeaderMap,
|
|
659
|
+
orig_headers: &OrigHeaderMap,
|
|
660
|
+
dst: &mut Vec<u8>,
|
|
661
|
+
) {
|
|
662
|
+
orig_headers.sort_headers_for_each(headers, |orig_name, value| {
|
|
663
|
+
extend(dst, orig_name);
|
|
664
|
+
|
|
665
|
+
// Wanted for curl test cases that send `X-Custom-Header:\r\n`
|
|
666
|
+
if value.is_empty() {
|
|
667
|
+
extend(dst, b":\r\n");
|
|
668
|
+
} else {
|
|
669
|
+
extend(dst, b": ");
|
|
670
|
+
extend(dst, value.as_bytes());
|
|
671
|
+
extend(dst, b"\r\n");
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
struct FastWrite<'a>(&'a mut Vec<u8>);
|
|
677
|
+
|
|
678
|
+
impl fmt::Write for FastWrite<'_> {
|
|
679
|
+
#[inline]
|
|
680
|
+
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
681
|
+
extend(self.0, s.as_bytes());
|
|
682
|
+
Ok(())
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
#[inline]
|
|
686
|
+
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
|
|
687
|
+
fmt::write(self, args)
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
#[inline]
|
|
692
|
+
fn extend(dst: &mut Vec<u8>, data: &[u8]) {
|
|
693
|
+
dst.extend_from_slice(data);
|
|
694
|
+
}
|