11use axum:: {
22 extract:: {
3- Path , State ,
3+ Request , State ,
44 ws:: { WebSocket , WebSocketUpgrade } ,
55 } ,
66 http:: StatusCode ,
@@ -15,13 +15,28 @@ use crate::{db, proxy::WsProxy};
1515/// WebSocket proxy handler
1616pub async fn websocket_handler (
1717 State ( pool) : State < Arc < SqlitePool > > ,
18- Path ( session_id) : Path < String > ,
1918 ws : WebSocketUpgrade ,
19+ req : Request ,
2020) -> Result < Response , StatusCode > {
21- info ! (
22- "Received WebSocket connection request: session_id={}" ,
23- session_id
24- ) ;
21+ // Extract full path from request
22+ let full_path = req. uri ( ) . path ( ) . to_string ( ) ;
23+
24+ info ! ( "Received WebSocket connection request: path={}" , full_path) ;
25+
26+ // Extract session_id from path (format: /ws/{session_id} or /{session_id})
27+ let session_id = full_path
28+ . trim_start_matches ( '/' )
29+ . split ( '/' )
30+ . nth ( 1 ) // Get the second segment (after 'ws')
31+ . unwrap_or ( "" )
32+ . to_string ( ) ;
33+
34+ if session_id. is_empty ( ) {
35+ warn ! ( "Invalid WebSocket path: {}" , full_path) ;
36+ return Err ( StatusCode :: BAD_REQUEST ) ;
37+ }
38+
39+ info ! ( "Extracted session_id: {}" , session_id) ;
2540
2641 // 1. Query database to get session information
2742 let session = match db:: get_session ( & pool, & session_id) . await {
@@ -41,11 +56,11 @@ pub async fn websocket_handler(
4156 return Err ( StatusCode :: SERVICE_UNAVAILABLE ) ;
4257 }
4358
44- // 3. Convert downstream URL to WebSocket format and append session_id to path
59+ // 3. Convert downstream URL to WebSocket format and append full path
4560 let downstream_ws_url = format ! (
46- "{}/ {}" ,
61+ "{}{}" ,
4762 convert_to_ws_url( & session. downstream_server_url) . trim_end_matches( '/' ) ,
48- session_id
63+ full_path
4964 ) ;
5065 info ! ( "Downstream WebSocket URL: {}" , downstream_ws_url) ;
5166
0 commit comments