Skip to content

Commit a704e00

Browse files
authored
[Fixes #561] Fix websocket-upgrade-request? for Firefox. (#562)
Firefox sometimes (always?) sends `keep-alive` as well as `Upgrade` in the Connection request header when it tries a WebSocket upgrade request, so we need to split the value to find the `Upgrade`
1 parent e675622 commit a704e00

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/aleph/http/server.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,10 @@
708708
(let [headers (:headers req)
709709
conn (get headers :connection)
710710
upgrade (get headers :upgrade)]
711-
(and (= "upgrade" (when (some? conn) (str/lower-case conn)))
711+
(and (contains? (when (some? conn)
712+
(set (map str/trim
713+
(-> (str/lower-case conn) (str/split #",")))))
714+
"upgrade")
712715
(= "websocket" (when (some? upgrade) (str/lower-case upgrade))))))
713716

714717
(defn initialize-websocket-handler

test/aleph/websocket_test.clj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
[byte-streams :as bs]
1010
[aleph.http :as http]
1111
[aleph.http.core :as http-core]
12+
[aleph.http.server :as http-server]
1213
[clojure.tools.logging :as log]))
1314

1415
(netty/leak-detector-level! :paranoid)
@@ -33,6 +34,18 @@
3334
`(with-server (http/start-server ~handler {:port 8080, :compression? true})
3435
~@body))
3536

37+
(defn connection-handler
38+
([req] (connection-handler {} req))
39+
([options req]
40+
(if (http-server/websocket-upgrade-request? req)
41+
(-> (http/websocket-connection req options)
42+
(d/chain' #(s/connect % %))
43+
(d/catch'
44+
(fn [^Throwable e]
45+
(log/error "upgrade to websocket conn failed"
46+
(.getMessage e))
47+
{}))))))
48+
3649
(defn echo-handler
3750
([req] (echo-handler {} req))
3851
([options req]
@@ -53,6 +66,20 @@
5366
(.getMessage e))
5467
{}))))
5568

69+
(deftest test-connection-header
70+
(with-handler connection-handler
71+
(let [c @(http/websocket-client "ws://localhost:8080")]
72+
(is @(s/put! c "hello"))
73+
(is (= "hello" @(s/try-take! c 5e3)))
74+
(is (= "upgrade" (get-in (s/description c) [:sink :websocket-handshake-headers "connection"]))))
75+
(let [c @(http/websocket-client "ws://localhost:8080" {:headers {:connection "keep-alive, Upgrade"}})]
76+
(is @(s/put! c "hello"))
77+
(is (= "hello" @(s/try-take! c 5e3)))
78+
(is (= "upgrade" (get-in (s/description c) [:sink :websocket-handshake-headers "connection"]))))
79+
(is (= 204 (:status @(http/get "http://localhost:8080"
80+
{:throw-exceptions false})))))
81+
)
82+
5683
(deftest test-echo-handler
5784
(with-handler echo-handler
5885
(let [c @(http/websocket-client "ws://localhost:8080")]

0 commit comments

Comments
 (0)