Skip to content

Commit

Permalink
[BACKPORT] #4099: Fix of the websocket request builder (#4113)
Browse files Browse the repository at this point in the history
Signed-off-by: Stukalov-A-M <[email protected]>
  • Loading branch information
Stukalov-A-M authored Dec 6, 2023
1 parent d7c7bfe commit 35fc900
Showing 1 changed file with 12 additions and 16 deletions.
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

0 comments on commit 35fc900

Please sign in to comment.