pub struct Request { /* private fields */ }Expand description
A request which can be executed with Client::execute().
Implementations§
Source§impl Request
impl Request
Sourcepub fn builder() -> Builder
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?
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
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<U, R>(uri: U, raw: R, unsafe_raw: bool) -> Request
pub fn raw<U, R>(uri: U, raw: R, unsafe_raw: bool) -> Request
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?
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
impl Request
Sourcepub fn method(&self) -> &Method
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();Sourcepub fn method_mut(&mut self) -> &mut Method
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));Sourcepub fn uri(&self) -> &Uri
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();Sourcepub fn uri_mut(&mut self) -> &mut Uri
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/" );Sourcepub fn headers(&self) -> &HeaderMap
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();Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
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" );Sourcepub fn body(&self) -> Option<&Body>
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();Sourcepub fn version(&self) -> Version
pub fn version(&self) -> Version
Returns the associated version.
§Examples
let request: Request<()> = Request::default();
assert_eq!(request.version(), Version::HTTP_11);Sourcepub fn version_mut(&mut self) -> &mut Version
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);Sourcepub fn raw_request(&self) -> &Option<RawRequest>
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());Sourcepub fn get_command(&self) -> String
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?
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
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 From<&Request> for CommandRecord
impl From<&Request> for CommandRecord
Auto Trait Implementations§
impl !Freeze for Request
impl RefUnwindSafe for Request
impl Send for Request
impl Sync for Request
impl Unpin for Request
impl UnwindSafe for Request
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)