-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.clj
253 lines (217 loc) · 6.61 KB
/
http.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
(ns nsfw.http
"Request / response utilities"
(:use [plumbing.core])
(:require [plumbing.graph :as pg]
[clojure.edn :as edn]
[nsfw.util :as util]
[hiccup.page]
[hiccup.core]
[cognitect.transit :as transit]
[clojure.string :as str]
[bidi.ring :as bidi]
[ring.middleware
file
file-info
session
params
nested-params
multipart-params
keyword-params]
[ring.middleware.session.cookie :only (cookie-store)]
[ring.util.response :only (response content-type)])
(:import [com.fasterxml.jackson.core JsonParseException]))
(defn -graph [gmap strategy]
(let [g (strategy gmap)]
(fn [r]
(:resp (g r)))))
(defn graph [gmap]
(-graph gmap pg/lazy-compile))
(defn graph-eager [gmap]
(-graph gmap pg/eager-compile))
(defn html-resp [body]
{:headers {"content-type" "text/html;charset=utf-8"}
:body body})
(defn edn [body]
{:headers {"content-type" "application/edn;charset=utf-8"}
:body (pr-str body)})
(def header-for {:html {"Content-Type" "text/html; charset=utf-8"}
:js {"Content-Type" "text/javascript; charset=utf-8"}})
(defn headers [& opts]
(apply merge (map header-for opts)))
(def html-header (headers :html))
(defn redirect [loc opts]
(merge
{:headers {"Location" loc}
:status 301}
opts))
(defn decode-body [content-length body]
(when (and content-length
(> content-length 0))
(let [buf (byte-array content-length)]
(.read body buf 0 content-length)
(.close body)
(String. buf))))
(defn response-body
"Turn a HttpInputStream into a string."
[{:keys [content-length body]}]
(if (string? body)
body
(decode-body content-length body)))
(defn decode-edn-body [r]
(-> r response-body edn/read-string))
(defn wrap-edn-request [h]
(fn [r]
(let [content-type-raw (-> r :headers (get "content-type"))]
(if (and content-type-raw
(.contains content-type-raw "application/edn"))
(h (assoc r :edn-body (decode-edn-body r)))
(h r)))))
(defn render-edn [body]
{:headers {"Content-Type" "application/edn; encoding=utf-8"}
:status 200
:body (pr-str body)})
(defn decode-content-type [cts]
(let [parts (str/split cts #";")
media-type (some->
(first parts)
str/trim)]
{:media-type media-type
:params (->> parts
rest
(map str/trim)
(remove empty?)
(map #(str/split % #"="))
(map (fn [[k v]]
[(keyword k) v]))
(into {}))}))
(defn content-type [request]
(when-let [cts (or (get-in request [:headers "Content-Type"])
(get-in request [:headers "content-type"]))]
(decode-content-type cts)))
(defn json-content? [req]
(= "application/json"
(:media-type (content-type req))))
(defn render-json
([opts body]
(merge {:headers {"Content-Type" "application/json;encoding=utf-8"}
:status 200
:body (util/to-json body)}
opts))
([body]
(render-json {} body)))
(defn wrap-decode-json-body [h]
(fn [r]
(if (json-content? r)
(try
(let [json-body (-> r util/response-body util/from-json)]
(h (assoc r :json-body json-body)))
(catch JsonParseException e
(render-json
{:status 400}
{:message "Problems parsing JSON"})))
(h r))))
(defn html-response? [r]
(= "text/html"
(:media-type (content-type r))))
(defn hiccup->html-string [body]
(if-not (vector? body)
body
(if (= :html5 (first body))
(hiccup.page/html5 (rest body))
(hiccup.core/html body))))
(defn wrap-html-response
"Render hiccup vector into an HTML string when the content type is
text/html."
[h]
(fn [req]
(let [resp (h req)]
(if (and (html-response? resp)
(vector? (:body resp)))
(update-in resp [:body] hiccup->html-string)
resp))))
(defn wrap-json-response [h]
(fn [r]
(let [res (h r)]
(-> res
(update-in [:body] util/to-json)
(assoc-in [:headers "Content-Type"]
"application/json;charset=utf-8")))))
(defn wrap-json-request [h]
(fn [r]
(if (= "application/json"
(:media-type (content-type r)))
(h (update-in r [:body] util/from-json))
(h r))))
(defn from-transit [s]
(transit/read
(transit/reader
(if (string? s)
(java.io.ByteArrayInputStream. (.getBytes s "UTF-8"))
s)
:json)))
(defn to-transit [o]
(let [bs (java.io.ByteArrayOutputStream.)]
(transit/write
(transit/writer bs :json)
o)
(.toString bs)))
(defn wrap-transit-response [h]
(fn [r]
(let [res (h r)]
(-> res
(update-in [:body] to-transit)
(assoc-in [:headers "Content-Type"]
"application/transit+json;charset=utf-8")))))
(defn wrap-transit-request [h]
(fn [r]
(if (= "application/transit+json"
(:media-type (content-type r)))
(h (update-in r [:body] from-transit))
(h r))))
(defn routes->bidi [routes]
["" (->> routes
(group-by :path)
(map (fn [[path route-fragments]]
[path (->> route-fragments
(map (fn [{:keys [method handler]}]
[method handler]))
(into {}))]))
(into {}))])
(defn apply-middleware [{:keys [handler middleware] :as route}]
(assoc route
:handler
(reduce
(fn [h mw]
(mw h))
handler
middleware)))
(defn routes->handler [routes]
(->> routes
(map apply-middleware)
routes->bidi
bidi/make-handler))
(defn wrap-404 [h]
(fn [r]
(let [res (h r)]
(if-not (nil? res)
res
{:status 404 :body "not found!!"}))))
(defn wrap-params [h]
(-> h
ring.middleware.keyword-params/wrap-keyword-params
ring.middleware.multipart-params/wrap-multipart-params
ring.middleware.nested-params/wrap-nested-params
ring.middleware.params/wrap-params))
(defn wrap-file [h dir]
(-> h
(ring.middleware.file/wrap-file dir {:allow-symlinks? true})
(ring.middleware.file-info/wrap-file-info)))
(defn wrap-cookie-session [h domain key]
(-> h
(ring.middleware.session/wrap-session
{:store (ring.middleware.session.cookie/cookie-store
{:domain domain
:key key})})))
(defn wrap-context [h ctx]
(fn [r]
(h (assoc r :ctx ctx))))