/
core.clj
474 lines (419 loc) · 17 KB
/
core.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
(ns muuntaja.core
(:require [clojure.set :as set]
[clojure.string :as str]
[muuntaja.parse :as parse]
[muuntaja.util :as util]
[muuntaja.protocols :as protocols]
[muuntaja.records :as records]
[muuntaja.format.json :as json-format]
[muuntaja.format.edn :as edn-format]
[muuntaja.format.transit :as transit-format])
(:import (muuntaja.records FormatAndCharset Adapter)
(java.nio.charset Charset)
(java.io EOFException IOException Writer)))
;;
;; Protocol
;;
(defprotocol Muuntaja
(encoder [this format])
(decoder [this format])
(adapters [this])
(options [this]))
(defprotocol MuuntajaHttp
(request-format [this request])
(response-format [this request])
(negotiate-request-response [this request])
(format-request [this request])
(format-response [this request response])
(negotiate-and-format-request [this request]))
(defn muuntaja? [x]
(satisfies? Muuntaja x))
;;
;; encode & decode
;;
(defn encode
"Encode data into the given format. Returns InputStream or throws."
([m format data]
(if-let [encode (encoder m format)]
(encode data)
(util/throw! m format "invalid encode format")))
([m format data charset]
(if-let [encode (encoder m format)]
(encode data charset)
(util/throw! m format "invalid encode format"))))
(defn decode
"Decode data into the given format. Returns InputStream or throws."
([m format data]
(if-let [decode (decoder m format)]
(decode data)
(util/throw! m format "invalid decode format")))
([m format data charset]
(if-let [decode (decoder m format)]
(decode data charset)
(util/throw! m format "invalid decode format"))))
;;
;; Documentation
;;
(defn decodes
"Set of formats that the Muuntaja instance can decode"
[m] (->> m (adapters) (filter #(-> % second :decode)) (map first) (set)))
(defn encodes
"Set of formats that the Muuntaja instance can encode"
[m] (->> m (adapters) (filter #(-> % second :encode)) (map first) (set)))
(defn matchers
"Map of format->regexp that the Muuntaja instance knows"
[m] (->> m (options) :formats (keep (fn [[n {:keys [matches]}]] (if matches [n matches]))) (into {})))
(defn charsets
"Set of charsets that the Muuntaja instance can handle"
[m] (->> m (options) :charsets))
(defn default-charset
"Default charset of the Muuntaja instance"
[m] (->> m (options) :default-charset))
(defn default-format
"Default format of the Muuntaja instance"
[m] (->> m (options) :default-format))
(defn formats
"Set of formats that the Muuntaja instance can decode or encode"
[m] (->> m (options) :formats (map first) (set)))
;;
;; transforming options
;;
(defn transform-formats [options f]
(update options :formats #(into (empty %) (map (fn [[k v]] [k (f k v)]) %))))
(defn select-formats [options formats]
(let [existing-formats (-> options :formats keys set)
future-formats (set formats)]
(when-let [diff (seq (set/difference future-formats existing-formats))]
(throw
(ex-info
(str "invalid formats: " diff)
{:invalid (seq diff)
:formats (seq formats)
:existing (seq existing-formats)})))
(-> options
(update :formats select-keys formats)
(assoc :default-format (first formats)))))
;;
;; default options
;;
(defn extract-content-type-ring
"Extracts content-type from ring-request."
[request]
(or
(:content-type request)
(get (:headers request) "content-type")))
(defn extract-accept-ring
"Extracts accept from ring-request."
[request]
(get (:headers request) "accept"))
(defn extract-accept-charset-ring
"Extracts accept-charset from ring-request."
[request]
(get (:headers request) "accept-charset"))
(defn encode-collections-with-override [_ response]
(or (-> response :body coll?)
(-> response :muuntaja/encode?)))
(def available-charsets
"Set of recognised charsets by the current JVM"
(into #{} (map str/lower-case (.keySet (Charset/availableCharsets)))))
(def default-options
{:http {:extract-content-type extract-content-type-ring
:extract-accept-charset extract-accept-charset-ring
:extract-accept extract-accept-ring
:decode-request-body? (constantly true)
:encode-response-body? encode-collections-with-override}
:allow-empty-input? true
:default-charset "utf-8"
:charsets available-charsets
:default-format "application/json"
:formats {"application/json" json-format/json-format
"application/edn" edn-format/edn-format
"application/transit+json" transit-format/transit-json-format
"application/transit+msgpack" transit-format/transit-msgpack-format}})
;;
;; HTTP stuff
;;
(defn- fail-on-request-decode-exception [m
^Exception e
^FormatAndCharset request-format-and-charset
^FormatAndCharset response-format-and-charset
request]
(throw
(ex-info
(str "Malformed " (:format request-format-and-charset) " request.")
{:type :muuntaja/decode
:default-format (default-format m)
:format (:format request-format-and-charset)
:charset (:charset request-format-and-charset)
:request request}
e)))
(defn- fail-on-request-charset-negotiation [m]
(throw
(ex-info
"Can't negotiate on request charset"
{:type :muuntaja/request-charset-negotiation
:charsets (charsets m)})))
(defn- fail-on-response-charset-negotiation [m]
(throw
(ex-info
"Can't negotiate on response charset"
{:type :muuntaja/response-charset-negotiation
:charsets (charsets m)})))
(defn- fail-on-response-format-negotiation [m]
(throw
(ex-info
"Can't negotiate on response format"
{:type :muuntaja/response-format-negotiation
:formats (encodes m)})))
(defn- set-content-type [response content-type]
(util/assoc-assoc response :headers "Content-Type" content-type))
(defn- content-type [format charset]
(str format "; charset=" charset))
;;
;; request helpers
;;
(defn disable-request-decoding [request]
(assoc request :muuntaja/format nil))
;;
;; response helpers
;;
(defn disable-response-encoding [response]
(assoc response :muuntaja/format nil))
(defn set-response-content-type [response content-type]
(assoc response :muuntaja/content-type content-type))
;;
;; Memoizable negotiation functions
;;
(defn- -negotiate-content-type [m s]
(let [consumes (decodes m)
matchers (matchers m)
default-charset (default-charset m)
charsets (charsets m)]
(if s
(let [[content-type-raw charset-raw] (parse/parse-content-type s)]
(records/->FormatAndCharset
(if content-type-raw
(or (consumes content-type-raw)
(some
(fn [[name r]]
(if (and r (re-find r content-type-raw)) name))
matchers)))
(or
;; if a provided charset was valid
(and charset-raw charsets (charsets charset-raw) charset-raw)
;; only set default if none were set
(and (not charset-raw) default-charset)
;; negotiation failed
(fail-on-request-charset-negotiation m)))))))
;; TODO: fail if no match?
(defn- -negotiate-accept [m s]
(let [produces (encodes m)
default-format (default-format m)]
(or
(util/some-value
produces
(parse/parse-accept s))
default-format)))
;; TODO: fail if no match?
(defn- -negotiate-accept-charset [m s]
(let [charsets (charsets m)
default-charset (default-charset m)]
(or
(util/some-value
(or charsets identity)
(parse/parse-accept-charset s))
default-charset)))
;;
;; Creation
;;
(defn- key-set [m accept?]
(set (for [[k v] m :when (accept? v)] k)))
;; TODO: we know th format, so we should delegate to formatter how to handle the empty input
(defn- on-exception [allow-empty-input? ^Exception e format type]
(let [message (.getMessage e)]
(if-not (try
(and allow-empty-input?
(or
;; msgpack
(instance? EOFException e)
;; transit
(some->> e .getCause (instance? EOFException))
;; jsonista
(and (instance? IOException e)
message (.startsWith message "No content to map due to end-of-input"))
;; edn
(and (instance? RuntimeException e)
(= message "EOF while reading"))))
(catch Exception _))
(throw
(ex-info
(str "Malformed " format " in " type "")
{:type type
:format format}
e)))))
(defn- create-coder [format type spec spec-opts default-charset allow-empty-input? [p pf]]
(let [decode? (= type :muuntaja/decode)
g (if (vector? spec)
(let [[f opts] spec]
(f (merge opts spec-opts)))
spec)
prepare (if decode? protocols/as-input-stream identity)
on-exception (partial on-exception allow-empty-input?)]
(if (and p pf)
(fn f
([x]
(f x default-charset))
([x charset]
(try
(if (and (record? x) (satisfies? p x))
(pf x charset)
(g (prepare x) charset))
(catch Exception e
(on-exception e format type)))))
(fn f
([x]
(f x default-charset))
([x charset]
(try
(g (prepare x) charset)
(catch Exception e
(on-exception e format type))))))))
(defn- create-adapters [formats default-charset allow-empty-input?]
(->> (for [[format {:keys [decoder decoder-opts encoder encoder-opts encode-protocol]}] formats]
(if-not (or encoder decoder)
(throw
(ex-info
(str "invalid format: " format)
{:format format
:formats (keys formats)}))
[format (records/map->Adapter
(merge
(if decoder {:decode (create-coder format :muuntaja/decode decoder decoder-opts default-charset allow-empty-input? nil)})
(if encoder {:encode (create-coder format :muuntaja/encode encoder encoder-opts default-charset allow-empty-input? encode-protocol)})))]))
(into {})))
(defn create
"Creates a new Muuntaja intance from a given prototype:
- existing Muuntaja (no-op)
- options-map (new created)
- nothing (new created with default-options)"
([]
(create default-options))
([muuntaja-or-options]
(if (satisfies? Muuntaja muuntaja-or-options)
muuntaja-or-options
(let [{:keys [formats
default-format
charsets
default-charset
allow-empty-input?] :as options} muuntaja-or-options
adapters (create-adapters formats default-charset allow-empty-input?)
valid-format? (key-set formats identity)]
(when-not (or (not default-format) (valid-format? default-format))
(throw
(ex-info
(str "Invalid default format " default-format)
{:formats valid-format?
:default-format default-format})))
(let [m (reify Muuntaja
(adapters [_] adapters)
(options [_] options))
{:keys [extract-content-type
extract-accept-charset
extract-accept
decode-request-body?
encode-response-body?]} (:http options)
produces (encodes m)
-encoder (fn [format]
(if-let [^Adapter adapter (adapters format)]
(.-encode adapter)))
-decoder (fn [format]
(if-let [^Adapter adapter (adapters format)]
(.-decode adapter)))
-negotiate-accept-charset (parse/fast-memoize 1000 (partial -negotiate-accept-charset m))
-negotiate-accept (parse/fast-memoize 1000 (partial -negotiate-accept m))
-negotiate-content-type (parse/fast-memoize 1000 (partial -negotiate-content-type m))
-decode-request? (fn [request]
(and (not (contains? request :muuntaja/format))
(decode-request-body? request)))
-decode-request-body (fn [m request ^FormatAndCharset req-fc ^FormatAndCharset res-fc]
(if (-decode-request? request)
(if-let [decode (decoder m (if req-fc (.-format req-fc)))]
(try
(decode (:body request) (.-charset req-fc))
(catch Exception e
(fail-on-request-decode-exception m e req-fc res-fc request))))))
-encode-response? (fn [request response]
(and (map? response)
(not (contains? response :muuntaja/format))
(encode-response-body? request response)))
-resolve-response-charset (fn [response request]
(or (if-let [ct (some-> request :muuntaja/response :charset)]
(charsets ct))
default-charset
(fail-on-response-charset-negotiation m)))
-resolve-response-format (fn [response request]
(or (if-let [ct (:muuntaja/content-type response)]
(produces ct))
(some-> request :muuntaja/response :format)
default-format
(fail-on-response-format-negotiation m)))
-handle-response (fn [response format encoder charset]
(as-> response $
(assoc $ :muuntaja/format format)
(dissoc $ :muuntaja/content-type)
(update $ :body encoder charset)
(if-not (get (:headers $) "Content-Type")
(set-content-type $ (content-type format charset))
$)))]
^{:type ::muuntaja}
(reify
Muuntaja
(encoder [_ format]
(-encoder format))
(decoder [_ format]
(-decoder format))
(adapters [_]
adapters)
(options [_]
options)
MuuntajaHttp
(request-format [_ request]
(-negotiate-content-type (extract-content-type request)))
(response-format [_ request]
(records/->FormatAndCharset
(-negotiate-accept (extract-accept request))
(-negotiate-accept-charset (extract-accept-charset request))))
(negotiate-request-response [this request]
(-> request
(assoc :muuntaja/request (request-format this request))
(assoc :muuntaja/response (response-format this request))))
(format-request [this request]
(let [req-fc (:muuntaja/request request)
res-fc (:muuntaja/response request)
body (-decode-request-body this request req-fc res-fc)]
(cond-> request
(not (nil? body)) (-> (assoc :muuntaja/format req-fc)
(assoc :body-params body)))))
(format-response [_ request response]
(or
(if (-encode-response? request response)
(if-let [format (-resolve-response-format response request)]
(if-let [charset (-resolve-response-charset response request)]
(if-let [encoder (-encoder format)]
(-handle-response response format encoder charset)))))
response))
(negotiate-and-format-request [this request]
(let [req-fc (request-format this request)
res-fc (response-format this request)
body (-decode-request-body this request req-fc res-fc)]
(as-> request $
(assoc $ :muuntaja/request req-fc)
(assoc $ :muuntaja/response res-fc)
(if (not (nil? body))
(-> $
(assoc :muuntaja/format req-fc)
(assoc :body-params body))
$))))))))))
(defmethod print-method ::muuntaja
[_ ^Writer w]
(.write w (str "<<Muuntaja>>")))