Skip to content

Commit

Permalink
[fix] #4099: Fix of the websocket request builder
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 committed Dec 4, 2023
1 parent 0ef18fe commit 10ca268
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions client/src/http_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use eyre::{eyre, Error, Result, WrapErr};
use http::header::HeaderName;
use tokio_tungstenite::tungstenite::{stream::MaybeTlsStream, WebSocket};
pub use tokio_tungstenite::tungstenite::{Error as WebSocketError, Message as WebSocketMessage};
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use url::Url;

use crate::http::{Method, RequestBuilder, Response};
Expand Down Expand Up @@ -117,21 +118,13 @@ 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()?;
if let Some(auth_header) = builder.headers_ref()
.ok_or(eyre!("No headers found"))?.get("Authorization") {
request.headers_mut().insert("Authorization", auth_header.clone());
}
Ok(DefaultWebSocketStreamRequest(request))
}
}

Expand Down

0 comments on commit 10ca268

Please sign in to comment.