Skip to content

Commit 81f136f

Browse files
committed
Reformat ring.middleware.cookies
1 parent da8ade3 commit 81f136f

File tree

1 file changed

+33
-55
lines changed

1 file changed

+33
-55
lines changed

ring-core/src/ring/middleware/cookies.clj

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,37 @@
88
[clojure.string :as str]
99
[ring.util.parsing :refer [re-token]]))
1010

11-
(def ^{:private true, :doc "RFC6265 cookie-octet"}
12-
re-cookie-octet
11+
;; RFC6265 regular expressions
12+
(def ^:private re-cookie-octet
1313
#"[!#$%&'()*+\-./0-9:<=>?@A-Z\[\]\^_`a-z\{\|\}~]")
1414

15-
(def ^{:private true, :doc "RFC6265 cookie-value"}
16-
re-cookie-value
15+
(def ^:private re-cookie-value
1716
(re-pattern (str "\"" re-cookie-octet "*\"|" re-cookie-octet "*")))
1817

19-
(def ^{:private true, :doc "RFC6265 set-cookie-string"}
20-
re-cookie
18+
(def ^:private re-cookie
2119
(re-pattern (str "\\s*(" re-token ")=(" re-cookie-value ")\\s*[;,]?")))
2220

23-
(def ^{:private true
24-
:doc "Attributes defined by RFC6265 that apply to the Set-Cookie header."}
25-
set-cookie-attrs
21+
(def ^:private set-cookie-attrs
2622
{:domain "Domain", :max-age "Max-Age", :path "Path"
2723
:secure "Secure", :expires "Expires", :http-only "HttpOnly"
2824
:same-site "SameSite"})
2925

30-
(def ^{:private true
31-
:doc "Values defined by RFC6265 that apply to the SameSite cookie attribute header."}
32-
same-site-values
33-
{:strict "Strict"
34-
:lax "Lax"
35-
:none "None"})
26+
(def ^:private same-site-values
27+
{:strict "Strict", :lax "Lax", :none "None"})
3628

37-
(defn- parse-cookie-header
38-
"Turn a HTTP Cookie header into a list of name/value pairs."
39-
[header]
29+
(defn- parse-cookie-header [header]
4030
(for [[_ name value] (re-seq re-cookie header)]
4131
[name value]))
4232

43-
(defn- strip-quotes
44-
"Strip quotes from a cookie value."
45-
[value]
33+
(defn- strip-quotes [value]
4634
(str/replace value #"^\"|\"$" ""))
4735

4836
(defn- decode-values [cookies decoder]
4937
(for [[name value] cookies]
50-
(if-let [value (decoder (strip-quotes value))]
38+
(when-let [value (decoder (strip-quotes value))]
5139
[name {:value value}])))
5240

53-
(defn- parse-cookies
54-
"Parse the cookies from a request map."
55-
[request encoder]
41+
(defn- parse-cookies [request encoder]
5642
(if-let [cookie (get-in request [:headers "cookie"])]
5743
(->> cookie
5844
parse-cookie-header
@@ -61,9 +47,7 @@
6147
(into {}))
6248
{}))
6349

64-
(defn- write-value
65-
"Write the main cookie value."
66-
[key value encoder]
50+
(defn- write-value [key value encoder]
6751
(encoder {key value}))
6852

6953
(defprotocol CookieInterval
@@ -72,7 +56,7 @@
7256
(defprotocol CookieDateTime
7357
(rfc822-format [this]))
7458

75-
(defn- ^Class class-by-name [s]
59+
(defn- class-by-name ^Class [s]
7660
(try (Class/forName s)
7761
(catch ClassNotFoundException _)))
7862

@@ -81,11 +65,12 @@
8165
CookieDateTime
8266
{:rfc822-format
8367
(eval
84-
'(let [fmtr (.. (org.joda.time.format.DateTimeFormat/forPattern "EEE, dd MMM yyyy HH:mm:ss Z")
85-
(withZone org.joda.time.DateTimeZone/UTC)
86-
(withLocale java.util.Locale/US))]
87-
(fn [interval]
88-
(.print fmtr ^org.joda.time.DateTime interval))))}))
68+
'(let [fmtr (.. (org.joda.time.format.DateTimeFormat/forPattern
69+
"EEE, dd MMM yyyy HH:mm:ss Z")
70+
(withZone org.joda.time.DateTimeZone/UTC)
71+
(withLocale java.util.Locale/US))]
72+
(fn [interval]
73+
(.print fmtr ^org.joda.time.DateTime interval))))}))
8974

9075
(when-let [interval (class-by-name "org.joda.time.Interval")]
9176
(extend interval
@@ -98,17 +83,16 @@
9883
(->seconds [this]
9984
(.get this ChronoUnit/SECONDS)))
10085

101-
(let [java-rfc822-formatter (.. (DateTimeFormatter/ofPattern "EEE, dd MMM yyyy HH:mm:ss Z")
102-
(withZone (ZoneId/of "UTC"))
103-
(withLocale Locale/US))]
86+
(let [java-rfc822-formatter
87+
(.. (DateTimeFormatter/ofPattern "EEE, dd MMM yyyy HH:mm:ss Z")
88+
(withZone (ZoneId/of "UTC"))
89+
(withLocale Locale/US))]
10490
(extend-protocol CookieDateTime
10591
ZonedDateTime
10692
(rfc822-format [this]
10793
(.format java-rfc822-formatter this))))
10894

109-
(defn- valid-attr?
110-
"Is the attribute valid?"
111-
[[key value]]
95+
(defn- valid-attr? [[key value]]
11296
(and (contains? set-cookie-attrs key)
11397
(not (.contains (str value) ";"))
11498
(case key
@@ -117,32 +101,26 @@
117101
:same-site (contains? same-site-values value)
118102
true)))
119103

120-
(defn- write-attr-map
121-
"Write a map of cookie attributes to a string."
122-
[attrs]
104+
(defn- write-attr-map [attrs]
123105
{:pre [(every? valid-attr? attrs)]}
124106
(for [[key value] attrs]
125-
(let [attr-name (name (set-cookie-attrs key))]
107+
(let [attr (name (set-cookie-attrs key))]
126108
(cond
127-
(satisfies? CookieInterval value) (str ";" attr-name "=" (->seconds value))
128-
(satisfies? CookieDateTime value) (str ";" attr-name "=" (rfc822-format value))
129-
(true? value) (str ";" attr-name)
109+
(satisfies? CookieInterval value) (str ";" attr "=" (->seconds value))
110+
(satisfies? CookieDateTime value) (str ";" attr "=" (rfc822-format value))
111+
(true? value) (str ";" attr)
130112
(false? value) ""
131-
(= :same-site key) (str ";" attr-name "=" (same-site-values value))
132-
:else (str ";" attr-name "=" value)))))
113+
(= :same-site key) (str ";" attr "=" (same-site-values value))
114+
:else (str ";" attr "=" value)))))
133115

134-
(defn- write-cookies
135-
"Turn a map of cookies into a seq of strings for a Set-Cookie header."
136-
[cookies encoder]
116+
(defn- write-cookies [cookies encoder]
137117
(for [[key value] cookies]
138118
(if (map? value)
139119
(apply str (write-value key (:value value) encoder)
140120
(write-attr-map (dissoc value :value)))
141121
(write-value key value encoder))))
142122

143-
(defn- set-cookies
144-
"Add a Set-Cookie header to a response if there is a :cookies key."
145-
[response encoder]
123+
(defn- set-cookies [response encoder]
146124
(if-let [cookies (:cookies response)]
147125
(update-in response
148126
[:headers "Set-Cookie"]

0 commit comments

Comments
 (0)