pub struct ClientBuilder { /* private fields */ }Expand description
A ClientBuilder can be used to create a Client with custom configuration.
§Example
use std::time::Duration;
let client = slinger::Client::builder()
.timeout(Some(Duration::from_secs(10)))
.build()?;Implementations§
Source§impl ClientBuilder
impl ClientBuilder
Sourcepub fn new() -> ClientBuilder
pub fn new() -> ClientBuilder
Constructs a new ClientBuilder.
This is the same as Client::builder().
Examples found in repository?
More examples
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
fn customize() -> Result<(), Box<dyn std::error::Error>> {
let redirect = slinger::redirect::Policy::Custom(custom);
let client = ClientBuilder::new().redirect(redirect).build().unwrap();
let resp = client
.get("http://httpbin.org/base64/c2xpbmdlcgo%3D")
.send()?;
assert!(resp.text().unwrap_or_default().contains("slinger=awesome"));
Ok(())
}
fn limit(max_redirect: usize) -> Result<(), Box<dyn std::error::Error>> {
let redirect = slinger::redirect::Policy::Limit(max_redirect);
let client = ClientBuilder::new().redirect(redirect).build().unwrap();
let resp = client.get("http://httpbin.org/redirect/10").send()?;
let record = resp.extensions().get::<Vec<HTTPRecord>>().unwrap();
assert_eq!(record.len(), 3);
Ok(())
}
fn jump() -> Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::new().build().unwrap();
let resp = client
.get("http://httpbin.org/redirect-to?url=http://www.example.com/")
.send()?;
let record = resp.extensions().get::<Vec<HTTPRecord>>().unwrap();
println!("{:?}", record);
assert_eq!(record.len(), 2);
Ok(())
}
fn only_same_host() -> Result<(), Box<dyn std::error::Error>> {
let redirect = slinger::redirect::Policy::Custom(slinger::redirect::only_same_host);
let client = ClientBuilder::new().redirect(redirect).build().unwrap();
let resp = client
.get("http://httpbin.org/redirect-to?url=http://www.example.com/")
.send()?;
let record = resp.extensions().get::<Vec<HTTPRecord>>().unwrap();
println!("{:?}", record);
assert_eq!(record.len(), 1);
let redirect_record = resp.redirect_record().unwrap();
println!("{:?}", redirect_record);
assert_eq!(
redirect_record.next,
Some(http::Uri::from_static("http://www.example.com/"))
);
Ok(())
}3 4 5 6 7 8 9 10 11 12 13 14 15 16
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::new().build().unwrap();
let resp = client
.post("https://httpbin.org/post")
.header("XX", "XX")
.header_line("X: X")
.body(b"data".as_slice())
.send()?;
println!("{:?}", resp.text());
let u = http::Uri::from_static("https://httpbin.org/post");
let raw = Request::raw(u, "", true);
println!("{:?}", raw);
Ok(())
}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
fn cve_2020_11724() {
let raw: &str = r#"GET /test1 HTTP/1.1
Host: 192.168.83.196:8081
Content-Length: 42
Transfer-Encoding: chunked
0
GET /test1 HTTP/1.1
Host: 192.168.83.196:8081
X: GET http://192.168.83.1:8080/admin.jsp HTTP/1.0
"#;
// let proxy = slinger::Proxy::parse("http://127.0.0.1:8080").unwrap();
let client = ClientBuilder::new().build().unwrap();
// replace \n to \r\n
let raw = raw.replace('\n', "\r\n");
let resp = client
.raw("http://127.0.0.1:9015/", raw, true)
.send()
.unwrap();
println!("{:?}", resp.text());
let command = resp.request().unwrap().get_command();
println!("{}", command);
}
fn nginx() {
let raw = r#"
GET /a HTTP/1.1
Host: localhost
Content-Length: 56
GET /_hidden/index.html HTTP/1.1
Host: notlocalhost
"#;
let client = ClientBuilder::new().build().unwrap();
// replace \n to \r\n
let raw = raw.replace('\n', "\r\n");
let resp = client
.raw("http://127.0.0.1:9015/", raw, true)
.send()
.unwrap();
println!("{:?}", resp.text());
let command = resp.request().unwrap().get_command();
println!("{}", command);
}4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::new()
.user_agent(HeaderValue::from_static(
"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0",
))
.build()
.unwrap();
let resp = slinger::get("http://httpbin.org/get")?;
println!("{:?}", resp.body());
let resp = client
.post("http://httpbin.org/post")
.body(b"test".to_vec())
.send()?;
println!("{:?}", resp.text());
let req = Request::builder()
.uri("http://httpbin.org/head")
.method("HEAD")
.header("pragma", "akamai-x-cache-on")
.body(None)
.unwrap();
let resp = client.execute(req).unwrap();
println!("{:?}", resp);
Ok(())
}Sourcepub fn build(self) -> Result<Client>
pub fn build(self) -> Result<Client>
Returns a Client that uses this ClientBuilder configuration.
§Errors
This method fails if TLS backend cannot be initialized, or the resolver cannot load the system configuration.
§Panics
See docs on
slinger::client for details.
Examples found in repository?
More examples
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
fn customize() -> Result<(), Box<dyn std::error::Error>> {
let redirect = slinger::redirect::Policy::Custom(custom);
let client = ClientBuilder::new().redirect(redirect).build().unwrap();
let resp = client
.get("http://httpbin.org/base64/c2xpbmdlcgo%3D")
.send()?;
assert!(resp.text().unwrap_or_default().contains("slinger=awesome"));
Ok(())
}
fn limit(max_redirect: usize) -> Result<(), Box<dyn std::error::Error>> {
let redirect = slinger::redirect::Policy::Limit(max_redirect);
let client = ClientBuilder::new().redirect(redirect).build().unwrap();
let resp = client.get("http://httpbin.org/redirect/10").send()?;
let record = resp.extensions().get::<Vec<HTTPRecord>>().unwrap();
assert_eq!(record.len(), 3);
Ok(())
}
fn jump() -> Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::new().build().unwrap();
let resp = client
.get("http://httpbin.org/redirect-to?url=http://www.example.com/")
.send()?;
let record = resp.extensions().get::<Vec<HTTPRecord>>().unwrap();
println!("{:?}", record);
assert_eq!(record.len(), 2);
Ok(())
}
fn only_same_host() -> Result<(), Box<dyn std::error::Error>> {
let redirect = slinger::redirect::Policy::Custom(slinger::redirect::only_same_host);
let client = ClientBuilder::new().redirect(redirect).build().unwrap();
let resp = client
.get("http://httpbin.org/redirect-to?url=http://www.example.com/")
.send()?;
let record = resp.extensions().get::<Vec<HTTPRecord>>().unwrap();
println!("{:?}", record);
assert_eq!(record.len(), 1);
let redirect_record = resp.redirect_record().unwrap();
println!("{:?}", redirect_record);
assert_eq!(
redirect_record.next,
Some(http::Uri::from_static("http://www.example.com/"))
);
Ok(())
}3 4 5 6 7 8 9 10 11 12 13 14 15 16
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::new().build().unwrap();
let resp = client
.post("https://httpbin.org/post")
.header("XX", "XX")
.header_line("X: X")
.body(b"data".as_slice())
.send()?;
println!("{:?}", resp.text());
let u = http::Uri::from_static("https://httpbin.org/post");
let raw = Request::raw(u, "", true);
println!("{:?}", raw);
Ok(())
}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
fn cve_2020_11724() {
let raw: &str = r#"GET /test1 HTTP/1.1
Host: 192.168.83.196:8081
Content-Length: 42
Transfer-Encoding: chunked
0
GET /test1 HTTP/1.1
Host: 192.168.83.196:8081
X: GET http://192.168.83.1:8080/admin.jsp HTTP/1.0
"#;
// let proxy = slinger::Proxy::parse("http://127.0.0.1:8080").unwrap();
let client = ClientBuilder::new().build().unwrap();
// replace \n to \r\n
let raw = raw.replace('\n', "\r\n");
let resp = client
.raw("http://127.0.0.1:9015/", raw, true)
.send()
.unwrap();
println!("{:?}", resp.text());
let command = resp.request().unwrap().get_command();
println!("{}", command);
}
fn nginx() {
let raw = r#"
GET /a HTTP/1.1
Host: localhost
Content-Length: 56
GET /_hidden/index.html HTTP/1.1
Host: notlocalhost
"#;
let client = ClientBuilder::new().build().unwrap();
// replace \n to \r\n
let raw = raw.replace('\n', "\r\n");
let resp = client
.raw("http://127.0.0.1:9015/", raw, true)
.send()
.unwrap();
println!("{:?}", resp.text());
let command = resp.request().unwrap().get_command();
println!("{}", command);
}4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::new()
.user_agent(HeaderValue::from_static(
"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0",
))
.build()
.unwrap();
let resp = slinger::get("http://httpbin.org/get")?;
println!("{:?}", resp.body());
let resp = client
.post("http://httpbin.org/post")
.body(b"test".to_vec())
.send()?;
println!("{:?}", resp.text());
let req = Request::builder()
.uri("http://httpbin.org/head")
.method("HEAD")
.header("pragma", "akamai-x-cache-on")
.body(None)
.unwrap();
let resp = client.execute(req).unwrap();
println!("{:?}", resp);
Ok(())
}Sourcepub fn user_agent<V>(self, value: V) -> ClientBuilderwhere
V: Into<HeaderValue>,
pub fn user_agent<V>(self, value: V) -> ClientBuilderwhere
V: Into<HeaderValue>,
Sets the User-Agent header to be used by this client.
§Example
fn doc() -> Result<(), slinger::Error> {
let ua = HeaderValue::from_static("XX_UA");
let client = slinger::Client::builder()
.user_agent(ua)
.build()?;
let res = client.get("https://www.rust-lang.org").send()?;Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::new()
.user_agent(HeaderValue::from_static(
"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0",
))
.build()
.unwrap();
let resp = slinger::get("http://httpbin.org/get")?;
println!("{:?}", resp.body());
let resp = client
.post("http://httpbin.org/post")
.body(b"test".to_vec())
.send()?;
println!("{:?}", resp.text());
let req = Request::builder()
.uri("http://httpbin.org/head")
.method("HEAD")
.header("pragma", "akamai-x-cache-on")
.body(None)
.unwrap();
let resp = client.execute(req).unwrap();
println!("{:?}", resp);
Ok(())
}Sourcepub fn default_headers(self, headers: HeaderMap) -> ClientBuilder
pub fn default_headers(self, headers: HeaderMap) -> ClientBuilder
Sets the default headers for every request.
§Example
use slinger::http::header;
let mut headers = header::HeaderMap::new();
headers.insert("X-MY-HEADER", header::HeaderValue::from_static("value"));
headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret"));
// Consider marking security-sensitive headers with `set_sensitive`.
let mut auth_value = header::HeaderValue::from_static("secret");
auth_value.set_sensitive(true);
headers.insert(header::AUTHORIZATION, auth_value);
// get a client builder
let client = slinger::Client::builder()
.default_headers(headers)
.build()?;
let res = client.get("https://www.rust-lang.org").send()?;Sourcepub fn redirect(self, policy: Policy) -> ClientBuilder
pub fn redirect(self, policy: Policy) -> ClientBuilder
Set a redirect::Policy for this client.
Default will follow redirects up to a maximum of 10.
Examples found in repository?
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
fn customize() -> Result<(), Box<dyn std::error::Error>> {
let redirect = slinger::redirect::Policy::Custom(custom);
let client = ClientBuilder::new().redirect(redirect).build().unwrap();
let resp = client
.get("http://httpbin.org/base64/c2xpbmdlcgo%3D")
.send()?;
assert!(resp.text().unwrap_or_default().contains("slinger=awesome"));
Ok(())
}
fn limit(max_redirect: usize) -> Result<(), Box<dyn std::error::Error>> {
let redirect = slinger::redirect::Policy::Limit(max_redirect);
let client = ClientBuilder::new().redirect(redirect).build().unwrap();
let resp = client.get("http://httpbin.org/redirect/10").send()?;
let record = resp.extensions().get::<Vec<HTTPRecord>>().unwrap();
assert_eq!(record.len(), 3);
Ok(())
}
fn jump() -> Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::new().build().unwrap();
let resp = client
.get("http://httpbin.org/redirect-to?url=http://www.example.com/")
.send()?;
let record = resp.extensions().get::<Vec<HTTPRecord>>().unwrap();
println!("{:?}", record);
assert_eq!(record.len(), 2);
Ok(())
}
fn only_same_host() -> Result<(), Box<dyn std::error::Error>> {
let redirect = slinger::redirect::Policy::Custom(slinger::redirect::only_same_host);
let client = ClientBuilder::new().redirect(redirect).build().unwrap();
let resp = client
.get("http://httpbin.org/redirect-to?url=http://www.example.com/")
.send()?;
let record = resp.extensions().get::<Vec<HTTPRecord>>().unwrap();
println!("{:?}", record);
assert_eq!(record.len(), 1);
let redirect_record = resp.redirect_record().unwrap();
println!("{:?}", redirect_record);
assert_eq!(
redirect_record.next,
Some(http::Uri::from_static("http://www.example.com/"))
);
Ok(())
}Sourcepub fn referer(self, enable: bool) -> ClientBuilder
pub fn referer(self, enable: bool) -> ClientBuilder
Enable or disable automatic setting of the Referer header.
Default is true.
Sourcepub fn proxy(self, proxy: Proxy) -> ClientBuilder
pub fn proxy(self, proxy: Proxy) -> ClientBuilder
Add a Proxy to the list of proxies the Client will use.
§Note
Adding a proxy will disable the automatic usage of the “system” proxy.
Sourcepub fn timeout(self, timeout: Option<Duration>) -> ClientBuilder
pub fn timeout(self, timeout: Option<Duration>) -> ClientBuilder
Set a timeout for connect, read and write operations of a Client.
Default is 30 seconds.
Pass None to disable timeout.
Sourcepub fn connect_timeout(self, timeout: Option<Duration>) -> ClientBuilder
pub fn connect_timeout(self, timeout: Option<Duration>) -> ClientBuilder
Set a timeout for only the connect phase of a Client.
Default is 10 seconds.
Sourcepub fn read_timeout(self, timeout: Option<Duration>) -> ClientBuilder
pub fn read_timeout(self, timeout: Option<Duration>) -> ClientBuilder
Set a timeout for only the read phase of a Client.
Default is 30 seconds.
Sourcepub fn tcp_nodelay(self, enabled: bool) -> ClientBuilder
pub fn tcp_nodelay(self, enabled: bool) -> ClientBuilder
Set whether sockets have TCP_NODELAY enabled.
Default is true.
Sourcepub fn danger_accept_invalid_hostnames(
self,
accept_invalid_hostname: bool,
) -> ClientBuilder
pub fn danger_accept_invalid_hostnames( self, accept_invalid_hostname: bool, ) -> ClientBuilder
Controls the use of hostname verification.
Defaults to false.
§Warning
You should think very carefully before you use this method. If hostname verification is not used, any valid certificate for any site will be trusted for use from any other. This introduces a significant vulnerability to man-in-the-middle attacks.
§Optional
This requires the optional tls feature to be enabled.
Sourcepub fn danger_accept_invalid_certs(
self,
accept_invalid_certs: bool,
) -> ClientBuilder
pub fn danger_accept_invalid_certs( self, accept_invalid_certs: bool, ) -> ClientBuilder
Controls the use of certificate validation.
Defaults to false.
§Warning
You should think very carefully before using this method. If invalid certificates are trusted, any certificate for any site will be trusted for use. This includes expired certificates. This introduces significant vulnerabilities, and should only be used as a last resort.
Trait Implementations§
Source§impl Clone for ClientBuilder
impl Clone for ClientBuilder
Source§fn clone(&self) -> ClientBuilder
fn clone(&self) -> ClientBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ClientBuilder
impl Debug for ClientBuilder
Auto Trait Implementations§
impl !Freeze for ClientBuilder
impl RefUnwindSafe for ClientBuilder
impl Send for ClientBuilder
impl Sync for ClientBuilder
impl Unpin for ClientBuilder
impl UnwindSafe for ClientBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)