-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
common.clj
48 lines (41 loc) · 1.44 KB
/
common.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(ns ring.adapter.jetty9.common
(:require [clojure.string :as string])
(:import [jakarta.servlet.http HttpServletRequest HttpServletResponse]
[java.util Locale]))
(defprotocol RequestMapDecoder
(build-request-map [r]))
(defn set-headers
"Update a HttpServletResponse with a map of headers."
[^HttpServletResponse response, headers]
(doseq [[key val-or-vals] headers]
(if (string? val-or-vals)
(.setHeader response key val-or-vals)
(doseq [val val-or-vals]
(.addHeader response key val))))
; Some headers must be set through specific methods
(some->> (get headers "Content-Type")
(.setContentType response)))
(defn- header-kv*
[^HttpServletRequest req ^String header-name]
[(.toLowerCase header-name Locale/ENGLISH)
(->> (.getHeaders req header-name)
enumeration-seq
(string/join ","))])
(defn get-headers
"Creates a name/value map of all the request headers."
[^HttpServletRequest request]
(->> (.getHeaderNames request)
enumeration-seq
(into {} (map (partial header-kv* request)))))
(defonce noop (constantly nil))
(defn normalize-response
"Normalize response for ring spec"
[response]
(if (string? response)
{:body response}
response))
(defn websocket-upgrade-response?
[{:keys [^long status ws]}]
;; NOTE: we know that when :ws attr is provided in the response, we
;; need to upgrade to websockets protocol.
(and status (== 101 status) ws))