Skip to content

Commit cf45f50

Browse files
refactor: adjust run_server
1 parent 9904164 commit cf45f50

File tree

1 file changed

+29
-60
lines changed

1 file changed

+29
-60
lines changed

src/websocket/server.mbt

Lines changed: 29 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct ServerConnection {
2222
///|
2323
/// Handle WebSocket handshake on raw TCP connection - internal use
2424
/// This performs the full HTTP upgrade handshake
25-
async fn ServerConnection::handshake(conn : @socket.Tcp) -> ServerConnection? {
25+
async fn ServerConnection::handshake(conn : @socket.Tcp) -> ServerConnection {
2626
// Read HTTP request
2727
let reader = @io.BufferedReader::new(conn)
2828

@@ -94,7 +94,7 @@ async fn ServerConnection::handshake(conn : @socket.Tcp) -> ServerConnection? {
9494
$|\r
9595
$|
9696
conn.write(@encoding/utf8.encode(response))
97-
Some({ conn, closed: None })
97+
{ conn, closed: None }
9898
}
9999

100100
///|
@@ -286,65 +286,34 @@ pub async fn run_server(
286286
addr : @socket.Addr,
287287
_path : String, // Currently unused in this simplified implementation
288288
f : async (ServerConnection, @socket.Addr) -> Unit,
289-
allow_failure? : Bool = true,
289+
allow_failure? : Bool,
290290
max_connections? : Int,
291291
) -> Unit {
292292
let server = @socket.TcpServer::new(addr)
293-
match max_connections {
294-
Some(max_conn) =>
295-
server.run_forever(
296-
async fn(tcp_conn, client_addr) {
297-
// Try to perform WebSocket handshake
298-
try {
299-
let ws_conn = ServerConnection::handshake(tcp_conn)
300-
match ws_conn {
301-
Some(conn) => f(conn, client_addr)
302-
None => tcp_conn.close()
303-
}
304-
} catch {
305-
InvalidHandshake(_) => {
306-
// Send proper HTTP error response before closing
307-
let error_response = "HTTP/1.1 400 Bad Request\r\n" +
308-
"Content-Length: 0\r\n" +
309-
"\r\n"
310-
tcp_conn.write(@encoding/utf8.encode(error_response))
311-
tcp_conn.close()
312-
}
313-
err => {
314-
tcp_conn.close()
315-
raise err
316-
}
317-
}
318-
},
319-
allow_failure~,
320-
max_connections=max_conn,
321-
)
322-
None =>
323-
server.run_forever(
324-
async fn(tcp_conn, client_addr) {
325-
// Try to perform WebSocket handshake
326-
try {
327-
let ws_conn = ServerConnection::handshake(tcp_conn)
328-
match ws_conn {
329-
Some(conn) => f(conn, client_addr)
330-
None => tcp_conn.close()
331-
}
332-
} catch {
333-
InvalidHandshake(_) => {
334-
// Send proper HTTP error response before closing
335-
let error_response = "HTTP/1.1 400 Bad Request\r\n" +
336-
"Content-Length: 0\r\n" +
337-
"\r\n"
338-
tcp_conn.write(@encoding/utf8.encode(error_response))
339-
tcp_conn.close()
340-
}
341-
err => {
342-
tcp_conn.close()
343-
raise err
344-
}
345-
}
346-
},
347-
allow_failure~,
348-
)
349-
}
293+
server.run_forever(
294+
async fn(tcp_conn, client_addr) {
295+
let ws_conn = ServerConnection::handshake(tcp_conn) catch {
296+
// Per spec section 4.2.1 of RFC 6455, send 400 Bad Request on failure
297+
InvalidHandshake(_) as e => {
298+
// Send proper HTTP error response before closing
299+
let error_response = "HTTP/1.1 400 Bad Request\r\n" +
300+
"Content-Length: 0\r\n" +
301+
"\r\n"
302+
tcp_conn.write(@encoding/utf8.encode(error_response))
303+
raise e
304+
}
305+
// Handle other unexpected errors
306+
e => {
307+
let error_response = "HTTP/1.1 500 Internal Server Error\r\n" +
308+
"Content-Length: 0\r\n" +
309+
"\r\n"
310+
tcp_conn.write(@encoding/utf8.encode(error_response))
311+
raise e
312+
}
313+
}
314+
f(ws_conn, client_addr)
315+
},
316+
allow_failure?,
317+
max_connections?,
318+
)
350319
}

0 commit comments

Comments
 (0)