1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! # hpx
//!
//! An ergonomic all-in-one HTTP client for browser emulation with TLS, JA3/JA4, and HTTP/2
//! fingerprints.
//!
//! - Plain bodies, [JSON](#json), [urlencoded](#forms), multipart
//! - Cookies Store
//! - [Redirect Policy](#redirect-policies)
//! - Original Header
//! - Rotating [Proxies](#proxies)
//! - [Certificate Store](#certificate-store)
//! - [Tower](https://docs.rs/tower/latest/tower) Middleware
//! - [WebSocket](#websocket) Upgrade
//! - HTTPS via [BoringSSL](#tls)
//! - HTTP/2 over TLS [Emulation](#emulation)
//!
//! Additional learning resources include:
//!
//! - [The Rust Cookbook](https://doc.rust-lang.org/stable/book/ch00-00-introduction.html)
//!
//! ## Emulation
//!
//! The `emulation` module provides a way to simulate various browser TLS/HTTP2 fingerprints.
//!
//! ```rust,no_run
//! use hpx::BrowserProfile;
//!
//! #[tokio::main]
//! async fn main() -> hpx::Result<()> {
//! // Use the API you're already familiar with
//! let resp = hpx::get("https://tls.peet.ws/api/all")
//! .emulation(BrowserProfile::Firefox)
//! .send()
//! .await?;
//! println!("{}", resp.text().await?);
//!
//! Ok(())
//! }
//! ```
//!
//! ## Websocket
//!
//! The `websocket` module provides a way to upgrade a connection to a websocket.
//!
//! ```rust,no_run
//! # #[cfg(feature = "ws")]
//! # #[tokio::main]
//! # async fn main() -> hpx::Result<()> {
//! use futures_util::{SinkExt, StreamExt, TryStreamExt};
//! use hpx::{header, ws::message::Message};
//!
//! // Use the API you're already familiar with
//! let websocket = hpx::websocket("wss://echo.websocket.org")
//! .header(header::USER_AGENT, env!("CARGO_PKG_NAME"))
//! .send()
//! .await?;
//!
//! assert_eq!(websocket.version(), http::Version::HTTP_11);
//!
//! let (mut tx, mut rx) = websocket.into_websocket().await?.split();
//!
//! tokio::spawn(async move {
//! for i in 1..11 {
//! if let Err(err) = tx.send(Message::text(format!("Hello, World! {i}"))).await {
//! eprintln!("failed to send message: {err}");
//! }
//! }
//! });
//!
//! while let Some(message) = rx.try_next().await? {
//! if let Message::Text(text) = message {
//! println!("received: {text}");
//! }
//! }
//!
//! Ok(())
//! # }
//! # #[cfg(not(feature = "ws"))]
//! # fn main() {}
//! ```
//!
//! ## Making a GET request
//!
//! Making a GET request is simple.
//!
//! ```rust
//! # async fn run() -> hpx::Result<()> {
//! let body = hpx::get("https://www.rust-lang.org")
//! .send()
//! .await?
//! .text()
//! .await?;
//!
//! println!("body = {:?}", body);
//! # Ok(())
//! # }
//! ```
//!
//! **NOTE**: If you plan to perform multiple requests, it is best to create a
//! [`Client`][client] and reuse it, taking advantage of keep-alive connection
//! pooling.
//!
//! ## Making POST requests (or setting request bodies)
//!
//! There are several ways you can set the body of a request. The basic one is
//! by using the `body()` method of a [`RequestBuilder`][builder]. This lets you set the
//! exact raw bytes of what the body should be. It accepts various types,
//! including `String` and `Vec<u8>`. If you wish to pass a custom
//! type, you can use the `hpx::Body` constructors.
//!
//! ```rust
//! # use hpx::Error;
//! #
//! # async fn run() -> Result<(), Error> {
//! let client = hpx::Client::new();
//! let res = client
//! .post("http://httpbin.org/post")
//! .body("the exact body that is sent")
//! .send()
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Forms
//!
//! It's very common to want to send form data in a request body. This can be
//! done with any type that can be serialized into form data.
//!
//! This can be an array of tuples, or a `HashMap`, or a custom type that
//! implements [`Serialize`][serde].
//!
//! The feature `form` is required.
//!
//! ```rust
//! # use hpx::Error;
//! # #[cfg(feature = "form")]
//! # async fn run() -> Result<(), Error> {
//! // This will POST a body of `foo=bar&baz=quux`
//! let params = [("foo", "bar"), ("baz", "quux")];
//! let client = hpx::Client::new();
//! let res = client
//! .post("http://httpbin.org/post")
//! .form(¶ms)
//! .send()
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### JSON
//!
//! There is also a `json` method helper on the [`RequestBuilder`][builder] that works in
//! a similar fashion the `form` method. It can take any value that can be
//! serialized into JSON. The feature `json` is required.
//!
//! ```rust
//! # use hpx::Error;
//! # use std::collections::HashMap;
//! #
//! # #[cfg(feature = "json")]
//! # async fn run() -> Result<(), Error> {
//! // This will POST a body of `{"lang":"rust","body":"json"}`
//! let mut map = HashMap::new();
//! map.insert("lang", "rust");
//! map.insert("body", "json");
//!
//! let client = hpx::Client::new();
//! let res = client
//! .post("http://httpbin.org/post")
//! .json(&map)
//! .send()
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Redirect Policies
//!
//! By default, the client does not handle HTTP redirects.
//! To customize this behavior, you can use [`redirect::Policy`][redirect] with ClientBuilder.
//!
//! ## Cookies
//!
//! The automatic storing and sending of session cookies can be enabled with
//! the `cookie_store` method on `ClientBuilder` (requires the `cookies` feature).
//!
//! ## Proxies
//!
//! **NOTE**: System proxies are **disabled** by default. Call
//! [`ClientBuilder::system_proxy()`] to opt in to automatic detection.
//!
//! When enabled, system proxies look in environment variables to set HTTP or
//! HTTPS proxies.
//!
//! `HTTP_PROXY` or `http_proxy` provide HTTP proxies for HTTP connections while
//! `HTTPS_PROXY` or `https_proxy` provide HTTPS proxies for HTTPS connections.
//! `ALL_PROXY` or `all_proxy` provide proxies for both HTTP and HTTPS connections.
//! If both the all proxy and HTTP or HTTPS proxy variables are set the more specific
//! HTTP or HTTPS proxies take precedence.
//!
//! These can be overwritten by adding a [`Proxy`] to `ClientBuilder`
//! i.e. `let proxy = hpx::Proxy::http("https://secure.example")?;`
//! or disabled by calling `ClientBuilder::no_proxy()`.
//!
//! `socks` feature is required if you have configured socks proxy like this:
//!
//! ```bash
//! export https_proxy=socks5://127.0.0.1:1086
//! ```
//!
//! * `http://` is the scheme for http proxy
//! * `https://` is the scheme for https proxy
//! * `socks4://` is the scheme for socks4 proxy
//! * `socks4a://` is the scheme for socks4a proxy
//! * `socks5://` is the scheme for socks5 proxy
//! * `socks5h://` is the scheme for socks5h proxy
//!
//! ## TLS
//!
//! By default, clients will utilize BoringSSL transport layer security to connect to HTTPS targets.
//!
//! - Various parts of TLS can also be configured or even disabled on the `ClientBuilder`.
//!
//! ## Certificate Store
//!
//! By default, hpx uses Mozilla's root certificates through the webpki-roots crate.
//! This static root certificate bundle is not automatically updated and ignores any root
//! certificates installed on the host. You can disable default-features to use the system's default
//! certificate path. Additionally, hpx provides a certificate store for users to customize and
//! update certificates.
//!
//! Custom Certificate Store verification supports Root CA certificates, peer certificates, and
//! self-signed certificate SSL pinning.
//!
//! ## Optional Features
//!
//! The following are a list of [Cargo features][cargo-features] that can be
//! enabled or disabled:
//!
//! - **cookies**: Provides cookie session support.
//! - **gzip**: Provides response body gzip decompression.
//! - **brotli**: Provides response body brotli decompression.
//! - **zstd**: Provides response body zstd decompression.
//! - **deflate**: Provides response body deflate decompression.
//! - **query**: Provides query parameter serialization.
//! - **form**: Provides form data serialization.
//! - **json**: Provides serialization and deserialization for JSON bodies.
//! - **multipart**: Provides functionality for multipart forms.
//! - **charset**: Improved support for decoding text.
//! - **stream**: Adds support for `futures::Stream`.
//! - **socks**: Provides SOCKS5 and SOCKS4 proxy support.
//! - **ws**: Provides websocket support.
//! - **hickory-dns**: Enables a hickory-dns async resolver instead of default threadpool using
//! `getaddrinfo`.
//! - **webpki-roots** *(enabled by default)*: Use the webpki-roots crate for root certificates.
//! - **system-proxy**: Enable system proxy support.
//! - **tracing**: Enable tracing logging support.
//!
//! [client]: ./struct.Client.html
//! [response]: ./struct.Response.html
//! [get]: ./fn.get.html
//! [builder]: ./struct.RequestBuilder.html
//! [serde]: http://serde.rs
//! [redirect]: crate::redirect
//! [Proxy]: ./struct.Proxy.html
//! [cargo-features]: https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section
use arc_swap as _;
use const_oid as _;
use der as _;
use hmac as _;
use parking_lot as _;
use pkcs8 as _;
use pkcs12 as _;
use rustls as _;
use rustls_pemfile as _;
use rustls_pki_types as _;
use sha2 as _;
use smallvec as _;
use webpki_root_certs as _;
use webpki_roots as _;
/// Request delay middleware for Tower services.
/// Lifecycle hooks for HTTP requests and responses.
/// Response recovery hooks for buffered status-based retries.
/// Typestate-pattern configuration builders with scoped contexts.
/// Automatic header value management.
pub use ;
use libc as _;
pub use http1;
pub use http2;
pub use multipart;
pub use ws;
pub use ;
/// Shortcut method to quickly make a `GET` request.
///
/// See also the methods on the [`hpx::RequestBuilder`](./struct.RequestBuilder.html)
/// type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> hpx::Result<()> {
/// let body = hpx::get("https://www.rust-lang.org")
/// .send()
/// .await?
/// .text()
/// .await?;
/// # Ok(())
/// # }
/// ```
/// Shortcut method to quickly make a `POST` request.
///
/// See also the methods on the [`hpx::RequestBuilder`](./struct.RequestBuilder.html)
/// type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> hpx::Result<()> {
/// let res = hpx::post("https://httpbin.org/post")
/// .body("example body")
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
/// Shortcut method to quickly make a `PUT` request.
///
/// See also the methods on the [`hpx::RequestBuilder`](./struct.RequestBuilder.html)
/// type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> hpx::Result<()> {
/// let res = hpx::put("https://httpbin.org/put")
/// .body("update content")
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
/// Shortcut method to quickly make a `DELETE` request.
///
/// See also the methods on the [`hpx::RequestBuilder`](./struct.RequestBuilder.html)
/// type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> hpx::Result<()> {
/// let res = hpx::delete("https://httpbin.org/delete").send().await?;
/// # Ok(())
/// # }
/// ```
/// Shortcut method to quickly make a `HEAD` request.
///
/// See also the methods on the [`hpx::RequestBuilder`](./struct.RequestBuilder.html)
/// type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> hpx::Result<()> {
/// let res = hpx::head("https://httpbin.org/get").send().await?;
/// # Ok(())
/// # }
/// ```
/// Shortcut method to quickly make a `PATCH` request.
///
/// See also the methods on the [`hpx::RequestBuilder`](./struct.RequestBuilder.html)
/// type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> hpx::Result<()> {
/// let res = hpx::patch("https://httpbin.org/patch")
/// .body("patch content")
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
/// Shortcut method to quickly make an `OPTIONS` request.
///
/// See also the methods on the [`hpx::RequestBuilder`](./struct.RequestBuilder.html)
/// type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> hpx::Result<()> {
/// let res = hpx::options("https://httpbin.org/get").send().await?;
/// # Ok(())
/// # }
/// ```
/// Shortcut method to quickly make a request with a custom HTTP method.
///
/// See also the methods on the [`hpx::RequestBuilder`](./struct.RequestBuilder.html)
/// type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> hpx::Result<()> {
/// use http::Method;
/// let res = hpx::request(Method::TRACE, "https://httpbin.org/trace")
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
/// Shortcut method to quickly make a WebSocket request.
///
/// See also the methods on the
/// [`hpx::ws::WebSocketRequestBuilder`](./ws/struct.WebSocketRequestBuilder.html) type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> hpx::Result<()> {
/// use futures_util::{SinkExt, StreamExt, TryStreamExt};
/// use hpx::{header, ws::message::Message};
///
/// let resp = hpx::websocket("wss://echo.websocket.org")
/// .header(header::USER_AGENT, env!("CARGO_PKG_NAME"))
/// .send()
/// .await?;
///
/// assert_eq!(resp.version(), http::Version::HTTP_11);
///
/// let websocket = resp.into_websocket().await?;
/// if let Some(protocol) = websocket.protocol() {
/// println!("WebSocket subprotocol: {:?}", protocol);
/// }
///
/// let (mut tx, mut rx) = websocket.split();
///
/// tokio::spawn(async move {
/// for i in 1..11 {
/// if let Err(err) = tx.send(Message::text(format!("Hello, World! {i}"))).await {
/// eprintln!("failed to send message: {err}");
/// }
/// }
/// });
///
/// while let Some(message) = rx.try_next().await? {
/// if let Message::Text(text) = message {
/// println!("received: {text}");
/// }
/// }
/// # Ok(())
/// # }
/// ```
use smallvec as _;