Skip to content

Commit

Permalink
fix(ws client): use query part of URL. (#429)
Browse files Browse the repository at this point in the history
* fix(ws client): use query part of URL.

Fixes #428, we didn't take that query part of the URL into account and it wasn't sent in GET request.

* add log when connecting to a target

* fix(grumbles): path -> path_and_query
  • Loading branch information
niklasad1 committed Sep 17, 2021
1 parent c850b39 commit 2276dce
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions ws-client/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,12 @@ impl<'a> WsTransportClientBuilder<'a> {
}
};

let mut client =
WsRawClient::new(BufReader::new(BufWriter::new(tcp_stream)), &self.target.host_header, &self.target.path);
log::debug!("Connecting to target: {:?}", self.target);
let mut client = WsRawClient::new(
BufReader::new(BufWriter::new(tcp_stream)),
&self.target.host_header,
&self.target.path_and_query,
);
if let Some(origin) = self.origin_header.as_ref() {
client.set_origin(origin);
}
Expand Down Expand Up @@ -276,8 +280,8 @@ pub struct Target {
host_header: String,
/// WebSocket stream mode, see [`Mode`] for further documentation.
mode: Mode,
/// The HTTP host resource path.
path: String,
/// The path and query parts from an URL.
path_and_query: String,
}

impl Target {
Expand All @@ -294,21 +298,26 @@ impl Target {
url.host_str().map(ToOwned::to_owned).ok_or_else(|| WsHandshakeError::Url("No host in URL".into()))?;
let port = url.port_or_known_default().ok_or_else(|| WsHandshakeError::Url("No port number in URL".into()))?;
let host_header = format!("{}:{}", host, port);
let mut path_and_query = url.path().to_owned();
if let Some(query) = url.query() {
path_and_query.push('?');
path_and_query.push_str(query);
}
// NOTE: `Url::socket_addrs` is using the default port if it's missing (ws:// - 80, wss:// - 443)
let sockaddrs = url.socket_addrs(|| None).map_err(WsHandshakeError::ResolutionFailed)?;
Ok(Self { sockaddrs, host, host_header, mode, path: url.path().to_owned() })
Ok(Self { sockaddrs, host, host_header, mode, path_and_query })
}
}

#[cfg(test)]
mod tests {
use super::{Mode, Target, WsHandshakeError};

fn assert_ws_target(target: Target, host: &str, host_header: &str, mode: Mode, path: &str) {
fn assert_ws_target(target: Target, host: &str, host_header: &str, mode: Mode, path_and_query: &str) {
assert_eq!(&target.host, host);
assert_eq!(&target.host_header, host_header);
assert_eq!(target.mode, mode);
assert_eq!(&target.path, path);
assert_eq!(&target.path_and_query, path_and_query);
}

#[test]
Expand Down Expand Up @@ -348,4 +357,16 @@ mod tests {
let target = Target::parse("wss://127.0.0.1/my-special-path").unwrap();
assert_ws_target(target, "127.0.0.1", "127.0.0.1:443", Mode::Tls, "/my-special-path");
}

#[test]
fn url_with_query_works() {
let target = Target::parse("wss://127.0.0.1/my?name1=value1&name2=value2").unwrap();
assert_ws_target(target, "127.0.0.1", "127.0.0.1:443", Mode::Tls, "/my?name1=value1&name2=value2");
}

#[test]
fn url_with_fragment_is_ignored() {
let target = Target::parse("wss://127.0.0.1/my.htm#ignore").unwrap();
assert_ws_target(target, "127.0.0.1", "127.0.0.1:443", Mode::Tls, "/my.htm");
}
}

0 comments on commit 2276dce

Please sign in to comment.