Skip to content

Commit 1d60c12

Browse files
committed
chore: bump tokio-tungstenite to 0.28
Signed-off-by: Xin Liu <[email protected]>
1 parent 3060e10 commit 1d60c12

File tree

5 files changed

+56
-51
lines changed

5 files changed

+56
-51
lines changed

Cargo.lock

Lines changed: 20 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ thiserror = "1.0"
2727
tokio = { version = "1", features = ["full"] }
2828

2929
# WebSocket
30-
tokio-tungstenite = "0.24"
30+
tokio-tungstenite = { version = "0.28", features = ["rustls-tls-webpki-roots"] }
3131

3232
# Utilities
3333
tower = "0.5"

src/handlers/http.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::{
22
body::Bytes,
3-
extract::{Path, State},
3+
extract::{Path, RawQuery, State},
44
http::{Method, StatusCode},
55
response::{IntoResponse, Response},
66
};
@@ -14,6 +14,7 @@ use crate::{db, proxy::HttpProxy};
1414
pub async fn http_proxy_handler(
1515
State(state): State<Arc<AppState>>,
1616
Path((session_id, path)): Path<(String, String)>,
17+
RawQuery(query): RawQuery,
1718
method: Method,
1819
headers: axum::http::HeaderMap,
1920
body: Bytes,
@@ -36,7 +37,7 @@ pub async fn http_proxy_handler(
3637
return Err(StatusCode::SERVICE_UNAVAILABLE);
3738
}
3839

39-
// 3. Construct full path
40+
// 3. Construct full path with query string
4041
let full_path = if path.is_empty() {
4142
"/".to_string()
4243
} else if path.starts_with('/') {
@@ -45,12 +46,19 @@ pub async fn http_proxy_handler(
4546
format!("/{}", path)
4647
};
4748

49+
// Append query string if present
50+
let full_path_with_query = if let Some(q) = query {
51+
format!("{}?{}", full_path, q)
52+
} else {
53+
full_path
54+
};
55+
4856
// 4. Forward request
4957
match state
5058
.http_proxy
5159
.forward_request(
5260
&session.downstream_server_url,
53-
&full_path,
61+
&full_path_with_query,
5462
method,
5563
headers,
5664
body,

src/handlers/websocket.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ fn convert_to_ws_url(http_url: &str) -> String {
6666
http_url.replace("https://", "wss://")
6767
} else if http_url.starts_with("http://") {
6868
http_url.replace("http://", "ws://")
69+
} else if http_url.starts_with("wss://") || http_url.starts_with("ws://") {
70+
// Already a WebSocket URL, return as-is
71+
http_url.to_string()
6972
} else {
7073
// If no protocol prefix, default to ws://
7174
format!("ws://{}", http_url)
@@ -87,5 +90,15 @@ mod tests {
8790
"wss://example.com"
8891
);
8992
assert_eq!(convert_to_ws_url("localhost:8080"), "ws://localhost:8080");
93+
94+
// Test WebSocket URLs are returned as-is
95+
assert_eq!(
96+
convert_to_ws_url("ws://localhost:8080"),
97+
"ws://localhost:8080"
98+
);
99+
assert_eq!(
100+
convert_to_ws_url("wss://echo.websocket.org"),
101+
"wss://echo.websocket.org"
102+
);
90103
}
91104
}

src/proxy/ws_proxy.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl WsProxy {
3838
buffer.len()
3939
);
4040
if let Err(e) = downstream_write
41-
.send(TungsteniteMessage::Text(buffer.to_string()))
41+
.send(TungsteniteMessage::Text(buffer.to_string().into()))
4242
.await
4343
{
4444
error!("Failed to forward text message to downstream: {}", e);
@@ -51,7 +51,7 @@ impl WsProxy {
5151
data.len()
5252
);
5353
if let Err(e) = downstream_write
54-
.send(TungsteniteMessage::Binary(data.into()))
54+
.send(TungsteniteMessage::Binary(data))
5555
.await
5656
{
5757
error!("Failed to forward binary message to downstream: {}", e);
@@ -60,19 +60,15 @@ impl WsProxy {
6060
}
6161
Ok(Message::Ping(data)) => {
6262
info!("Client -> Downstream: Ping");
63-
if let Err(e) = downstream_write
64-
.send(TungsteniteMessage::Ping(data.into()))
65-
.await
63+
if let Err(e) = downstream_write.send(TungsteniteMessage::Ping(data)).await
6664
{
6765
error!("Failed to forward Ping to downstream: {}", e);
6866
break;
6967
}
7068
}
7169
Ok(Message::Pong(data)) => {
7270
info!("Client -> Downstream: Pong");
73-
if let Err(e) = downstream_write
74-
.send(TungsteniteMessage::Pong(data.into()))
75-
.await
71+
if let Err(e) = downstream_write.send(TungsteniteMessage::Pong(data)).await
7672
{
7773
error!("Failed to forward Pong to downstream: {}", e);
7874
break;
@@ -97,7 +93,10 @@ impl WsProxy {
9793
match msg {
9894
Ok(TungsteniteMessage::Text(text)) => {
9995
info!("Downstream -> Client: Text message ({} bytes)", text.len());
100-
if let Err(e) = client_write.send(Message::Text(text.into())).await {
96+
if let Err(e) = client_write
97+
.send(Message::Text(text.to_string().into()))
98+
.await
99+
{
101100
error!("Failed to forward text message to client: {}", e);
102101
break;
103102
}
@@ -107,21 +106,21 @@ impl WsProxy {
107106
"Downstream -> Client: Binary message ({} bytes)",
108107
data.len()
109108
);
110-
if let Err(e) = client_write.send(Message::Binary(data.into())).await {
109+
if let Err(e) = client_write.send(Message::Binary(data)).await {
111110
error!("Failed to forward binary message to client: {}", e);
112111
break;
113112
}
114113
}
115114
Ok(TungsteniteMessage::Ping(data)) => {
116115
info!("Downstream -> Client: Ping");
117-
if let Err(e) = client_write.send(Message::Ping(data.into())).await {
116+
if let Err(e) = client_write.send(Message::Ping(data)).await {
118117
error!("Failed to forward Ping to client: {}", e);
119118
break;
120119
}
121120
}
122121
Ok(TungsteniteMessage::Pong(data)) => {
123122
info!("Downstream -> Client: Pong");
124-
if let Err(e) = client_write.send(Message::Pong(data.into())).await {
123+
if let Err(e) = client_write.send(Message::Pong(data)).await {
125124
error!("Failed to forward Pong to client: {}", e);
126125
break;
127126
}

0 commit comments

Comments
 (0)