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.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/Cargo.lock +2688 -0
  3. data/Cargo.toml +6 -0
  4. data/README.md +179 -0
  5. data/ext/wreq_rb/Cargo.toml +39 -0
  6. data/ext/wreq_rb/extconf.rb +22 -0
  7. data/ext/wreq_rb/src/client.rs +565 -0
  8. data/ext/wreq_rb/src/error.rs +25 -0
  9. data/ext/wreq_rb/src/lib.rs +20 -0
  10. data/ext/wreq_rb/src/response.rs +132 -0
  11. data/lib/wreq-rb/version.rb +5 -0
  12. data/lib/wreq-rb.rb +17 -0
  13. data/patches/0001-add-transfer-size-tracking.patch +292 -0
  14. data/vendor/wreq/Cargo.toml +306 -0
  15. data/vendor/wreq/LICENSE +202 -0
  16. data/vendor/wreq/README.md +122 -0
  17. data/vendor/wreq/examples/cert_store.rs +77 -0
  18. data/vendor/wreq/examples/connect_via_lower_priority_tokio_runtime.rs +258 -0
  19. data/vendor/wreq/examples/emulation.rs +118 -0
  20. data/vendor/wreq/examples/form.rs +14 -0
  21. data/vendor/wreq/examples/http1_websocket.rs +37 -0
  22. data/vendor/wreq/examples/http2_websocket.rs +45 -0
  23. data/vendor/wreq/examples/json_dynamic.rs +41 -0
  24. data/vendor/wreq/examples/json_typed.rs +47 -0
  25. data/vendor/wreq/examples/keylog.rs +16 -0
  26. data/vendor/wreq/examples/request_with_emulation.rs +115 -0
  27. data/vendor/wreq/examples/request_with_interface.rs +37 -0
  28. data/vendor/wreq/examples/request_with_local_address.rs +16 -0
  29. data/vendor/wreq/examples/request_with_proxy.rs +13 -0
  30. data/vendor/wreq/examples/request_with_redirect.rs +22 -0
  31. data/vendor/wreq/examples/request_with_version.rs +15 -0
  32. data/vendor/wreq/examples/tor_socks.rs +24 -0
  33. data/vendor/wreq/examples/unix_socket.rs +33 -0
  34. data/vendor/wreq/src/client/body.rs +304 -0
  35. data/vendor/wreq/src/client/conn/conn.rs +231 -0
  36. data/vendor/wreq/src/client/conn/connector.rs +549 -0
  37. data/vendor/wreq/src/client/conn/http.rs +1023 -0
  38. data/vendor/wreq/src/client/conn/proxy/socks.rs +233 -0
  39. data/vendor/wreq/src/client/conn/proxy/tunnel.rs +260 -0
  40. data/vendor/wreq/src/client/conn/proxy.rs +39 -0
  41. data/vendor/wreq/src/client/conn/tls_info.rs +98 -0
  42. data/vendor/wreq/src/client/conn/uds.rs +44 -0
  43. data/vendor/wreq/src/client/conn/verbose.rs +149 -0
  44. data/vendor/wreq/src/client/conn.rs +323 -0
  45. data/vendor/wreq/src/client/core/body/incoming.rs +485 -0
  46. data/vendor/wreq/src/client/core/body/length.rs +118 -0
  47. data/vendor/wreq/src/client/core/body.rs +34 -0
  48. data/vendor/wreq/src/client/core/common/buf.rs +149 -0
  49. data/vendor/wreq/src/client/core/common/rewind.rs +141 -0
  50. data/vendor/wreq/src/client/core/common/watch.rs +76 -0
  51. data/vendor/wreq/src/client/core/common.rs +3 -0
  52. data/vendor/wreq/src/client/core/conn/http1.rs +342 -0
  53. data/vendor/wreq/src/client/core/conn/http2.rs +307 -0
  54. data/vendor/wreq/src/client/core/conn.rs +11 -0
  55. data/vendor/wreq/src/client/core/dispatch.rs +299 -0
  56. data/vendor/wreq/src/client/core/error.rs +435 -0
  57. data/vendor/wreq/src/client/core/ext.rs +201 -0
  58. data/vendor/wreq/src/client/core/http1.rs +178 -0
  59. data/vendor/wreq/src/client/core/http2.rs +483 -0
  60. data/vendor/wreq/src/client/core/proto/h1/conn.rs +988 -0
  61. data/vendor/wreq/src/client/core/proto/h1/decode.rs +1170 -0
  62. data/vendor/wreq/src/client/core/proto/h1/dispatch.rs +684 -0
  63. data/vendor/wreq/src/client/core/proto/h1/encode.rs +580 -0
  64. data/vendor/wreq/src/client/core/proto/h1/io.rs +879 -0
  65. data/vendor/wreq/src/client/core/proto/h1/role.rs +694 -0
  66. data/vendor/wreq/src/client/core/proto/h1.rs +104 -0
  67. data/vendor/wreq/src/client/core/proto/h2/client.rs +650 -0
  68. data/vendor/wreq/src/client/core/proto/h2/ping.rs +539 -0
  69. data/vendor/wreq/src/client/core/proto/h2.rs +379 -0
  70. data/vendor/wreq/src/client/core/proto/headers.rs +138 -0
  71. data/vendor/wreq/src/client/core/proto.rs +58 -0
  72. data/vendor/wreq/src/client/core/rt/bounds.rs +57 -0
  73. data/vendor/wreq/src/client/core/rt/timer.rs +150 -0
  74. data/vendor/wreq/src/client/core/rt/tokio.rs +99 -0
  75. data/vendor/wreq/src/client/core/rt.rs +25 -0
  76. data/vendor/wreq/src/client/core/upgrade.rs +267 -0
  77. data/vendor/wreq/src/client/core.rs +16 -0
  78. data/vendor/wreq/src/client/emulation.rs +161 -0
  79. data/vendor/wreq/src/client/http/client/error.rs +142 -0
  80. data/vendor/wreq/src/client/http/client/exec.rs +29 -0
  81. data/vendor/wreq/src/client/http/client/extra.rs +77 -0
  82. data/vendor/wreq/src/client/http/client/lazy.rs +79 -0
  83. data/vendor/wreq/src/client/http/client/pool.rs +1105 -0
  84. data/vendor/wreq/src/client/http/client/util.rs +104 -0
  85. data/vendor/wreq/src/client/http/client.rs +1003 -0
  86. data/vendor/wreq/src/client/http/future.rs +99 -0
  87. data/vendor/wreq/src/client/http.rs +1629 -0
  88. data/vendor/wreq/src/client/layer/config/options.rs +156 -0
  89. data/vendor/wreq/src/client/layer/config.rs +116 -0
  90. data/vendor/wreq/src/client/layer/cookie.rs +161 -0
  91. data/vendor/wreq/src/client/layer/decoder.rs +139 -0
  92. data/vendor/wreq/src/client/layer/redirect/future.rs +270 -0
  93. data/vendor/wreq/src/client/layer/redirect/policy.rs +63 -0
  94. data/vendor/wreq/src/client/layer/redirect.rs +145 -0
  95. data/vendor/wreq/src/client/layer/retry/classify.rs +105 -0
  96. data/vendor/wreq/src/client/layer/retry/scope.rs +51 -0
  97. data/vendor/wreq/src/client/layer/retry.rs +151 -0
  98. data/vendor/wreq/src/client/layer/timeout/body.rs +233 -0
  99. data/vendor/wreq/src/client/layer/timeout/future.rs +90 -0
  100. data/vendor/wreq/src/client/layer/timeout.rs +177 -0
  101. data/vendor/wreq/src/client/layer.rs +15 -0
  102. data/vendor/wreq/src/client/multipart.rs +717 -0
  103. data/vendor/wreq/src/client/request.rs +818 -0
  104. data/vendor/wreq/src/client/response.rs +534 -0
  105. data/vendor/wreq/src/client/ws/json.rs +99 -0
  106. data/vendor/wreq/src/client/ws/message.rs +453 -0
  107. data/vendor/wreq/src/client/ws.rs +714 -0
  108. data/vendor/wreq/src/client.rs +27 -0
  109. data/vendor/wreq/src/config.rs +140 -0
  110. data/vendor/wreq/src/cookie.rs +579 -0
  111. data/vendor/wreq/src/dns/gai.rs +249 -0
  112. data/vendor/wreq/src/dns/hickory.rs +78 -0
  113. data/vendor/wreq/src/dns/resolve.rs +180 -0
  114. data/vendor/wreq/src/dns.rs +69 -0
  115. data/vendor/wreq/src/error.rs +502 -0
  116. data/vendor/wreq/src/ext.rs +398 -0
  117. data/vendor/wreq/src/hash.rs +143 -0
  118. data/vendor/wreq/src/header.rs +506 -0
  119. data/vendor/wreq/src/into_uri.rs +187 -0
  120. data/vendor/wreq/src/lib.rs +586 -0
  121. data/vendor/wreq/src/proxy/mac.rs +82 -0
  122. data/vendor/wreq/src/proxy/matcher.rs +806 -0
  123. data/vendor/wreq/src/proxy/uds.rs +66 -0
  124. data/vendor/wreq/src/proxy/win.rs +31 -0
  125. data/vendor/wreq/src/proxy.rs +569 -0
  126. data/vendor/wreq/src/redirect.rs +575 -0
  127. data/vendor/wreq/src/retry.rs +198 -0
  128. data/vendor/wreq/src/sync.rs +129 -0
  129. data/vendor/wreq/src/tls/conn/cache.rs +123 -0
  130. data/vendor/wreq/src/tls/conn/cert_compression.rs +125 -0
  131. data/vendor/wreq/src/tls/conn/ext.rs +82 -0
  132. data/vendor/wreq/src/tls/conn/macros.rs +34 -0
  133. data/vendor/wreq/src/tls/conn/service.rs +138 -0
  134. data/vendor/wreq/src/tls/conn.rs +681 -0
  135. data/vendor/wreq/src/tls/keylog/handle.rs +64 -0
  136. data/vendor/wreq/src/tls/keylog.rs +99 -0
  137. data/vendor/wreq/src/tls/options.rs +464 -0
  138. data/vendor/wreq/src/tls/x509/identity.rs +122 -0
  139. data/vendor/wreq/src/tls/x509/parser.rs +71 -0
  140. data/vendor/wreq/src/tls/x509/store.rs +228 -0
  141. data/vendor/wreq/src/tls/x509.rs +68 -0
  142. data/vendor/wreq/src/tls.rs +154 -0
  143. data/vendor/wreq/src/trace.rs +55 -0
  144. data/vendor/wreq/src/util.rs +122 -0
  145. data/vendor/wreq/tests/badssl.rs +228 -0
  146. data/vendor/wreq/tests/brotli.rs +350 -0
  147. data/vendor/wreq/tests/client.rs +1098 -0
  148. data/vendor/wreq/tests/connector_layers.rs +227 -0
  149. data/vendor/wreq/tests/cookie.rs +306 -0
  150. data/vendor/wreq/tests/deflate.rs +347 -0
  151. data/vendor/wreq/tests/emulation.rs +260 -0
  152. data/vendor/wreq/tests/gzip.rs +347 -0
  153. data/vendor/wreq/tests/layers.rs +261 -0
  154. data/vendor/wreq/tests/multipart.rs +165 -0
  155. data/vendor/wreq/tests/proxy.rs +438 -0
  156. data/vendor/wreq/tests/redirect.rs +629 -0
  157. data/vendor/wreq/tests/retry.rs +135 -0
  158. data/vendor/wreq/tests/support/delay_server.rs +117 -0
  159. data/vendor/wreq/tests/support/error.rs +16 -0
  160. data/vendor/wreq/tests/support/layer.rs +183 -0
  161. data/vendor/wreq/tests/support/mod.rs +9 -0
  162. data/vendor/wreq/tests/support/server.rs +232 -0
  163. data/vendor/wreq/tests/timeouts.rs +281 -0
  164. data/vendor/wreq/tests/unix_socket.rs +135 -0
  165. data/vendor/wreq/tests/upgrade.rs +98 -0
  166. data/vendor/wreq/tests/zstd.rs +559 -0
  167. metadata +225 -0
@@ -0,0 +1,806 @@
1
+ //! Proxy matchers
2
+ //!
3
+ //! This module contains different matchers to configure rules for when a proxy
4
+ //! should be used, and if so, with what arguments.
5
+ //!
6
+ //! A [`Matcher`] can be constructed either using environment variables, or
7
+ //! a [`Matcher::builder()`].
8
+ //!
9
+ //! Once constructed, the `Matcher` can be asked if it intercepts a `Uri` by
10
+ //! calling [`Matcher::intercept()`].
11
+ //!
12
+ //! An [`Intercept`] includes the destination for the proxy, and any parsed
13
+ //! authentication to be used.
14
+
15
+ use std::net::IpAddr;
16
+ #[cfg(unix)]
17
+ use std::{path::Path, sync::Arc};
18
+
19
+ use bytes::Bytes;
20
+ use http::{
21
+ HeaderMap, Uri,
22
+ header::HeaderValue,
23
+ uri::{Authority, Scheme},
24
+ };
25
+ use ipnet::IpNet;
26
+ use percent_encoding::percent_decode_str;
27
+
28
+ use self::builder::IntoValue;
29
+ use super::{Extra, Intercepted};
30
+ use crate::ext::UriExt;
31
+
32
+ /// A proxy matcher, usually built from environment variables.
33
+ #[derive(Debug, Clone, PartialEq, Eq, Hash)]
34
+ pub struct Matcher {
35
+ http: Option<Intercept>,
36
+ https: Option<Intercept>,
37
+ no: NoProxy,
38
+ #[cfg(unix)]
39
+ unix: Option<Arc<Path>>,
40
+ }
41
+
42
+ /// A matched proxy,
43
+ ///
44
+ /// This is returned by a matcher if a proxy should be used.
45
+ #[derive(Debug, Clone, PartialEq, Eq, Hash)]
46
+ pub struct Intercept {
47
+ uri: Uri,
48
+ auth: Auth,
49
+ extra: Extra,
50
+ }
51
+
52
+ /// A builder to create a [`Matcher`].
53
+ ///
54
+ /// Construct with [`Matcher::builder()`].
55
+ #[derive(Default)]
56
+ pub struct Builder {
57
+ pub(super) is_cgi: bool,
58
+ pub(super) all: String,
59
+ pub(super) http: String,
60
+ pub(super) https: String,
61
+ pub(super) no: String,
62
+ #[cfg(unix)]
63
+ pub(super) unix: Option<Arc<Path>>,
64
+ }
65
+
66
+ #[derive(Debug, Clone, PartialEq, Eq, Hash)]
67
+ pub enum Auth {
68
+ Empty,
69
+ Basic(HeaderValue),
70
+ Raw(Bytes, Bytes),
71
+ }
72
+
73
+ /// A filter for proxy matchers.
74
+ ///
75
+ /// This type is based off the `NO_PROXY` rules used by curl.
76
+ #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
77
+ struct NoProxy {
78
+ ips: IpMatcher,
79
+ domains: DomainMatcher,
80
+ }
81
+
82
+ #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
83
+ struct DomainMatcher(Vec<String>);
84
+
85
+ #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
86
+ struct IpMatcher(Vec<Ip>);
87
+
88
+ #[derive(Clone, Debug, PartialEq, Eq, Hash)]
89
+ enum Ip {
90
+ Address(IpAddr),
91
+ Network(IpNet),
92
+ }
93
+
94
+ // ===== impl Matcher =====
95
+
96
+ impl Matcher {
97
+ /// Create a matcher from the environment or system.
98
+ ///
99
+ /// This checks the same environment variables as `from_env()`, and if not
100
+ /// set, checks the system configuration for values for the OS.
101
+ ///
102
+ /// This constructor is always available, but if the `client-proxy-system`
103
+ /// feature is enabled, it will check more configuration. Use this
104
+ /// constructor if you want to allow users to optionally enable more, or
105
+ /// use `from_env` if you do not want the values to change based on an
106
+ /// enabled feature.
107
+ pub fn from_system() -> Self {
108
+ Builder::from_system().build(Extra::default())
109
+ }
110
+
111
+ /// Start a builder to configure a matcher.
112
+ pub fn builder() -> Builder {
113
+ Builder::default()
114
+ }
115
+
116
+ /// Check if the destination should be intercepted by a proxy.
117
+ ///
118
+ /// If the proxy rules match the destination, a new `Uri` will be returned
119
+ /// to connect to.
120
+ pub fn intercept(&self, dst: &Uri) -> Option<Intercepted> {
121
+ // if unix sockets are configured, check them first
122
+ #[cfg(unix)]
123
+ if let Some(unix) = &self.unix {
124
+ return Some(Intercepted::Unix(unix.clone()));
125
+ }
126
+
127
+ // TODO(perf): don't need to check `no` if below doesn't match...
128
+ if self.no.contains(dst.host()?) {
129
+ return None;
130
+ }
131
+ if dst.is_http() {
132
+ return self.http.clone().map(Intercepted::Proxy);
133
+ }
134
+
135
+ if dst.is_https() {
136
+ return self.https.clone().map(Intercepted::Proxy);
137
+ }
138
+
139
+ None
140
+ }
141
+ }
142
+
143
+ // ===== impl Intercept =====
144
+
145
+ impl Intercept {
146
+ #[inline]
147
+ pub(crate) fn uri(&self) -> &Uri {
148
+ &self.uri
149
+ }
150
+
151
+ pub(crate) fn basic_auth(&self) -> Option<&HeaderValue> {
152
+ if let Some(ref val) = self.extra.auth {
153
+ return Some(val);
154
+ }
155
+
156
+ if let Auth::Basic(ref val) = self.auth {
157
+ Some(val)
158
+ } else {
159
+ None
160
+ }
161
+ }
162
+
163
+ #[inline]
164
+ pub(crate) fn custom_headers(&self) -> Option<&HeaderMap> {
165
+ self.extra.misc.as_ref()
166
+ }
167
+
168
+ #[cfg(feature = "socks")]
169
+ pub(crate) fn raw_auth(&self) -> Option<(Bytes, Bytes)> {
170
+ if let Auth::Raw(ref u, ref p) = self.auth {
171
+ Some((u.clone(), p.clone()))
172
+ } else {
173
+ None
174
+ }
175
+ }
176
+ }
177
+
178
+ // ===== impl Builder =====
179
+
180
+ impl Builder {
181
+ fn from_env() -> Self {
182
+ Builder {
183
+ is_cgi: std::env::var_os("REQUEST_METHOD").is_some(),
184
+ all: get_first_env(&["ALL_PROXY", "all_proxy"]),
185
+ http: get_first_env(&["HTTP_PROXY", "http_proxy"]),
186
+ https: get_first_env(&["HTTPS_PROXY", "https_proxy"]),
187
+ no: get_first_env(&["NO_PROXY", "no_proxy"]),
188
+ #[cfg(unix)]
189
+ unix: None,
190
+ }
191
+ }
192
+
193
+ fn from_system() -> Self {
194
+ #[allow(unused_mut)]
195
+ let mut builder = Self::from_env();
196
+
197
+ #[cfg(all(target_os = "macos", feature = "system-proxy"))]
198
+ super::mac::with_system(&mut builder);
199
+
200
+ #[cfg(all(windows, feature = "system-proxy"))]
201
+ super::win::with_system(&mut builder);
202
+
203
+ builder
204
+ }
205
+
206
+ /// Set the target proxy for all destinations.
207
+ pub fn all<S>(mut self, val: S) -> Self
208
+ where
209
+ S: IntoValue,
210
+ {
211
+ self.all = val.into_value();
212
+ self
213
+ }
214
+
215
+ /// Set the target proxy for HTTP destinations.
216
+ pub fn http<S>(mut self, val: S) -> Self
217
+ where
218
+ S: IntoValue,
219
+ {
220
+ self.http = val.into_value();
221
+ self
222
+ }
223
+
224
+ /// Set the target proxy for HTTPS destinations.
225
+ pub fn https<S>(mut self, val: S) -> Self
226
+ where
227
+ S: IntoValue,
228
+ {
229
+ self.https = val.into_value();
230
+ self
231
+ }
232
+
233
+ /// Set the "no" proxy filter.
234
+ ///
235
+ /// The rules are as follows:
236
+ /// * Entries are expected to be comma-separated (whitespace between entries is ignored)
237
+ /// * IP addresses (both IPv4 and IPv6) are allowed, as are optional subnet masks (by adding
238
+ /// /size, for example "`192.168.1.0/24`").
239
+ /// * An entry "`*`" matches all hostnames (this is the only wildcard allowed)
240
+ /// * Any other entry is considered a domain name (and may contain a leading dot, for example
241
+ /// `google.com` and `.google.com` are equivalent) and would match both that domain AND all
242
+ /// subdomains.
243
+ ///
244
+ /// For example, if `"NO_PROXY=google.com, 192.168.1.0/24"` was set, all of the following would
245
+ /// match (and therefore would bypass the proxy):
246
+ /// * `http://google.com/`
247
+ /// * `http://www.google.com/`
248
+ /// * `http://192.168.1.42/`
249
+ ///
250
+ /// The URI `http://notgoogle.com/` would not match.
251
+ pub fn no<S>(mut self, val: S) -> Self
252
+ where
253
+ S: IntoValue,
254
+ {
255
+ self.no = val.into_value();
256
+ self
257
+ }
258
+
259
+ // / Set the unix socket target proxy for all destinations.
260
+ #[cfg(unix)]
261
+ pub fn unix<S>(mut self, val: S) -> Self
262
+ where
263
+ S: super::uds::IntoUnixSocket,
264
+ {
265
+ self.unix = Some(val.unix_socket());
266
+ self
267
+ }
268
+
269
+ /// Construct a [`Matcher`] using the configured values.
270
+ pub(super) fn build(self, extra: Extra) -> Matcher {
271
+ if self.is_cgi {
272
+ return Matcher {
273
+ http: None,
274
+ https: None,
275
+ no: NoProxy::empty(),
276
+ #[cfg(unix)]
277
+ unix: None,
278
+ };
279
+ }
280
+
281
+ let mut all = parse_env_uri(&self.all);
282
+ let mut http = parse_env_uri(&self.http);
283
+ let mut https = parse_env_uri(&self.https);
284
+
285
+ if let Some(http) = http.as_mut() {
286
+ http.extra = extra.clone();
287
+ }
288
+ if let Some(https) = https.as_mut() {
289
+ https.extra = extra.clone();
290
+ }
291
+ if http.is_none() || https.is_none() {
292
+ if let Some(all) = all.as_mut() {
293
+ all.extra = extra;
294
+ }
295
+ }
296
+
297
+ Matcher {
298
+ http: http.or_else(|| all.clone()),
299
+ https: https.or(all),
300
+ no: NoProxy::from_string(&self.no),
301
+ #[cfg(unix)]
302
+ unix: self.unix,
303
+ }
304
+ }
305
+ }
306
+
307
+ fn get_first_env(names: &[&str]) -> String {
308
+ for name in names {
309
+ if let Ok(val) = std::env::var(name) {
310
+ return val;
311
+ }
312
+ }
313
+
314
+ String::new()
315
+ }
316
+
317
+ fn parse_env_uri(val: &str) -> Option<Intercept> {
318
+ let uri = val.parse::<Uri>().ok()?;
319
+ let mut builder = Uri::builder();
320
+ let mut is_httpish = false;
321
+ let mut is_socks = false;
322
+ let mut auth = Auth::Empty;
323
+
324
+ builder = builder.scheme(match uri.scheme() {
325
+ Some(s) => {
326
+ if s == &Scheme::HTTP || s == &Scheme::HTTPS {
327
+ is_httpish = true;
328
+ s.clone()
329
+ } else if matches!(s.as_str(), "socks4" | "socks4a" | "socks5" | "socks5h") {
330
+ is_socks = true;
331
+ s.clone()
332
+ } else {
333
+ // can't use this proxy scheme
334
+ return None;
335
+ }
336
+ }
337
+ // if no scheme provided, assume they meant 'http'
338
+ None => {
339
+ is_httpish = true;
340
+ Scheme::HTTP
341
+ }
342
+ });
343
+
344
+ let authority = {
345
+ let authority = uri.authority()?;
346
+ // default SOCKS port to 1080 if missing
347
+ if is_socks && authority.port().is_none() {
348
+ Authority::from_maybe_shared(Bytes::from(format!("{authority}:1080"))).ok()?
349
+ } else {
350
+ authority.clone()
351
+ }
352
+ };
353
+
354
+ if let Some((userinfo, host_port)) = authority.as_str().rsplit_once('@') {
355
+ let (user, pass) = match userinfo.split_once(':') {
356
+ Some((user, pass)) => (user, Some(pass)),
357
+ None => (userinfo, None),
358
+ };
359
+
360
+ let user = percent_decode_str(user).decode_utf8_lossy();
361
+ let pass = pass.map(|pass| percent_decode_str(pass).decode_utf8_lossy());
362
+ if is_httpish {
363
+ auth = Auth::Basic(crate::util::basic_auth(&user, pass.as_deref()));
364
+ } else {
365
+ auth = Auth::Raw(
366
+ Bytes::from(user.into_owned()),
367
+ Bytes::from(pass.map_or_else(String::new, std::borrow::Cow::into_owned)),
368
+ );
369
+ }
370
+ builder = builder.authority(host_port);
371
+ } else {
372
+ builder = builder.authority(authority);
373
+ }
374
+
375
+ // removing any path, but we MUST specify one or the builder errors
376
+ builder = builder.path_and_query("/");
377
+
378
+ Some(Intercept {
379
+ auth,
380
+ extra: Extra::default(),
381
+ uri: builder.build().ok()?,
382
+ })
383
+ }
384
+
385
+ impl NoProxy {
386
+ fn empty() -> NoProxy {
387
+ NoProxy {
388
+ ips: IpMatcher(Vec::new()),
389
+ domains: DomainMatcher(Vec::new()),
390
+ }
391
+ }
392
+
393
+ /// Returns a new no-proxy configuration based on a `no_proxy` string (or `None` if no variables
394
+ /// are set)
395
+ /// The rules are as follows:
396
+ /// * The environment variable `NO_PROXY` is checked, if it is not set, `no_proxy` is checked
397
+ /// * If neither environment variable is set, `None` is returned
398
+ /// * Entries are expected to be comma-separated (whitespace between entries is ignored)
399
+ /// * IP addresses (both IPv4 and IPv6) are allowed, as are optional subnet masks (by adding
400
+ /// /size, for example "`192.168.1.0/24`").
401
+ /// * An entry "`*`" matches all hostnames (this is the only wildcard allowed)
402
+ /// * Any other entry is considered a domain name (and may contain a leading dot, for example
403
+ /// `google.com` and `.google.com` are equivalent) and would match both that domain AND all
404
+ /// subdomains.
405
+ ///
406
+ /// For example, if `"NO_PROXY=google.com, 192.168.1.0/24"` was set, all of the following would
407
+ /// match (and therefore would bypass the proxy):
408
+ /// * `http://google.com/`
409
+ /// * `http://www.google.com/`
410
+ /// * `http://192.168.1.42/`
411
+ ///
412
+ /// The URI `http://notgoogle.com/` would not match.
413
+ pub fn from_string(no_proxy_list: &str) -> Self {
414
+ let mut ips = Vec::new();
415
+ let mut domains = Vec::new();
416
+ let parts = no_proxy_list.split(',').map(str::trim);
417
+ for part in parts {
418
+ match part.parse::<IpNet>() {
419
+ // If we can parse an IP net or address, then use it, otherwise, assume it is a
420
+ // domain
421
+ Ok(ip) => ips.push(Ip::Network(ip)),
422
+ Err(_) => match part.parse::<IpAddr>() {
423
+ Ok(addr) => ips.push(Ip::Address(addr)),
424
+ Err(_) => {
425
+ if !part.trim().is_empty() {
426
+ domains.push(part.to_owned())
427
+ }
428
+ }
429
+ },
430
+ }
431
+ }
432
+ NoProxy {
433
+ ips: IpMatcher(ips),
434
+ domains: DomainMatcher(domains),
435
+ }
436
+ }
437
+
438
+ /// Return true if this matches the host (domain or IP).
439
+ pub fn contains(&self, host: &str) -> bool {
440
+ // According to RFC3986, raw IPv6 hosts will be wrapped in []. So we need to strip those off
441
+ // the end in order to parse correctly
442
+ let host = if host.starts_with('[') {
443
+ let x: &[_] = &['[', ']'];
444
+ host.trim_matches(x)
445
+ } else {
446
+ host
447
+ };
448
+ match host.parse::<IpAddr>() {
449
+ // If we can parse an IP addr, then use it, otherwise, assume it is a domain
450
+ Ok(ip) => self.ips.contains(ip),
451
+ Err(_) => self.domains.contains(host),
452
+ }
453
+ }
454
+ }
455
+
456
+ impl IpMatcher {
457
+ fn contains(&self, addr: IpAddr) -> bool {
458
+ for ip in &self.0 {
459
+ match ip {
460
+ Ip::Address(address) => {
461
+ if &addr == address {
462
+ return true;
463
+ }
464
+ }
465
+ Ip::Network(net) => {
466
+ if net.contains(&addr) {
467
+ return true;
468
+ }
469
+ }
470
+ }
471
+ }
472
+ false
473
+ }
474
+ }
475
+
476
+ impl DomainMatcher {
477
+ // The following links may be useful to understand the origin of these rules:
478
+ // * https://curl.se/libcurl/c/CURLOPT_NOPROXY.html
479
+ // * https://github.com/curl/curl/issues/1208
480
+ fn contains(&self, domain: &str) -> bool {
481
+ let domain_len = domain.len();
482
+ for d in &self.0 {
483
+ if d.eq_ignore_ascii_case(domain)
484
+ || d.strip_prefix('.')
485
+ .is_some_and(|s| s.eq_ignore_ascii_case(domain))
486
+ {
487
+ return true;
488
+ } else if domain
489
+ .get(domain_len.saturating_sub(d.len())..)
490
+ .is_some_and(|s| s.eq_ignore_ascii_case(d))
491
+ {
492
+ if d.starts_with('.') {
493
+ // If the first character of d is a dot, that means the first character of
494
+ // domain must also be a dot, so we are looking at a
495
+ // subdomain of d and that matches
496
+ return true;
497
+ } else if domain
498
+ .as_bytes()
499
+ .get(domain_len.saturating_sub(d.len() + 1))
500
+ == Some(&b'.')
501
+ {
502
+ // Given that d is a prefix of domain, if the prior character in domain is a dot
503
+ // then that means we must be matching a subdomain of d, and that matches
504
+ return true;
505
+ }
506
+ } else if d == "*" {
507
+ return true;
508
+ }
509
+ }
510
+ false
511
+ }
512
+ }
513
+
514
+ mod builder {
515
+ /// A type that can used as a `Builder` value.
516
+ ///
517
+ /// Private and sealed, only visible in docs.
518
+ pub trait IntoValue {
519
+ #[doc(hidden)]
520
+ fn into_value(self) -> String;
521
+ }
522
+
523
+ impl IntoValue for String {
524
+ #[doc(hidden)]
525
+ fn into_value(self) -> String {
526
+ self
527
+ }
528
+ }
529
+
530
+ impl IntoValue for &String {
531
+ #[doc(hidden)]
532
+ fn into_value(self) -> String {
533
+ self.into()
534
+ }
535
+ }
536
+
537
+ impl IntoValue for &str {
538
+ #[doc(hidden)]
539
+ fn into_value(self) -> String {
540
+ self.into()
541
+ }
542
+ }
543
+ }
544
+
545
+ #[cfg(test)]
546
+ mod tests {
547
+ use super::*;
548
+
549
+ #[test]
550
+ fn test_domain_matcher() {
551
+ let domains = vec![".foo.bar".into(), "bar.foo".into()];
552
+ let matcher = DomainMatcher(domains);
553
+
554
+ // domains match with leading `.`
555
+ assert!(matcher.contains("foo.bar"));
556
+ assert!(matcher.contains("FOO.BAR"));
557
+ // subdomains match with leading `.`
558
+ assert!(matcher.contains("www.foo.bar"));
559
+ assert!(matcher.contains("WWW.FOO.BAR"));
560
+
561
+ // domains match with no leading `.`
562
+ assert!(matcher.contains("bar.foo"));
563
+ assert!(matcher.contains("Bar.foo"));
564
+ // subdomains match with no leading `.`
565
+ assert!(matcher.contains("www.bar.foo"));
566
+ assert!(matcher.contains("WWW.BAR.FOO"));
567
+
568
+ // non-subdomain string prefixes don't match
569
+ assert!(!matcher.contains("notfoo.bar"));
570
+ assert!(!matcher.contains("notbar.foo"));
571
+ }
572
+
573
+ #[test]
574
+ fn test_no_proxy_wildcard() {
575
+ let no_proxy = NoProxy::from_string("*");
576
+ assert!(no_proxy.contains("any.where"));
577
+ }
578
+
579
+ #[test]
580
+ fn test_no_proxy_ip_ranges() {
581
+ let no_proxy =
582
+ NoProxy::from_string(".foo.bar, bar.baz,10.42.1.1/24,::1,10.124.7.8,2001::/17");
583
+
584
+ let should_not_match = [
585
+ // random uri, not in no_proxy
586
+ "hyper.rs",
587
+ // make sure that random non-subdomain string prefixes don't match
588
+ "notfoo.bar",
589
+ // make sure that random non-subdomain string prefixes don't match
590
+ "notbar.baz",
591
+ // ipv4 address out of range
592
+ "10.43.1.1",
593
+ // ipv4 address out of range
594
+ "10.124.7.7",
595
+ // ipv6 address out of range
596
+ "[ffff:db8:a0b:12f0::1]",
597
+ // ipv6 address out of range
598
+ "[2005:db8:a0b:12f0::1]",
599
+ ];
600
+
601
+ for host in &should_not_match {
602
+ assert!(!no_proxy.contains(host), "should not contain {host:?}");
603
+ }
604
+
605
+ let should_match = [
606
+ // make sure subdomains (with leading .) match
607
+ "hello.foo.bar",
608
+ // make sure exact matches (without leading .) match (also makes sure spaces between
609
+ // entries work)
610
+ "bar.baz",
611
+ // make sure subdomains (without leading . in no_proxy) match
612
+ "foo.bar.baz",
613
+ // make sure subdomains (without leading . in no_proxy) match - this differs from cURL
614
+ "foo.bar",
615
+ // ipv4 address match within range
616
+ "10.42.1.100",
617
+ // ipv6 address exact match
618
+ "[::1]",
619
+ // ipv6 address match within range
620
+ "[2001:db8:a0b:12f0::1]",
621
+ // ipv4 address exact match
622
+ "10.124.7.8",
623
+ ];
624
+
625
+ for host in &should_match {
626
+ assert!(no_proxy.contains(host), "should contain {host:?}");
627
+ }
628
+ }
629
+
630
+ macro_rules! p {
631
+ ($($n:ident = $v:expr,)*) => ({Builder {
632
+ $($n: $v.into(),)*
633
+ ..Builder::default()
634
+ }.build(Extra::default())});
635
+ }
636
+
637
+ fn intercept(p: &Matcher, u: &str) -> Intercept {
638
+ match p.intercept(&u.parse().unwrap()).unwrap() {
639
+ Intercepted::Proxy(intercept) => intercept,
640
+ #[cfg(unix)]
641
+ Intercepted::Unix(path) => {
642
+ unreachable!("should not intercept unix socket: {path:?}")
643
+ }
644
+ }
645
+ }
646
+
647
+ #[test]
648
+ fn test_all_proxy() {
649
+ let p = p! {
650
+ all = "http://om.nom",
651
+ };
652
+
653
+ assert_eq!("http://om.nom", intercept(&p, "http://example.com").uri());
654
+
655
+ assert_eq!("http://om.nom", intercept(&p, "https://example.com").uri());
656
+ }
657
+
658
+ #[test]
659
+ fn test_specific_overrides_all() {
660
+ let p = p! {
661
+ all = "http://no.pe",
662
+ http = "http://y.ep",
663
+ };
664
+
665
+ assert_eq!("http://no.pe", intercept(&p, "https://example.com").uri());
666
+
667
+ // the http rule is "more specific" than the all rule
668
+ assert_eq!("http://y.ep", intercept(&p, "http://example.com").uri());
669
+ }
670
+
671
+ #[test]
672
+ fn test_parse_no_scheme_defaults_to_http() {
673
+ let p = p! {
674
+ https = "y.ep",
675
+ http = "127.0.0.1:8887",
676
+ };
677
+
678
+ assert_eq!(intercept(&p, "https://example.local").uri(), "http://y.ep");
679
+ assert_eq!(
680
+ intercept(&p, "http://example.local").uri(),
681
+ "http://127.0.0.1:8887"
682
+ );
683
+ }
684
+
685
+ #[test]
686
+ fn test_parse_http_auth() {
687
+ let p = p! {
688
+ all = "http://Aladdin:opensesame@y.ep",
689
+ };
690
+
691
+ let proxy = intercept(&p, "https://example.local");
692
+ assert_eq!(proxy.uri(), "http://y.ep");
693
+ assert_eq!(
694
+ proxy.basic_auth().expect("basic_auth"),
695
+ "Basic QWxhZGRpbjpvcGVuc2VzYW1l"
696
+ );
697
+ }
698
+
699
+ #[test]
700
+ fn test_parse_http_auth_without_password() {
701
+ let p = p! {
702
+ all = "http://Aladdin@y.ep",
703
+ };
704
+ let proxy = intercept(&p, "https://example.local");
705
+ assert_eq!(proxy.uri(), "http://y.ep");
706
+ assert_eq!(
707
+ proxy.basic_auth().expect("basic_auth"),
708
+ "Basic QWxhZGRpbjo="
709
+ );
710
+ }
711
+
712
+ #[test]
713
+ fn test_parse_http_auth_without_scheme() {
714
+ let p = p! {
715
+ all = "Aladdin:opensesame@y.ep",
716
+ };
717
+
718
+ let proxy = intercept(&p, "https://example.local");
719
+ assert_eq!(proxy.uri(), "http://y.ep");
720
+ assert_eq!(
721
+ proxy.basic_auth().expect("basic_auth"),
722
+ "Basic QWxhZGRpbjpvcGVuc2VzYW1l"
723
+ );
724
+ }
725
+
726
+ #[test]
727
+ fn test_dont_parse_http_when_is_cgi() {
728
+ let mut builder = Matcher::builder();
729
+ builder.is_cgi = true;
730
+ builder.http = "http://never.gonna.let.you.go".into();
731
+ let m = builder.build(Extra::default());
732
+
733
+ assert!(m.intercept(&"http://rick.roll".parse().unwrap()).is_none());
734
+ }
735
+
736
+ fn test_parse_socks(uri: &str) {
737
+ let p = p! {
738
+ all = uri,
739
+ };
740
+
741
+ let proxy = intercept(&p, "https://example.local");
742
+ assert_eq!(proxy.uri(), uri);
743
+ }
744
+
745
+ #[test]
746
+ fn test_parse_socks4() {
747
+ test_parse_socks("socks4://localhost:8887");
748
+ test_parse_socks("socks4a://localhost:8887");
749
+ }
750
+
751
+ #[test]
752
+ fn test_parse_socks5() {
753
+ test_parse_socks("socks5://localhost:8887");
754
+ test_parse_socks("socks5h://localhost:8887");
755
+ }
756
+
757
+ #[test]
758
+ fn test_domain_matcher_case_insensitive() {
759
+ let domains = vec![".foo.bar".into()];
760
+ let matcher = DomainMatcher(domains);
761
+
762
+ assert!(matcher.contains("foo.bar"));
763
+ assert!(matcher.contains("FOO.BAR"));
764
+ assert!(matcher.contains("Foo.Bar"));
765
+
766
+ assert!(matcher.contains("www.foo.bar"));
767
+ assert!(matcher.contains("WWW.FOO.BAR"));
768
+ assert!(matcher.contains("Www.Foo.Bar"));
769
+ }
770
+
771
+ #[test]
772
+ fn test_no_proxy_case_insensitive() {
773
+ let p = p! {
774
+ all = "http://proxy.local",
775
+ no = ".example.com",
776
+ };
777
+
778
+ // should bypass proxy (case insensitive match)
779
+ assert!(
780
+ p.intercept(&"http://example.com".parse().unwrap())
781
+ .is_none()
782
+ );
783
+ assert!(
784
+ p.intercept(&"http://EXAMPLE.COM".parse().unwrap())
785
+ .is_none()
786
+ );
787
+ assert!(
788
+ p.intercept(&"http://Example.com".parse().unwrap())
789
+ .is_none()
790
+ );
791
+
792
+ // subdomain should bypass proxy (case insensitive match)
793
+ assert!(
794
+ p.intercept(&"http://www.example.com".parse().unwrap())
795
+ .is_none()
796
+ );
797
+ assert!(
798
+ p.intercept(&"http://WWW.EXAMPLE.COM".parse().unwrap())
799
+ .is_none()
800
+ );
801
+ assert!(
802
+ p.intercept(&"http://Www.Example.Com".parse().unwrap())
803
+ .is_none()
804
+ );
805
+ }
806
+ }