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
impl RequestBuilder
Sourcepub fn new(client: Client, builder: Builder) -> RequestBuilder
pub fn new(client: Client, builder: Builder) -> RequestBuilder
Constructs a new request.
Sourcepub fn uri<U: Into<Uri>>(self, uri: U) -> RequestBuilder
pub fn uri<U: Into<Uri>>(self, uri: U) -> RequestBuilder
Set uri to this Request.
Sourcepub fn header<K, V>(self, key: K, value: V) -> RequestBuilderwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
<HeaderValue as TryFrom<V>>::Error: Into<Error>,
pub fn header<K, V>(self, key: K, value: V) -> RequestBuilderwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
<HeaderValue as TryFrom<V>>::Error: Into<Error>,
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(())
}Sourcepub fn header_line<L: Into<String>>(self, lines: L) -> RequestBuilder
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(())
}Sourcepub fn headers(self, headers: HeaderMap) -> RequestBuilder
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.
Sourcepub fn body<T: Into<Body>>(self, body: T) -> RequestBuilder
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
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(())
}Sourcepub fn raw<R: Into<Bytes>>(self, raw: R, unsafe_raw: bool) -> RequestBuilder
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))
}Sourcepub fn build(self) -> Result<Request>
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))
}Sourcepub fn send(self) -> Result<Response>
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?
More examples
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
impl Debug for RequestBuilder
Auto Trait Implementations§
impl !Freeze for RequestBuilder
impl !RefUnwindSafe for RequestBuilder
impl Send for RequestBuilder
impl Sync for RequestBuilder
impl Unpin for RequestBuilder
impl !UnwindSafe for RequestBuilder
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
Mutably borrows from an owned value. Read more