slinger

Struct RequestBuilder

Source
pub struct RequestBuilder { /* private fields */ }
Expand description

A builder to construct the properties of a Request.

To construct a RequestBuilder, refer to the Client documentation.

Implementations§

Source§

impl RequestBuilder

Source

pub fn new(client: Client, builder: Builder) -> RequestBuilder

Constructs a new request.

Source

pub fn uri<U: Into<Uri>>(self, uri: U) -> RequestBuilder

Set uri to this Request.

Source

pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder

Add a Header to this Request.

Examples found in repository?
examples/request_budiler.rs (line 7)
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(())
}
Source

pub fn header_line<L: Into<String>>(self, lines: L) -> RequestBuilder

Add a Header from lines to this Request.

Examples found in repository?
examples/request_budiler.rs (line 8)
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(())
}
Source

pub fn headers(self, headers: HeaderMap) -> RequestBuilder

Add a set of Headers to the existing ones on this Request.

The headers will be merged in to any already set.

Source

pub fn body<T: Into<Body>>(self, body: T) -> RequestBuilder

Set the request body.

Examples found in repository?
examples/request_budiler.rs (line 9)
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
Hide additional examples
examples/base.rs (line 15)
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(())
}
Source

pub fn raw<R: Into<Bytes>>(self, raw: R, unsafe_raw: bool) -> RequestBuilder

set raw request

Examples found in repository?
examples/http_to_command.rs (line 56)
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
fn main() {
  let req: slinger::Request = slinger::Request::builder()
    .uri("http://httpbin.org/get")
    .header("X", "X")
    .body(Bytes::from(b"\x7f\x45\x4c\x46\x01\x00\x02\x03".to_vec()))
    .unwrap()
    .into();
  println!("{}", req.get_command());
  let raw = RAW.replace('\n', "\r\n");
  let raw = [raw.as_bytes(), b"\x7f\x45\x4c\x46\x01\x00\x02\x03"].concat();
  let raw_req = slinger::RequestBuilder::default()
    .raw(raw, true)
    .build()
    .unwrap();
  println!("{}", raw_req.get_command());
  // or from request
  println!("{}", CommandRecord::from(&req))
}
Source

pub fn build(self) -> Result<Request>

Build a Request, which can be inspected, modified and executed with Client::execute().

Examples found in repository?
examples/http_to_command.rs (line 57)
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
fn main() {
  let req: slinger::Request = slinger::Request::builder()
    .uri("http://httpbin.org/get")
    .header("X", "X")
    .body(Bytes::from(b"\x7f\x45\x4c\x46\x01\x00\x02\x03".to_vec()))
    .unwrap()
    .into();
  println!("{}", req.get_command());
  let raw = RAW.replace('\n', "\r\n");
  let raw = [raw.as_bytes(), b"\x7f\x45\x4c\x46\x01\x00\x02\x03"].concat();
  let raw_req = slinger::RequestBuilder::default()
    .raw(raw, true)
    .build()
    .unwrap();
  println!("{}", raw_req.get_command());
  // or from request
  println!("{}", CommandRecord::from(&req))
}
Source

pub fn send(self) -> Result<Response>

Constructs the Request and sends it to the target URL, returning a future Response.

§Errors

This method fails if there was an error while sending request, redirect loop was detected or redirect limit was exhausted.

§Example
let response = slinger::Client::new()
    .get("https://hyper.rs")
    .send()?;
Examples found in repository?
examples/proxy.rs (line 7)
4
5
6
7
8
9
10
fn main() -> Result<(), Box<dyn std::error::Error>> {
  let proxy = slinger::Proxy::parse("http://127.0.0.1:8080").unwrap();
  let client = ClientBuilder::new().proxy(proxy).build().unwrap();
  let resp = client.get("https://httpbin.org/get").send()?;
  println!("{:?}", resp.text());
  Ok(())
}
More examples
Hide additional examples
examples/http_record.rs (line 7)
3
4
5
6
7
8
9
10
11
12
fn main() -> Result<(), Box<dyn std::error::Error>> {
  let client = ClientBuilder::new().build().unwrap();
  let resp = client
    .get("https://httpbin.org/redirect-to?url=https%3A%2F%2Fhttpbin.org%2Fget")
    .send()?;
  println!("{:?}", resp.request());
  let record = resp.http_record().unwrap();
  println!("{:?}", record.len());
  Ok(())
}
examples/redirect.rs (line 35)
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(())
}
examples/request_budiler.rs (line 10)
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(())
}
examples/smuggling.rs (line 30)
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);
}
examples/base.rs (line 16)
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§

Source§

impl Debug for RequestBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RequestBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.