From 795dd67dee3dc3f5fa791a20d4b51cc4e811b5d6 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 5 Jun 2024 07:44:52 -0400 Subject: Remove timeout handling that is incompatible with suspendable ports. Timeouts should be handled by a concurrency layer such as fibers. --- web/socket/client.scm | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'web/socket/client.scm') diff --git a/web/socket/client.scm b/web/socket/client.scm index 966e3f7..d3a9cb8 100644 --- a/web/socket/client.scm +++ b/web/socket/client.scm @@ -183,15 +183,16 @@ true, verify HTTPS server certificates." ws) (error "not a websocket uri" uri)))) -(define* (close-websocket ws #:key (timeout 1)) - "Close the WebSocket connection for the client WS, waiting up to -TIMEOUT seconds (default 1 second) for a graceful shutdown." +(define (close-websocket ws) + "Close the WebSocket connection for the client WS." (let ((socket (websocket-socket ws))) (set-websocket-state! ws 'closing) (write-frame (make-close-frame (make-bytevector 0)) socket) - ;; Per section 5.5.1 , wait for the server to close the connection - ;; for a reasonable amount of time. - (read-close-frame socket #:timeout timeout) + ;; Per section 5.5.1, wait for the server to close the connection. + (let loop () + (match (read-frame socket) + ((or #f (? close-frame?)) (values)) + (_ (loop)))) (close-port socket) (close-port (websocket-entropy-port ws)) (set-websocket-state! ws 'closed) @@ -209,20 +210,29 @@ connected to." (write-frame (make-text-frame data (generate-masking-key ws)) (websocket-socket ws))) -(define* (websocket-receive ws #:key timeout) +;; TODO: Close frames should be echoed back if the server initiated +;; the disconnect. +;; +;; Per section 5.5.1: +;; +;; If an endpoint receives a Close frame and did not previously send a +;; Close frame, the endpoint MUST send a Close frame in response. +;; (When sending a Close frame in response, the endpoint typically +;; echos the status code it received.) It SHOULD do so as soon as +;; practical. +(define (websocket-receive ws) "Read data from the server that WS is connected to. Returns a string if text data was received, a bytevector if binary data was received, a -pair of (code . reason) if the WebSocket was closed, or #f if TIMEOUT -seconds elapsed without receiving a data frame." +pair of (code . reason) if the WebSocket was closed." (let ((socket (websocket-socket ws))) - ;; For server initiated disconnects and timeouts. + ;; For server initiated disconnects. (define (maybe-close) (when (port-closed? socket) ;; EOF - clean up the websocket. (close-port (websocket-entropy-port ws)) (set-websocket-state! ws 'closed))) - (match (read-data-frame socket #:timeout timeout) - (#f ; timeout + (match (read-data-frame socket) + (#f ; EOF or socket closed. (maybe-close) #f) ((? close-frame? frame) -- cgit v1.2.3