Skip to content

Commit 76ad767

Browse files
committed
Fix erroneous HeaderMap type hints
Turns out that there are in fact two distinct types of that name which are used to abstract over the different classes representing header maps in Netty's H1 and H2 implementations. Introduce a protocol instead which is implemented by both wrapper types for accessing raw header values.
1 parent 031f4a4 commit 76ad767

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

src/aleph/http.clj

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[aleph.flow :as flow]
55
[aleph.http.client :as client]
66
[aleph.http.client-middleware :as middleware]
7+
[aleph.http.common :as common]
78
[aleph.http.file :as file]
89
[aleph.http.server :as server]
910
[aleph.http.websocket.client :as ws.client]
@@ -15,8 +16,6 @@
1516
[manifold.deferred :as d]
1617
[manifold.executor :as executor])
1718
(:import
18-
(aleph.http.core
19-
HeaderMap)
2019
(aleph.utils
2120
ConnectionTimeoutException
2221
PoolTimeoutException
@@ -502,12 +501,8 @@
502501
(defn get-all
503502
"Given a header map from an HTTP request or response, returns a collection of
504503
values associated with the key, rather than a comma-delimited string."
505-
[^HeaderMap header-m ^String k]
506-
(let [raw-headers (.headers header-m)]
507-
(condp instance? raw-headers
508-
HttpHeaders (.getAll ^HttpHeaders raw-headers k)
509-
Headers (.getAll ^Headers raw-headers k)
510-
(throw (IllegalArgumentException. (str "Unknown headers type: " (class raw-headers)))))))
504+
[header-m ^String k]
505+
(common/get-header-values header-m k))
511506

512507
(defn wrap-ring-async-handler
513508
"Converts given asynchronous Ring handler to Aleph-compliant handler.

src/aleph/http/client_middleware.clj

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"This middleware is adapted from clj-http, whose license is amenable to this sort of
33
copy/pastery"
44
(:require
5+
[aleph.http.common :as common]
56
;; leave this dependency to make sure that HeaderMap is already compiled
67
[aleph.http.core :as http]
78
[aleph.http.schema :as schema]
@@ -743,18 +744,12 @@
743744
(parse-cookie cookie-spec)
744745
(netty-cookie->cookie))))
745746

746-
;; we might want to use here http/get-all helper,
747-
;; but it would result in circular dependencies
748747
(defn ^:no-doc extract-cookies-from-response-headers
749748
([headers]
750749
(extract-cookies-from-response-headers default-cookie-spec headers))
751750
([cookie-spec ^HeaderMap header-m]
752-
(let [raw-headers (.headers header-m)]
753-
(->> (condp instance? raw-headers
754-
HttpHeaders (.getAll ^HttpHeaders raw-headers set-cookie-header-name)
755-
Headers (.getAll ^Headers raw-headers set-cookie-header-name)
756-
(throw (IllegalArgumentException. (str "Unknown headers type: " (class raw-headers)))))
757-
(map (partial decode-set-cookie-header cookie-spec))))))
751+
(->> (common/get-header-values header-m set-cookie-header-name)
752+
(map (partial decode-set-cookie-header cookie-spec)))))
758753

759754
(defn ^:no-doc handle-cookies [{:keys [cookie-store cookie-spec]
760755
:or {cookie-spec default-cookie-spec}}

src/aleph/http/common.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
(def aleph-server-header "Aleph value for the Server header" (AsciiString. "Aleph/0.7.0-alpha1"))
3131

32+
(defprotocol HeaderMap
33+
(get-header-values [m ^String k]))
34+
3235
(defn coerce-element
3336
"Turns an object into something writable to a Netty channel.
3437

src/aleph/http/core.clj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
s')))
104104

105105
;; About 10x faster than using Clojure maps, and handles more keys
106+
;; Note that there's a copy of this in aleph.http.http2 because unfortunately, Netty uses a different class
107+
;; for representing H2 headers (See https://github.com/netty/netty/issues/8528)
106108
(def-map-type HeaderMap
107109
[^HttpHeaders headers
108110
added
@@ -145,7 +147,10 @@
145147
default-value
146148
(if (== 1 (.size vs))
147149
(.toString (.get vs 0))
148-
(str/join "," vs))))))))
150+
(str/join "," vs)))))))
151+
common/HeaderMap
152+
(get-header-values [_ k]
153+
(.getAll headers ^String k)))
149154

150155
(defn headers->map
151156
"Returns a map of Ring headers from a Netty HttpHeaders object."

src/aleph/http/http2.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,10 @@
423423
default-value
424424
(if (== 1 (.size vs))
425425
(.toString (.get vs 0))
426-
(str/join "," vs))))))))
426+
(str/join "," vs)))))))
427+
common/HeaderMap
428+
(get-header-values [_ k]
429+
(.getAll headers ^String k)))
427430

428431
(defn headers->map
429432
"Returns a map of Ring headers from a Netty Headers object.

0 commit comments

Comments
 (0)