use std::convert::TryFrom;
use std::fmt;
use std::future::Future;
use std::io::Write;
use std::time::Duration;
use base64::write::EncoderWriter as Base64Encoder;
use serde::Serialize;
#[cfg(feature = "json")]
use serde_json;
use super::body::Body;
use super::client::{Client, Pending};
use super::multipart;
use super::response::Response;
use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_LENGTH, CONTENT_TYPE};
use crate::{Method, Url};
use http::{Request as HttpRequest, request::Parts};
pub struct Request {
method: Method,
url: Url,
headers: HeaderMap,
body: Option<Body>,
timeout: Option<Duration>,
}
#[must_use = "RequestBuilder does nothing until you 'send' it"]
pub struct RequestBuilder {
client: Client,
request: crate::Result<Request>,
}
impl Request {
#[inline]
pub fn new(method: Method, url: Url) -> Self {
Request {
method,
url,
headers: HeaderMap::new(),
body: None,
timeout: None
}
}
#[inline]
pub fn method(&self) -> &Method {
&self.method
}
#[inline]
pub fn method_mut(&mut self) -> &mut Method {
&mut self.method
}
#[inline]
pub fn url(&self) -> &Url {
&self.url
}
#[inline]
pub fn url_mut(&mut self) -> &mut Url {
&mut self.url
}
#[inline]
pub fn headers(&self) -> &HeaderMap {
&self.headers
}
#[inline]
pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.headers
}
#[inline]
pub fn body(&self) -> Option<&Body> {
self.body.as_ref()
}
#[inline]
pub fn body_mut(&mut self) -> &mut Option<Body> {
&mut self.body
}
#[inline]
pub fn timeout(&self) -> Option<&Duration> {
self.timeout.as_ref()
}
#[inline]
pub fn timeout_mut(&mut self) -> &mut Option<Duration> {
&mut self.timeout
}
pub fn try_clone(&self) -> Option<Request> {
let body = match self.body.as_ref() {
Some(ref body) => Some(body.try_clone()?),
None => None,
};
let mut req = Request::new(self.method().clone(), self.url().clone());
*req.timeout_mut() = self.timeout().cloned();
*req.headers_mut() = self.headers().clone();
req.body = body;
Some(req)
}
pub(super) fn pieces(self) -> (Method, Url, HeaderMap, Option<Body>, Option<Duration>) {
(self.method, self.url, self.headers, self.body, self.timeout)
}
}
impl RequestBuilder {
pub(super) fn new(client: Client, request: crate::Result<Request>) -> RequestBuilder {
let mut builder = RequestBuilder { client, request };
let auth = builder
.request
.as_mut()
.ok()
.and_then(|req| extract_authority(&mut req.url));
if let Some((username, password)) = auth {
builder.basic_auth(username, password)
} else {
builder
}
}
pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
{
self.header_sensitive(key, value, false)
}
fn header_sensitive<K, V>(mut self, key: K, value: V, sensitive: bool) -> RequestBuilder
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
{
let mut error = None;
if let Ok(ref mut req) = self.request {
match <HeaderName as TryFrom<K>>::try_from(key) {
Ok(key) => match <HeaderValue as TryFrom<V>>::try_from(value) {
Ok(mut value) => {
value.set_sensitive(sensitive);
req.headers_mut().append(key, value);
}
Err(e) => error = Some(crate::error::builder(e.into())),
},
Err(e) => error = Some(crate::error::builder(e.into())),
};
}
if let Some(err) = error {
self.request = Err(err);
}
self
}
pub fn headers(mut self, headers: crate::header::HeaderMap) -> RequestBuilder {
if let Ok(ref mut req) = self.request {
crate::util::replace_headers(req.headers_mut(), headers);
}
self
}
pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder
where
U: fmt::Display,
P: fmt::Display,
{
let mut header_value = b"Basic ".to_vec();
{
let mut encoder = Base64Encoder::new(&mut header_value, base64::STANDARD);
write!(encoder, "{}:", username).unwrap();
if let Some(password) = password {
write!(encoder, "{}", password).unwrap();
}
}
self.header_sensitive(crate::header::AUTHORIZATION, header_value, true)
}
pub fn bearer_auth<T>(self, token: T) -> RequestBuilder
where
T: fmt::Display,
{
let header_value = format!("Bearer {}", token);
self.header_sensitive(crate::header::AUTHORIZATION, header_value, true)
}
pub fn body<T: Into<Body>>(mut self, body: T) -> RequestBuilder {
if let Ok(ref mut req) = self.request {
*req.body_mut() = Some(body.into());
}
self
}
pub fn timeout(mut self, timeout: Duration) -> RequestBuilder {
if let Ok(ref mut req) = self.request {
*req.timeout_mut() = Some(timeout);
}
self
}
pub fn multipart(self, mut multipart: multipart::Form) -> RequestBuilder {
let mut builder = self.header(
CONTENT_TYPE,
format!("multipart/form-data; boundary={}", multipart.boundary()).as_str(),
);
builder = match multipart.compute_length() {
Some(length) => builder.header(CONTENT_LENGTH, length),
None => builder,
};
if let Ok(ref mut req) = builder.request {
*req.body_mut() = Some(multipart.stream())
}
builder
}
pub fn query<T: Serialize + ?Sized>(mut self, query: &T) -> RequestBuilder {
let mut error = None;
if let Ok(ref mut req) = self.request {
let url = req.url_mut();
let mut pairs = url.query_pairs_mut();
let serializer = serde_urlencoded::Serializer::new(&mut pairs);
if let Err(err) = query.serialize(serializer) {
error = Some(crate::error::builder(err));
}
}
if let Ok(ref mut req) = self.request {
if let Some("") = req.url().query() {
req.url_mut().set_query(None);
}
}
if let Some(err) = error {
self.request = Err(err);
}
self
}
pub fn form<T: Serialize + ?Sized>(mut self, form: &T) -> RequestBuilder {
let mut error = None;
if let Ok(ref mut req) = self.request {
match serde_urlencoded::to_string(form) {
Ok(body) => {
req.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_static("application/x-www-form-urlencoded"),
);
*req.body_mut() = Some(body.into());
}
Err(err) => error = Some(crate::error::builder(err)),
}
}
if let Some(err) = error {
self.request = Err(err);
}
self
}
#[cfg(feature = "json")]
pub fn json<T: Serialize + ?Sized>(mut self, json: &T) -> RequestBuilder {
let mut error = None;
if let Ok(ref mut req) = self.request {
match serde_json::to_vec(json) {
Ok(body) => {
req.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
*req.body_mut() = Some(body.into());
}
Err(err) => error = Some(crate::error::builder(err)),
}
}
if let Some(err) = error {
self.request = Err(err);
}
self
}
pub fn fetch_mode_no_cors(self) -> RequestBuilder {
self
}
pub fn build(self) -> crate::Result<Request> {
self.request
}
pub fn send(self) -> impl Future<Output = Result<Response, crate::Error>> {
match self.request {
Ok(req) => self.client.execute_request(req),
Err(err) => Pending::new_err(err),
}
}
pub fn try_clone(&self) -> Option<RequestBuilder> {
self.request
.as_ref()
.ok()
.and_then(|req| req.try_clone())
.map(|req| RequestBuilder {
client: self.client.clone(),
request: Ok(req),
})
}
}
impl fmt::Debug for Request {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_request_fields(&mut f.debug_struct("Request"), self).finish()
}
}
impl fmt::Debug for RequestBuilder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut builder = f.debug_struct("RequestBuilder");
match self.request {
Ok(ref req) => fmt_request_fields(&mut builder, req).finish(),
Err(ref err) => builder.field("error", err).finish(),
}
}
}
fn fmt_request_fields<'a, 'b>(
f: &'a mut fmt::DebugStruct<'a, 'b>,
req: &Request,
) -> &'a mut fmt::DebugStruct<'a, 'b> {
f.field("method", &req.method)
.field("url", &req.url)
.field("headers", &req.headers)
}
pub(crate) fn extract_authority(url: &mut Url) -> Option<(String, Option<String>)> {
use percent_encoding::percent_decode;
if url.has_authority() {
let username: String = percent_decode(url.username().as_bytes())
.decode_utf8()
.ok()?
.into();
let password = url.password().and_then(|pass| {
percent_decode(pass.as_bytes())
.decode_utf8()
.ok()
.map(String::from)
});
if !username.is_empty() || password.is_some() {
url
.set_username("")
.expect("has_authority means set_username shouldn't fail");
url
.set_password(None)
.expect("has_authority means set_password shouldn't fail");
return Some((username, password))
}
}
None
}
impl<T> TryFrom<HttpRequest<T>> for Request where T:Into<Body>{
type Error = crate::Error;
fn try_from(req: HttpRequest<T>) -> crate::Result<Self> {
let (parts, body) = req.into_parts();
let Parts {
method,
uri,
headers,
..
} = parts;
let url = Url::parse(&uri.to_string())
.map_err(crate::error::builder)?;
Ok(Request {
method,
url,
headers,
body: Some(body.into()),
timeout: None,
})
}
}
#[cfg(test)]
mod tests {
use super::{Client, HttpRequest, Request};
use crate::Method;
use serde::Serialize;
use std::collections::BTreeMap;
use std::convert::TryFrom;
#[test]
fn add_query_append() {
let client = Client::new();
let some_url = "https://google.com/";
let r = client.get(some_url);
let r = r.query(&[("foo", "bar")]);
let r = r.query(&[("qux", 3)]);
let req = r.build().expect("request is valid");
assert_eq!(req.url().query(), Some("foo=bar&qux=3"));
}
#[test]
fn add_query_append_same() {
let client = Client::new();
let some_url = "https://google.com/";
let r = client.get(some_url);
let r = r.query(&[("foo", "a"), ("foo", "b")]);
let req = r.build().expect("request is valid");
assert_eq!(req.url().query(), Some("foo=a&foo=b"));
}
#[test]
fn add_query_struct() {
#[derive(Serialize)]
struct Params {
foo: String,
qux: i32,
}
let client = Client::new();
let some_url = "https://google.com/";
let r = client.get(some_url);
let params = Params {
foo: "bar".into(),
qux: 3,
};
let r = r.query(¶ms);
let req = r.build().expect("request is valid");
assert_eq!(req.url().query(), Some("foo=bar&qux=3"));
}
#[test]
fn add_query_map() {
let mut params = BTreeMap::new();
params.insert("foo", "bar");
params.insert("qux", "three");
let client = Client::new();
let some_url = "https://google.com/";
let r = client.get(some_url);
let r = r.query(¶ms);
let req = r.build().expect("request is valid");
assert_eq!(req.url().query(), Some("foo=bar&qux=three"));
}
#[test]
fn test_replace_headers() {
use http::HeaderMap;
let mut headers = HeaderMap::new();
headers.insert("foo", "bar".parse().unwrap());
headers.append("foo", "baz".parse().unwrap());
let client = Client::new();
let req = client
.get("https://hyper.rs")
.header("im-a", "keeper")
.header("foo", "pop me")
.headers(headers)
.build()
.expect("request build");
assert_eq!(req.headers()["im-a"], "keeper");
let foo = req.headers().get_all("foo").iter().collect::<Vec<_>>();
assert_eq!(foo.len(), 2);
assert_eq!(foo[0], "bar");
assert_eq!(foo[1], "baz");
}
#[test]
fn normalize_empty_query() {
let client = Client::new();
let some_url = "https://google.com/";
let empty_query: &[(&str, &str)] = &[];
let req = client
.get(some_url)
.query(empty_query)
.build()
.expect("request build");
assert_eq!(req.url().query(), None);
assert_eq!(req.url().as_str(), "https://google.com/");
}
#[test]
fn try_clone_reusable() {
let client = Client::new();
let builder = client.post("http://httpbin.org/post")
.header("foo", "bar")
.body("from a &str!");
let req = builder
.try_clone()
.expect("clone successful")
.build()
.expect("request is valid");
assert_eq!(req.url().as_str(), "http://httpbin.org/post");
assert_eq!(req.method(), Method::POST);
assert_eq!(req.headers()["foo"], "bar");
}
#[test]
fn try_clone_no_body() {
let client = Client::new();
let builder = client.get("http://httpbin.org/get");
let req = builder
.try_clone()
.expect("clone successful")
.build()
.expect("request is valid");
assert_eq!(req.url().as_str(), "http://httpbin.org/get");
assert_eq!(req.method(), Method::GET);
assert!(req.body().is_none());
}
#[test]
#[cfg(feature = "stream")]
fn try_clone_stream() {
let chunks: Vec<Result<_, ::std::io::Error>> = vec![
Ok("hello"),
Ok(" "),
Ok("world"),
];
let stream = futures_util::stream::iter(chunks);
let client = Client::new();
let builder = client.get("http://httpbin.org/get")
.body(super::Body::wrap_stream(stream));
let clone = builder.try_clone();
assert!(clone.is_none());
}
#[test]
fn convert_url_authority_into_basic_auth() {
let client = Client::new();
let some_url = "https://Aladdin:open sesame@localhost/";
let req = client
.get(some_url)
.build()
.expect("request build");
assert_eq!(req.url().as_str(), "https://localhost/");
assert_eq!(req.headers()["authorization"], "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
}
#[test]
fn test_basic_auth_sensitive_header() {
let client = Client::new();
let some_url = "https://localhost/";
let req = client
.get(some_url)
.basic_auth("Aladdin", Some("open sesame"))
.build()
.expect("request build");
assert_eq!(req.url().as_str(), "https://localhost/");
assert_eq!(req.headers()["authorization"], "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
assert_eq!(req.headers()["authorization"].is_sensitive(), true);
}
#[test]
fn test_bearer_auth_sensitive_header() {
let client = Client::new();
let some_url = "https://localhost/";
let req = client
.get(some_url)
.bearer_auth("Hold my bear")
.build()
.expect("request build");
assert_eq!(req.url().as_str(), "https://localhost/");
assert_eq!(req.headers()["authorization"], "Bearer Hold my bear");
assert_eq!(req.headers()["authorization"].is_sensitive(), true);
}
#[test]
fn convert_from_http_request() {
let http_request = HttpRequest::builder().method("GET")
.uri("http://localhost/")
.header("User-Agent", "my-awesome-agent/1.0")
.body("test test test")
.unwrap();
let req: Request = Request::try_from(http_request).unwrap();
assert_eq!(req.body().is_none(), false);
let test_data = b"test test test";
assert_eq!(req.body().unwrap().as_bytes(), Some(&test_data[..]));
let headers = req.headers();
assert_eq!(headers.get("User-Agent").unwrap(), "my-awesome-agent/1.0");
assert_eq!(req.method(), Method::GET);
assert_eq!(req.url().as_str(), "http://localhost/");
}
}