Skip to content

Commit 07d6d16

Browse files
committed
Handle username/password from URL.
This makes ws://john:[email protected]/ to be correctly handled instead of being silently ignored. This was tested locally in the absence of proper unit tests. Rationale: this project is supposedly implementing RFC6455 that states[1]: URI scheme syntax Using the ABNF [RFC5234] syntax and ABNF terminals from the URI specification [RFC3986]: "ws:" "//" authority path-abempty [ "?" query ] And RFC3986 states[2] that 'authority' may include a username and password. [1] https://tools.ietf.org/html/rfc6455#section-11.1.1 [2] https://tools.ietf.org/html/rfc3986#section-3.2.1
1 parent b0a3c5a commit 07d6d16

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)