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,534 @@
1
+ use std::{
2
+ net::SocketAddr,
3
+ pin::Pin,
4
+ task::{Context, Poll},
5
+ };
6
+
7
+ use bytes::Bytes;
8
+ #[cfg(feature = "charset")]
9
+ use encoding_rs::{Encoding, UTF_8};
10
+ #[cfg(feature = "stream")]
11
+ use futures_util::Stream;
12
+ use http::{HeaderMap, StatusCode, Uri, Version};
13
+ use http_body::{Body as HttpBody, Frame};
14
+ use http_body_util::{BodyExt, Collected};
15
+ #[cfg(feature = "charset")]
16
+ use mime::Mime;
17
+ #[cfg(feature = "json")]
18
+ use serde::de::DeserializeOwned;
19
+
20
+ use super::{
21
+ conn::HttpInfo,
22
+ core::{ext::ReasonPhrase, upgrade},
23
+ };
24
+ #[cfg(feature = "cookies")]
25
+ use crate::cookie;
26
+ use crate::{Body, Error, Upgraded, error::BoxError, ext::RequestUri};
27
+
28
+ /// A Response to a submitted [`crate::Request`].
29
+ #[derive(Debug)]
30
+ pub struct Response {
31
+ uri: Uri,
32
+ res: http::Response<Body>,
33
+ }
34
+
35
+ impl Response {
36
+ pub(super) fn new<B>(res: http::Response<B>, uri: Uri) -> Response
37
+ where
38
+ B: HttpBody + Send + Sync + 'static,
39
+ B::Data: Into<Bytes>,
40
+ B::Error: Into<BoxError>,
41
+ {
42
+ Response {
43
+ uri,
44
+ res: res.map(Body::wrap),
45
+ }
46
+ }
47
+
48
+ /// Get the final [`Uri`] of this [`Response`].
49
+ #[inline]
50
+ pub fn uri(&self) -> &Uri {
51
+ &self.uri
52
+ }
53
+
54
+ /// Get the [`StatusCode`] of this [`Response`].
55
+ #[inline]
56
+ pub fn status(&self) -> StatusCode {
57
+ self.res.status()
58
+ }
59
+
60
+ /// Get the HTTP [`Version`] of this [`Response`].
61
+ #[inline]
62
+ pub fn version(&self) -> Version {
63
+ self.res.version()
64
+ }
65
+
66
+ /// Get the [`HeaderMap`] of this [`Response`].
67
+ #[inline]
68
+ pub fn headers(&self) -> &HeaderMap {
69
+ self.res.headers()
70
+ }
71
+
72
+ /// Get a mutable reference to the [`HeaderMap`] of this [`Response`].
73
+ #[inline]
74
+ pub fn headers_mut(&mut self) -> &mut HeaderMap {
75
+ self.res.headers_mut()
76
+ }
77
+
78
+ /// Get the content length of the [`Response`], if it is known.
79
+ ///
80
+ /// This value does not directly represents the value of the `Content-Length`
81
+ /// header, but rather the size of the response's body. To read the header's
82
+ /// value, please use the [`Response::headers`] method instead.
83
+ ///
84
+ /// Reasons it may not be known:
85
+ ///
86
+ /// - The response does not include a body (e.g. it responds to a `HEAD` request).
87
+ /// - The response is gzipped and automatically decoded (thus changing the actual decoded
88
+ /// length).
89
+ #[inline]
90
+ pub fn content_length(&self) -> Option<u64> {
91
+ HttpBody::size_hint(self.res.body()).exact()
92
+ }
93
+
94
+ /// Retrieve the cookies contained in the [`Response`].
95
+ ///
96
+ /// Note that invalid 'Set-Cookie' headers will be ignored.
97
+ ///
98
+ /// # Optional
99
+ ///
100
+ /// This requires the optional `cookies` feature to be enabled.
101
+ #[cfg(feature = "cookies")]
102
+ pub fn cookies(&self) -> impl Iterator<Item = cookie::Cookie<'_>> {
103
+ self.res
104
+ .headers()
105
+ .get_all(crate::header::SET_COOKIE)
106
+ .iter()
107
+ .map(cookie::Cookie::parse)
108
+ .filter_map(Result::ok)
109
+ }
110
+
111
+ /// Get the local address used to get this [`Response`].
112
+ pub fn local_addr(&self) -> Option<SocketAddr> {
113
+ self.res
114
+ .extensions()
115
+ .get::<HttpInfo>()
116
+ .map(HttpInfo::local_addr)
117
+ }
118
+
119
+ /// Get the remote address used to get this [`Response`].
120
+ pub fn remote_addr(&self) -> Option<SocketAddr> {
121
+ self.res
122
+ .extensions()
123
+ .get::<HttpInfo>()
124
+ .map(HttpInfo::remote_addr)
125
+ }
126
+
127
+ // body methods
128
+
129
+ /// Get the full response text.
130
+ ///
131
+ /// This method decodes the response body with BOM sniffing
132
+ /// and with malformed sequences replaced with the [`char::REPLACEMENT_CHARACTER`].
133
+ /// Encoding is determined from the `charset` parameter of `Content-Type` header,
134
+ /// and defaults to `utf-8` if not presented.
135
+ ///
136
+ /// Note that the BOM is stripped from the returned String.
137
+ ///
138
+ /// # Note
139
+ ///
140
+ /// If the `charset` feature is disabled the method will only attempt to decode the
141
+ /// response as UTF-8, regardless of the given `Content-Type`
142
+ ///
143
+ /// # Example
144
+ ///
145
+ /// ```
146
+ /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
147
+ /// let content = wreq::Client::new()
148
+ /// .get("http://httpbin.org/range/26")
149
+ /// .send()
150
+ /// .await?
151
+ /// .text()
152
+ /// .await?;
153
+ ///
154
+ /// println!("text: {content:?}");
155
+ /// # Ok(())
156
+ /// # }
157
+ /// ```
158
+ pub async fn text(self) -> crate::Result<String> {
159
+ #[cfg(feature = "charset")]
160
+ {
161
+ self.text_with_charset("utf-8").await
162
+ }
163
+
164
+ #[cfg(not(feature = "charset"))]
165
+ {
166
+ let full = self.bytes().await?;
167
+ let text = String::from_utf8_lossy(&full);
168
+ Ok(text.into_owned())
169
+ }
170
+ }
171
+
172
+ /// Get the full response text given a specific encoding.
173
+ ///
174
+ /// This method decodes the response body with BOM sniffing
175
+ /// and with malformed sequences replaced with the
176
+ /// [`char::REPLACEMENT_CHARACTER`].
177
+ /// You can provide a default encoding for decoding the raw message, while the
178
+ /// `charset` parameter of `Content-Type` header is still prioritized. For more information
179
+ /// about the possible encoding name, please go to [`encoding_rs`] docs.
180
+ ///
181
+ /// Note that the BOM is stripped from the returned String.
182
+ ///
183
+ /// [`encoding_rs`]: https://docs.rs/encoding_rs/0.8/encoding_rs/#relationship-with-windows-code-pages
184
+ ///
185
+ /// # Optional
186
+ ///
187
+ /// This requires the optional `encoding_rs` feature enabled.
188
+ ///
189
+ /// # Example
190
+ ///
191
+ /// ```
192
+ /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
193
+ /// let content = wreq::Client::new()
194
+ /// .get("http://httpbin.org/range/26")
195
+ /// .send()
196
+ /// .await?
197
+ /// .text_with_charset("utf-8")
198
+ /// .await?;
199
+ ///
200
+ /// println!("text: {content:?}");
201
+ /// # Ok(())
202
+ /// # }
203
+ /// ```
204
+ #[cfg(feature = "charset")]
205
+ #[cfg_attr(docsrs, doc(cfg(feature = "charset")))]
206
+ pub async fn text_with_charset(
207
+ self,
208
+ default_encoding: impl AsRef<str>,
209
+ ) -> crate::Result<String> {
210
+ let content_type = self
211
+ .headers()
212
+ .get(crate::header::CONTENT_TYPE)
213
+ .and_then(|value| value.to_str().ok())
214
+ .and_then(|value| value.parse::<Mime>().ok());
215
+ let encoding_name = content_type
216
+ .as_ref()
217
+ .and_then(|mime| mime.get_param("charset").map(|charset| charset.as_str()))
218
+ .unwrap_or(default_encoding.as_ref());
219
+ let encoding = Encoding::for_label(encoding_name.as_bytes()).unwrap_or(UTF_8);
220
+
221
+ let full = self.bytes().await?;
222
+ let (text, _, _) = encoding.decode(&full);
223
+ Ok(text.into_owned())
224
+ }
225
+
226
+ /// Try to deserialize the response body as JSON.
227
+ ///
228
+ /// # Optional
229
+ ///
230
+ /// This requires the optional `json` feature enabled.
231
+ ///
232
+ /// # Examples
233
+ ///
234
+ /// ```
235
+ /// # extern crate wreq;
236
+ /// # extern crate serde;
237
+ /// #
238
+ /// # use wreq::Error;
239
+ /// # use serde::Deserialize;
240
+ /// #
241
+ /// // This `derive` requires the `serde` dependency.
242
+ /// #[derive(Deserialize)]
243
+ /// struct Ip {
244
+ /// origin: String,
245
+ /// }
246
+ ///
247
+ /// # async fn run() -> Result<(), Error> {
248
+ /// let ip = wreq::Client::new()
249
+ /// .get("http://httpbin.org/ip")
250
+ /// .send()
251
+ /// .await?
252
+ /// .json::<Ip>()
253
+ /// .await?;
254
+ ///
255
+ /// println!("ip: {}", ip.origin);
256
+ /// # Ok(())
257
+ /// # }
258
+ /// #
259
+ /// # fn main() { }
260
+ /// ```
261
+ ///
262
+ /// # Errors
263
+ ///
264
+ /// This method fails whenever the response body is not in JSON format
265
+ /// or it cannot be properly deserialized to target type `T`. For more
266
+ /// details please see [`serde_json::from_reader`].
267
+ ///
268
+ /// [`serde_json::from_reader`]: https://docs.serde.rs/serde_json/fn.from_reader.html
269
+ #[cfg(feature = "json")]
270
+ #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
271
+ pub async fn json<T: DeserializeOwned>(self) -> crate::Result<T> {
272
+ let full = self.bytes().await?;
273
+ serde_json::from_slice(&full).map_err(Error::decode)
274
+ }
275
+
276
+ /// Get the full response body as [`Bytes`].
277
+ ///
278
+ /// # Example
279
+ ///
280
+ /// ```
281
+ /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
282
+ /// let bytes = wreq::Client::new()
283
+ /// .get("http://httpbin.org/ip")
284
+ /// .send()
285
+ /// .await?
286
+ /// .bytes()
287
+ /// .await?;
288
+ ///
289
+ /// println!("bytes: {bytes:?}");
290
+ /// # Ok(())
291
+ /// # }
292
+ /// ```
293
+ pub async fn bytes(self) -> crate::Result<Bytes> {
294
+ BodyExt::collect(self.res.into_body())
295
+ .await
296
+ .map(Collected::<Bytes>::to_bytes)
297
+ }
298
+
299
+ /// Stream a chunk of the response body.
300
+ ///
301
+ /// When the response body has been exhausted, this will return `None`.
302
+ ///
303
+ /// # Example
304
+ ///
305
+ /// ```
306
+ /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
307
+ /// let mut res = wreq::get("https://hyper.rs").send().await?;
308
+ ///
309
+ /// while let Some(chunk) = res.chunk().await? {
310
+ /// println!("Chunk: {chunk:?}");
311
+ /// }
312
+ /// # Ok(())
313
+ /// # }
314
+ /// ```
315
+ pub async fn chunk(&mut self) -> crate::Result<Option<Bytes>> {
316
+ loop {
317
+ if let Some(res) = self.res.body_mut().frame().await {
318
+ if let Ok(buf) = res?.into_data() {
319
+ return Ok(Some(buf));
320
+ }
321
+ } else {
322
+ return Ok(None);
323
+ }
324
+ }
325
+ }
326
+
327
+ /// Convert the response into a [`Stream`] of [`Bytes`] from the body.
328
+ ///
329
+ /// # Example
330
+ ///
331
+ /// ```
332
+ /// use futures_util::StreamExt;
333
+ ///
334
+ /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
335
+ /// let mut stream = wreq::Client::new()
336
+ /// .get("http://httpbin.org/ip")
337
+ /// .send()
338
+ /// .await?
339
+ /// .bytes_stream();
340
+ ///
341
+ /// while let Some(item) = stream.next().await {
342
+ /// println!("Chunk: {:?}", item?);
343
+ /// }
344
+ /// # Ok(())
345
+ /// # }
346
+ /// ```
347
+ ///
348
+ /// # Optional
349
+ ///
350
+ /// This requires the optional `stream` feature to be enabled.
351
+ #[cfg(feature = "stream")]
352
+ #[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
353
+ pub fn bytes_stream(self) -> impl Stream<Item = crate::Result<Bytes>> {
354
+ http_body_util::BodyDataStream::new(self.res.into_body())
355
+ }
356
+
357
+ // extension methods
358
+
359
+ /// Returns a reference to the associated extensions.
360
+ ///
361
+ /// # Example
362
+ ///
363
+ /// ```
364
+ /// # use wreq::{Client, tls::TlsInfo};
365
+ /// # async fn run() -> wreq::Result<()> {
366
+ /// // Build a client that records TLS information.
367
+ /// let client = Client::builder()
368
+ /// .tls_info(true)
369
+ /// .build()?;
370
+ ///
371
+ /// // Make a request.
372
+ /// let resp = client.get("https://www.google.com").send().await?;
373
+ ///
374
+ /// // Take the TlsInfo extension to inspect it.
375
+ /// if let Some(tls_info) = resp.extensions().get::<TlsInfo>() {
376
+ /// // Now you own the TlsInfo and can process it.
377
+ /// println!("Peer certificate: {:?}", tls_info.peer_certificate());
378
+ /// }
379
+ ///
380
+ /// # Ok(())
381
+ /// # }
382
+ /// ```
383
+ #[inline]
384
+ pub fn extensions(&self) -> &http::Extensions {
385
+ self.res.extensions()
386
+ }
387
+
388
+ /// Returns a mutable reference to the associated extensions.
389
+ ///
390
+ /// # Example
391
+ ///
392
+ /// ```
393
+ /// # use wreq::{Client, tls::TlsInfo};
394
+ /// # async fn run() -> wreq::Result<()> {
395
+ /// // Build a client that records TLS information.
396
+ /// let client = Client::builder()
397
+ /// .tls_info(true)
398
+ /// .build()?;
399
+ ///
400
+ /// // Make a request.
401
+ /// let mut resp = client.get("https://www.google.com").send().await?;
402
+ ///
403
+ /// // Take the TlsInfo extension to inspect it.
404
+ /// if let Some(tls_info) = resp.extensions_mut().remove::<TlsInfo>() {
405
+ /// // Now you own the TlsInfo and can process it.
406
+ /// println!("Peer certificate: {:?}", tls_info.peer_certificate());
407
+ /// }
408
+ ///
409
+ /// # Ok(())
410
+ /// # }
411
+ /// ```
412
+ #[inline]
413
+ pub fn extensions_mut(&mut self) -> &mut http::Extensions {
414
+ self.res.extensions_mut()
415
+ }
416
+
417
+ // util methods
418
+
419
+ /// Turn a response into an error if the server returned an error.
420
+ ///
421
+ /// # Example
422
+ ///
423
+ /// ```
424
+ /// # use wreq::Response;
425
+ /// fn on_response(res: Response) {
426
+ /// match res.error_for_status() {
427
+ /// Ok(_res) => (),
428
+ /// Err(err) => {
429
+ /// // asserting a 400 as an example
430
+ /// // it could be any status between 400...599
431
+ /// assert_eq!(err.status(), Some(wreq::StatusCode::BAD_REQUEST));
432
+ /// }
433
+ /// }
434
+ /// }
435
+ /// # fn main() {}
436
+ /// ```
437
+ pub fn error_for_status(mut self) -> crate::Result<Self> {
438
+ let status = self.status();
439
+ if status.is_client_error() || status.is_server_error() {
440
+ let reason = self.res.extensions_mut().remove::<ReasonPhrase>();
441
+ Err(Error::status_code(self.uri, status, reason))
442
+ } else {
443
+ Ok(self)
444
+ }
445
+ }
446
+
447
+ /// Turn a reference to a response into an error if the server returned an error.
448
+ ///
449
+ /// # Example
450
+ ///
451
+ /// ```
452
+ /// # use wreq::Response;
453
+ /// fn on_response(res: &Response) {
454
+ /// match res.error_for_status_ref() {
455
+ /// Ok(_res) => (),
456
+ /// Err(err) => {
457
+ /// // asserting a 400 as an example
458
+ /// // it could be any status between 400...599
459
+ /// assert_eq!(err.status(), Some(wreq::StatusCode::BAD_REQUEST));
460
+ /// }
461
+ /// }
462
+ /// }
463
+ /// # fn main() {}
464
+ /// ```
465
+ pub fn error_for_status_ref(&self) -> crate::Result<&Self> {
466
+ let status = self.status();
467
+ if status.is_client_error() || status.is_server_error() {
468
+ let reason = self.res.extensions().get::<ReasonPhrase>().cloned();
469
+ Err(Error::status_code(self.uri.clone(), status, reason))
470
+ } else {
471
+ Ok(self)
472
+ }
473
+ }
474
+
475
+ /// Consumes the [`Response`] and returns a future for a possible HTTP upgrade.
476
+ pub async fn upgrade(self) -> crate::Result<Upgraded> {
477
+ upgrade::on(self.res).await.map_err(Error::upgrade)
478
+ }
479
+ }
480
+
481
+ /// I'm not sure this conversion is that useful... People should be encouraged
482
+ /// to use [`http::Response`], not `wreq::Response`.
483
+ impl<T: Into<Body>> From<http::Response<T>> for Response {
484
+ fn from(r: http::Response<T>) -> Response {
485
+ let mut res = r.map(Into::into);
486
+ let uri = res
487
+ .extensions_mut()
488
+ .remove::<RequestUri>()
489
+ .unwrap_or_else(|| RequestUri(Uri::from_static("http://no.url.provided.local")));
490
+ Response { res, uri: uri.0 }
491
+ }
492
+ }
493
+
494
+ /// A [`Response`] can be converted into a [`http::Response`].
495
+ // It's supposed to be the inverse of the conversion above.
496
+ impl From<Response> for http::Response<Body> {
497
+ fn from(r: Response) -> http::Response<Body> {
498
+ let mut res = r.res.map(Body::wrap);
499
+ res.extensions_mut().insert(RequestUri(r.uri));
500
+ res
501
+ }
502
+ }
503
+
504
+ /// A [`Response`] can be piped as the [`Body`] of another request.
505
+ impl From<Response> for Body {
506
+ fn from(r: Response) -> Body {
507
+ Body::wrap(r.res.into_body())
508
+ }
509
+ }
510
+
511
+ /// A [`Response`] implements [`HttpBody`] to allow streaming the body.
512
+ impl HttpBody for Response {
513
+ type Data = Bytes;
514
+
515
+ type Error = Error;
516
+
517
+ #[inline]
518
+ fn poll_frame(
519
+ mut self: Pin<&mut Self>,
520
+ cx: &mut Context<'_>,
521
+ ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
522
+ Pin::new(self.res.body_mut()).poll_frame(cx)
523
+ }
524
+
525
+ #[inline]
526
+ fn is_end_stream(&self) -> bool {
527
+ self.res.body().is_end_stream()
528
+ }
529
+
530
+ #[inline]
531
+ fn size_hint(&self) -> http_body::SizeHint {
532
+ self.res.body().size_hint()
533
+ }
534
+ }
@@ -0,0 +1,99 @@
1
+ use serde::{Serialize, de::DeserializeOwned};
2
+
3
+ use super::{Message, Utf8Bytes};
4
+ use crate::Error;
5
+
6
+ impl Message {
7
+ /// Tries to serialize the JSON as a [`Message::Text`].
8
+ ///
9
+ /// # Optional
10
+ ///
11
+ /// This requires the optional `json` feature enabled.
12
+ ///
13
+ /// # Errors
14
+ ///
15
+ /// Serialization can fail if `T`'s implementation of `Serialize` decides to
16
+ /// fail, or if `T` contains a map with non-string keys.
17
+ #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
18
+ pub fn text_from_json<T: Serialize + ?Sized>(json: &T) -> crate::Result<Self> {
19
+ serde_json::to_string(json)
20
+ .map(Utf8Bytes::from)
21
+ .map(Message::Text)
22
+ .map_err(Error::decode)
23
+ }
24
+
25
+ /// Tries to serialize the JSON as a [`Message::Binary`].
26
+ ///
27
+ /// # Optional
28
+ ///
29
+ /// This requires that the optional `json` feature is enabled.
30
+ ///
31
+ /// # Errors
32
+ ///
33
+ /// Serialization can fail if `T`'s implementation of `Serialize` decides to
34
+ /// fail, or if `T` contains a map with non-string keys.
35
+ #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
36
+ pub fn binary_from_json<T: Serialize + ?Sized>(json: &T) -> crate::Result<Self> {
37
+ serde_json::to_vec(json)
38
+ .map(bytes::Bytes::from)
39
+ .map(Message::Binary)
40
+ .map_err(Error::decode)
41
+ }
42
+
43
+ /// Tries to deserialize the message body as JSON.
44
+ ///
45
+ /// # Optional
46
+ ///
47
+ /// This requires that the optional `json` feature is enabled.
48
+ ///
49
+ /// # Errors
50
+ ///
51
+ /// This method fails whenever the response body is not in `JSON` format,
52
+ /// or it cannot be properly deserialized to target type `T`.
53
+ ///
54
+ /// For more details please see [`serde_json::from_str`] and
55
+ /// [`serde_json::from_slice`].
56
+ #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
57
+ pub fn json<T: DeserializeOwned>(&self) -> crate::Result<T> {
58
+ use serde::de::Error as _;
59
+ match self {
60
+ Self::Text(x) => serde_json::from_str(x).map_err(Error::decode),
61
+ Self::Binary(x) => serde_json::from_slice(x).map_err(Error::decode),
62
+ Self::Ping(_) | Self::Pong(_) | Self::Close { .. } => Err(Error::decode(
63
+ serde_json::Error::custom("neither text nor binary"),
64
+ )),
65
+ }
66
+ }
67
+ }
68
+
69
+ #[cfg(test)]
70
+ mod test {
71
+ use serde::{Deserialize, Serialize};
72
+
73
+ use super::Message;
74
+
75
+ #[derive(Default, Serialize, Deserialize)]
76
+ struct Content {
77
+ message: String,
78
+ }
79
+
80
+ #[test]
81
+ pub fn text_json() -> crate::Result<()> {
82
+ let content = Content::default();
83
+ let message = Message::text_from_json(&content)?;
84
+ assert!(matches!(message, Message::Text(_)));
85
+ let _: Content = message.json()?;
86
+
87
+ Ok(())
88
+ }
89
+
90
+ #[test]
91
+ pub fn binary_json() -> crate::Result<()> {
92
+ let content = Content::default();
93
+ let message = Message::binary_from_json(&content)?;
94
+ assert!(matches!(message, Message::Binary(_)));
95
+ let _: Content = message.json()?;
96
+
97
+ Ok(())
98
+ }
99
+ }