Skip to content

Commit 936561d

Browse files
authored
Merge pull request #420 from shilder/wshandshake-exceptions
Handle WS handshake exceptions
2 parents 8bdbc64 + a98149d commit 936561d

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/aleph/http/client.clj

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@
601601
:channel-inactive
602602
([_ ctx]
603603
(when (realized? d)
604-
(s/close! @d))
604+
;; close only on success
605+
(d/chain' d s/close!))
605606
(.fireChannelInactive ctx))
606607

607608
:channel-active
@@ -618,26 +619,32 @@
618619
(cond
619620

620621
(not (.isHandshakeComplete handshaker))
621-
(d/chain'
622-
(netty/wrap-future (.processHandshake handshaker ch msg))
623-
(fn [_]
624-
(let [out (netty/sink ch false
625-
(fn [c]
626-
(if (instance? CharSequence c)
627-
(TextWebSocketFrame. (bs/to-string c))
628-
(BinaryWebSocketFrame. (netty/to-byte-buf ctx c))))
629-
(fn [] @desc))]
630-
631-
(d/success! d
632-
(doto
633-
(s/splice out @in)
634-
(reset-meta! {:aleph/channel ch})))
635-
636-
(s/on-drained @in
637-
#(when (.isOpen ch)
638-
(d/chain'
639-
(netty/wrap-future (.close handshaker ch (CloseWebSocketFrame.)))
640-
(fn [_] (netty/close ctx))))))))
622+
(-> (netty/wrap-future (.processHandshake handshaker ch msg))
623+
(d/chain'
624+
(fn [_]
625+
(let [out (netty/sink ch false
626+
(fn [c]
627+
(if (instance? CharSequence c)
628+
(TextWebSocketFrame. (bs/to-string c))
629+
(BinaryWebSocketFrame. (netty/to-byte-buf ctx c))))
630+
(fn [] @desc))]
631+
632+
(d/success! d
633+
(doto
634+
(s/splice out @in)
635+
(reset-meta! {:aleph/channel ch})))
636+
637+
(s/on-drained @in
638+
#(when (.isOpen ch)
639+
(d/chain'
640+
(netty/wrap-future (.close handshaker ch (CloseWebSocketFrame.)))
641+
(fn [_] (netty/close ctx))))))))
642+
(d/catch'
643+
(fn [ex]
644+
;; handle handshake exception
645+
(d/error! d ex)
646+
(s/close! @in)
647+
(netty/close ctx))))
641648

642649
(instance? FullHttpResponse msg)
643650
(let [rsp ^FullHttpResponse msg]

0 commit comments

Comments
 (0)