Skip to content

Commit db355c6

Browse files
authored
Merge pull request #236 from Zopieux/master
Handle username/password from URL
2 parents 6c6c2b2 + 07d6d16 commit db355c6

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/client/builder.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use header::extensions::Extension;
44
use header::{Origin, WebSocketExtensions, WebSocketKey, WebSocketProtocol, WebSocketVersion};
5-
use hyper::header::{Header, HeaderFormat, Headers};
5+
use hyper::header::{Authorization, Basic, Header, HeaderFormat, Headers};
66
use hyper::version::HttpVersion;
77
use std::borrow::Cow;
88
use std::convert::Into;
@@ -817,6 +817,17 @@ impl<'u> ClientBuilder<'u> {
817817
});
818818
}
819819

820+
// handle username/password from URL
821+
if !self.url.username().is_empty() {
822+
self.headers.set(Authorization(Basic {
823+
username: self.url.username().to_owned(),
824+
password: match self.url.password() {
825+
Some(password) => Some(password.to_owned()),
826+
None => None,
827+
},
828+
}));
829+
}
830+
820831
self.headers
821832
.set(Connection(vec![ConnectionOption::ConnectionHeader(
822833
UniCase("Upgrade".to_string()),
@@ -980,4 +991,14 @@ mod tests {
980991
assert!(protos.contains(&"electric".to_string()));
981992
assert!(!protos.contains(&"rust-websocket".to_string()));
982993
}
994+
995+
#[test]
996+
fn build_client_with_username_password() {
997+
use super::*;
998+
let mut builder = ClientBuilder::new("ws://john:[email protected]:8080/hello").unwrap();
999+
let _request = builder.build_request();
1000+
let auth = builder.headers.get::<Authorization<Basic>>().unwrap();
1001+
assert!(auth.username == "john");
1002+
assert_eq!(auth.password, Some("pswd".to_owned()));
1003+
}
9831004
}

0 commit comments

Comments
 (0)