summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Hintz <t@thintz.com>2014-10-08 17:17:45 -0700
committerThomas Hintz <t@thintz.com>2014-10-08 17:17:45 -0700
commitfb9d35db77d2b732b1be412899723fc4e0190063 (patch)
treee33050957c76b8f68a30681b19c448d1cac089d2
parenta6570f265941eac32d2765f99e02482bb33d4b1f (diff)
downloadwebsockets-fb9d35db77d2b732b1be412899723fc4e0190063.tar.gz
Cleanup.
-rw-r--r--websockets.scm62
1 files changed, 16 insertions, 46 deletions
diff --git a/websockets.scm b/websockets.scm
index 1927a0c..655fa48 100644
--- a/websockets.scm
+++ b/websockets.scm
@@ -336,7 +336,7 @@
frame-fin frame-optype))
(else
(signal (make-websocket-exception
- (make-property-condition 'unhandled-opcode
+ (make-property-condition 'unhandled-optype
'optype frame-optype)))))))))))
(define (valid-utf8-2? s)
@@ -501,6 +501,7 @@ static const uint8_t utf8d[] = {
(process-fragments fragments optype)))))
; TODO does #!optional and #!key work together?
+; TODO document websocket state close states
(define (close-websocket #!optional (ws (current-websocket))
#!key (close-reason 'normal) (data (make-u8vector 0)))
(define invalid-close-reason #f)
@@ -632,6 +633,15 @@ static const uint8_t utf8d[] = {
ws))
(define (with-websocket proc #!optional (concurrent #f))
+ (define (handle-error close-reason exn)
+ (set-websocket-state! (current-websocket) 'closing)
+ (close-websocket (current-websocket) close-reason: close-reason)
+ (unless (port-closed? (request-port (current-request)))
+ (close-input-port (request-port (current-request))))
+ (unless (port-closed? (response-port (current-response)))
+ (close-output-port (response-port (current-response))))
+ (when (propagate-common-errors)
+ (signal exn)))
(parameterize
((current-websocket (websocket-accept concurrent)))
(condition-case
@@ -639,51 +649,11 @@ static const uint8_t utf8d[] = {
(close-websocket)
(close-input-port (request-port (current-request)))
(close-output-port (response-port (current-response))))
- (exn (websocket protocol-error)
- (set-websocket-state! (current-websocket) 'closing)
- (close-websocket (current-websocket) close-reason: 'protocol-error)
- (unless (port-closed? (request-port (current-request)))
- (close-input-port (request-port (current-request))))
- (unless (port-closed? (response-port (current-response)))
- (close-output-port (response-port (current-response))))
- (when (propagate-common-errors)
- (signal exn)))
- (exn (websocket invalid-data)
- (set-websocket-state! (current-websocket) 'closing)
- (close-websocket (current-websocket) close-reason: 'invalid-data)
- (unless (port-closed? (request-port (current-request)))
- (close-input-port (request-port (current-request))))
- (unless (port-closed? (response-port (current-response)))
- (close-output-port (response-port (current-response))))
- (when (propagate-common-errors)
- (signal exn)))
- (exn (websocket connection-timeout)
- (set-websocket-state! (current-websocket) 'closing)
- (close-websocket (current-websocket) close-reason: 'going-away)
- (unless (port-closed? (request-port (current-request)))
- (close-input-port (request-port (current-request))))
- (unless (port-closed? (response-port (current-response)))
- (close-output-port (response-port (current-response))))
- (when (propagate-common-errors)
- (signal exn)))
- (exn (websocket message-too-large)
- (set-websocket-state! (current-websocket) 'closing)
- (close-websocket (current-websocket) close-reason: 'message-too-large)
- (unless (port-closed? (request-port (current-request)))
- (close-input-port (request-port (current-request))))
- (unless (port-closed? (response-port (current-response)))
- (close-output-port (response-port (current-response))))
- (when (propagate-common-errors)
- (signal exn)))
- (exn ()
- (close-websocket (current-websocket) close-reason: 1011)
- (unless (port-closed? (request-port (current-request)))
- (close-input-port (request-port (current-request))))
- (unless (port-closed? (response-port (current-response)))
- (close-output-port (response-port (current-response))))
- (abort exn)
- ;(signal (make-websocket-exception (make-property-condition 'unexpected-error)))
- ))))
+ (exn (websocket protocol-error) (handle-error 'protocol-error exn))
+ (exn (websocket invalid-data) (handle-error 'invalid-data exn))
+ (exn (websocket connection-timeout) (handle-error 'going-away exn))
+ (exn (websocket message-too-large) (handle-error 'message-too-large exn))
+ (exn () (handle-error 'unexpected-error exn)))))
(define (with-concurrent-websocket proc)
(let ((parent-thread (current-thread)))