Use base64 to fully encode basic auth creds

Change basic_auth to use less allocations
This commit is contained in:
Douman
2018-07-27 22:39:09 +03:00
committed by Sean McArthur
parent 12cfbafc28
commit a25f62f4cb
2 changed files with 14 additions and 10 deletions

View File

@@ -162,12 +162,14 @@ impl RequestBuilder {
/// ```
pub fn basic_auth<U, P>(&mut self, username: U, password: Option<P>) -> &mut RequestBuilder
where
U: Into<String>,
P: Into<String>,
U: fmt::Display,
P: fmt::Display,
{
let username = username.into();
let password = password.map(|p| p.into()).unwrap_or(String::new());
let header_value = format!("basic {}:{}", username, encode(&password));
let auth = match password {
Some(password) => format!("{}:{}", username, password),
None => format!("{}:", username)
};
let header_value = format!("basic {}", encode(&auth));
self.header(::header::AUTHORIZATION, HeaderValue::from_str(header_value.as_str()).expect(""))
}