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
//! [![GitHub]](https://github.com/emo-crab/slinger) [![crates-io]](https://crates.io/crates/slinger) [![docs-rs]](crate)
//!
//! [GitHub]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! # slinger (投石器)
//!
//! The `slinger` crate provides a convenient, low-level HTTP
//! [`Client`].
//!
//! It handles many of the things that most people just expect an HTTP client
//! to do for them.
//!
//! - Customizable [redirect policy](#redirect-policies)
//! - HTTP [Proxies](#proxies)
//! - Uses [TLS](#tls) by default
//! - Cookies
//!
//!
//! Additional learning resources include:
//!
//! - [Slinger Repository Examples](https://github.com/emo-crab/slinger/tree/master/examples)
//!
//! ## Making a GET request
//!
//! For a single request, you can use the [`get`] shortcut method.
//!
//!```rust
//! async fn run() -> Result<(), Box<dyn std::error::Error>> {
//! let body = slinger::get("https://httpbin.org/get").await?
//! .text()?;
//! println!("body = {body:?}");
//! Ok(())
//! }
//! ```
//!
//! **NOTE**: If you plan to perform multiple requests, it is best to create a
//! [`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`]. 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 `slinger::Body` constructors.
//!
//!```rust
//! # use slinger::Error;
//! #
//! # async fn run() -> Result<(), Error> {
//! let client = slinger::Client::new();
//! let res = client.post("http://httpbin.org/post")
//! .body("the exact body that is sent")
//! .send().await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Redirect Policies
//!
//! By default, a `Client` will automatically handle HTTP redirects, having a
//! maximum redirect chain of 10 hops. To customize this behavior, a
//! [`redirect::Policy`][redirect] can be used with a `ClientBuilder`.
//!
//! ## Cookies
//!
//! The automatic storing and sending of session cookies can be enabled with
//! the [`cookie_store`][client::ClientBuilder::cookie_store] method on `ClientBuilder`.
//!
//! ## Proxies
//!```rust
//! async fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
//! let proxy = slinger::Proxy::parse("http://user:pass@127.0.0.1:1080").unwrap();
//! // let proxy = slinger::Proxy::parse("socks5://user:pass@127.0.0.1:1080").unwrap();
//! let client = slinger::ClientBuilder::default().proxy(proxy).build().unwrap();
//! let resp = client.get("https://httpbin.org/get").send().await?;
//! println!("{:?}", resp);
//! Ok(())
//! }
//!```
//! ## TLS
//!
//! A `Client` will use transport layer security (TLS) by default to connect to
//! HTTPS destinations.
//!
//! - Additional server certificates can be configured on a `ClientBuilder`
//! with the [`tls::Certificate`][tls::Certificate] type.
//! - Client certificates can be added to a `ClientBuilder` with the
//! [`tls::Identity`][tls::Identity] type.
//! - Various parts of TLS can also be configured or even disabled on the
//! `ClientBuilder`.
//!
//! ### TLS Backend
//!
//! When the `rustls` feature is enabled (recommended), slinger uses rustls as the default
//! TLS backend through the unified `CustomTlsConnector` interface. This provides a consistent
//! API regardless of which TLS implementation you use.
//!
//! You can also provide your own TLS implementation by implementing the `CustomTlsConnector`
//! trait and setting it with `ConnectorBuilder::custom_tls_connector()`.
//!
//! ## Optional Features
//!
//! The following are a list of [Cargo features]\[cargo-features\] that can be
//! enabled or disabled:
//!
//! - **charset**: Improved support for decoding text.
//! - **cookie**: Provides cookie session support.
//! - **dns**: Provides custom DNS server support via hickory-dns.
//! - **tls**: Base TLS feature flag. Enables TLS types and interfaces.
//! - **rustls**: Provides HTTPS support via rustls (requires `tls` feature). This is the recommended TLS backend.
//! - **serde**: Provides serialization and deserialization support.
//! - **gzip**: Provides response body gzip decompression.
//! - **http2**: Provides HTTP/2 support (requires a TLS backend).
//!
//! ### Custom DNS Resolution
//!
//! When the `dns` feature is enabled, you can use custom DNS servers for hostname resolution:
//!
//! ```ignore
//! use slinger::{Client, dns::DnsResolver};
//!
//! # fn example() -> Result<(), slinger::Error> {
//! // Create a resolver with Google's public DNS servers
//! let resolver = DnsResolver::new(vec![
//! "8.8.8.8:53".parse().unwrap(),
//! "8.8.4.4:53".parse().unwrap(),
//! ])?;
//!
//! let client = Client::builder()
//! .dns_resolver(resolver)
//! .build()?;
//! # Ok(())
//! # }
//! ```
//!
//! ### TLS Backend Selection
//!
//! When the `tls` feature is enabled, you have several options:
//! - Enable `rustls` for the default pure Rust TLS implementation (recommended)
//! - Or provide a custom TLS connector by implementing the `CustomTlsConnector` trait
//!
//! ### Custom TLS Backend
//!
//! If you enable the `tls` feature, you can implement your own TLS handshake logic
//! using any TLS library (OpenSSL, BoringSSL, native-tls, etc.):
//!
//! ```ignore
//! use slinger::{ConnectorBuilder, CustomTlsConnector};
//!
//! struct MyTlsConnector;
//!
//! impl CustomTlsConnector for MyTlsConnector {
//! fn connect<'a>(&'a self, domain: &'a str, stream: Socket)
//! -> Pin<Box<dyn Future<Output = Result<Socket>> + Send + 'a>>
//! {
//! // Your TLS implementation here
//! todo!()
//! }
//! }
//!
//! let connector = ConnectorBuilder::default()
//! .custom_tls_connector(Arc::new(MyTlsConnector))
//! .build()?;
//! ```
//!
//! When the `rustls` feature is enabled, rustls is provided as the default implementation
//! but you can still override it with your own custom connector if needed.
//!
/// DNS resolver module for custom DNS server support.
/// record info
/// Redirect Handling
pub use Body;
use Bytes;
pub use ;
pub use ;
pub use ;
pub use http;
pub use http_serde;
pub use Proxy;
pub use ;
pub use ;
pub use Socket;
pub use StreamWrapper;
/// Shortcut method to quickly make a `GET` request.
///
/// See also the methods on the [`slinger::Response`](./struct.Response.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() -> Result<(), slinger::Error> {
/// let body = slinger::get("https://www.rust-lang.org").await?
/// .text()?;
/// # Ok(())
/// # }
/// ```
///
pub async
/// Shortcut method to quickly make a `RAW` request.
///
/// See also the methods on the [`slinger::Response`](./struct.Response.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() -> Result<(), slinger::Error> {
/// let body = slinger::raw("http://httpbin.org","GET /robots HTTP/1.1\r\n\r\n",true).await?
/// .text()?;
/// # Ok(())
/// # }
/// ```
///
pub async
pub const CR_LF: & = &;
pub const SPACE: & = &;
pub const COLON_SPACE: & = &;