-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
common.clj
42 lines (37 loc) · 1.27 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
(ns ring.adapter.jetty9.common
(:require [clojure.string :as string])
(:import [javax.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
(when-let [content-type (get headers "Content-Type")]
(.setContentType response content-type)))
(defn get-headers
"Creates a name/value map of all the request headers."
[^HttpServletRequest request]
(reduce
(fn [headers, ^String name]
(assoc headers
(.toLowerCase name Locale/ENGLISH)
(->> (.getHeaders request name)
(enumeration-seq)
(string/join ","))))
{}
(enumeration-seq (.getHeaderNames request))))
(defn lower-case-keys [m]
(->> m
(map #(if (string? (first %))
(update % 0 string/lower-case)
%))
(into {})))
(defn =ignore-case [^String s1 ^String s2]
(.equalsIgnoreCase s1 s2))