Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ws client): use query part of URL. #429

Merged
merged 3 commits into from
Aug 5, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion ws-client/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ impl<'a> WsTransportClientBuilder<'a> {
}
};

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);
if let Some(origin) = self.origin_header.as_ref() {
Expand Down Expand Up @@ -294,9 +295,14 @@ 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 = url.path().to_owned();
if let Some(query) = url.query() {
path.push('?');
path.push_str(query);
}
Copy link
Collaborator

@jsdw jsdw Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks sane to me (without a wider context of how the path String that now contains the query part is used elsewhere)! I wonder whether it should be renamed path_and_query for clarity? Is there anywhere that will be surprised to now be getting a query string in here?

I was wondering whether percent-encoded URLs would end up being double percent-encoded when returned (because Url::query percent-encodes) but I did this:

use url::Url;

fn main() {
    let foo = Url::parse("ws://localhost/foo?bar=hello%20world").unwrap();
    println!("{:?}", foo.query());
}

in the rust playground and the output is "bar=hello%20world" as I'd hope for :)

Copy link
Member Author

@niklasad1 niklasad1 Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same as soketto calls http resource but I like path_and_query better even if the query might be empty :P

Copy link
Member Author

@niklasad1 niklasad1 Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further path_and_query is passed directly to soketto::Client::new(...) called resource there, it's used here

// 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 })
}
}

Expand Down Expand Up @@ -348,4 +354,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");
}
}