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] #4099: Fix of the websocket request builder #4114

Merged
Changes from all 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
28 changes: 12 additions & 16 deletions client/src/http_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use attohttpc::{
};
use eyre::{eyre, Error, Result, WrapErr};
use http::header::HeaderName;
use tokio_tungstenite::tungstenite::{stream::MaybeTlsStream, WebSocket};
use tokio_tungstenite::tungstenite::{
client::IntoClientRequest, stream::MaybeTlsStream, WebSocket,
};
pub use tokio_tungstenite::tungstenite::{Error as WebSocketError, Message as WebSocketMessage};
use url::Url;

Expand Down Expand Up @@ -117,21 +119,15 @@ impl DefaultWebSocketRequestBuilder {

/// Consumes itself to build request.
pub fn build(self) -> Result<DefaultWebSocketStreamRequest> {
let mut req = self.0.and_then(|b| b.body(()).map_err(Into::into))?;

let uri = req.uri().to_string();
let headers = req.headers_mut();

headers.insert("Host", uri.parse()?);
headers.insert("Connection", "Upgrade".parse()?);
headers.insert("Upgrade", "websocket".parse()?);
headers.insert("Sec-WebSocket-Version", "13".parse()?);
headers.insert(
"Sec-WebSocket-Key",
tokio_tungstenite::tungstenite::handshake::client::generate_key().parse()?,
);

Ok(DefaultWebSocketStreamRequest(req))
let builder = self.0?;
let mut request = builder
.uri_ref()
.ok_or(eyre!("Missing URI"))?
.into_client_request()?;
for (header, value) in builder.headers_ref().ok_or(eyre!("No headers found"))? {
request.headers_mut().entry(header).or_insert(value.clone());
}
Ok(DefaultWebSocketStreamRequest(request))
}
}

Expand Down
Loading