-
Notifications
You must be signed in to change notification settings - Fork 11
/
headers.clj
29 lines (26 loc) · 884 Bytes
/
headers.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(ns net.http.headers
(:require [clojure.string :as str])
(:import io.netty.handler.codec.http.DefaultHttpHeaders
io.netty.handler.codec.http.HttpHeaders))
(defn put!
[^HttpHeaders headers k v]
(.set headers (name k) (str v)))
(defn ^HttpHeaders prepare
([^HttpHeaders headers input]
(when-not (or (empty? input) (map? input))
(throw (IllegalArgumentException.
"HTTP headers should be supplied as a map")))
(let [hmap (reduce-kv #(assoc %1 (keyword %2) %3) {} input)]
(when-not (:connection hmap)
(put! headers :connection "close"))
(doseq [[k v] input]
(put! headers k v))
headers))
([input]
(prepare (DefaultHttpHeaders. true) input)))
(defn as-map
"Get a map out of netty headers."
[^HttpHeaders headers]
(into
{}
(map (fn [[k v]] [(-> k str/lower-case keyword) v])) (.entries headers)))