/
body_params.clj
148 lines (127 loc) · 5.72 KB
/
body_params.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
; Copyright 2013 Relevance, Inc.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
; which can be found in the file epl-v10.html at the root of this distribution.
;
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
;
; You must not remove this notice, or any other, from this software.
(ns io.pedestal.service.http.body-params
(:require [clojure.edn :as edn]
[cheshire.core :as json]
[cheshire.parse :as parse]
[clojure.string :as str]
[io.pedestal.service.interceptor :as interceptor :refer [definterceptorfn]]
[io.pedestal.service.log :as log]
[ring.middleware.params :as params]))
(defn- parser-for
"Find a parser for the given content-type, never returns nil"
[parser-map content-type]
(or (when content-type
(parser-map (some #(when (re-find % content-type) %) (keys parser-map))))
identity))
(defn- parse-content-type
"Runs the request through the appropriate parser"
[parser-map request]
((parser-for parser-map (:content-type request)) request))
(defn- set-content-type
"Changes the content type of a request"
[request content-type]
(-> request
(assoc :content-type content-type)
(assoc-in [:headers "Content-Type"] content-type)))
(defn- convert-middleware
"Turn a ring middleware into a parser. If a content type is given, return a parser
that will ensure that the handler sees that content type in the request"
([wrap-fn] (fn [request] (wrap-fn identity)))
([wrap-fn expected-content-type]
(let [parser (wrap-fn identity)]
(fn [request]
(let [retyped-request (set-content-type request expected-content-type)
parsed-request (parser retyped-request)]
(set-content-type parsed-request (:content-type request)))))))
(defn add-parser
[parser-map content-type parser-fn]
(assoc parser-map (re-pattern (str "^" content-type "$") parser-fn)))
(defn add-ring-middleware
[parser-map content-type middleware]
(add-parser parser-map content-type (convert-middleware middleware)))
(defn custom-edn-parser
"Return an edn-parser fn that, given a request, will read the body of that
request with `edn/read`. options are key-val pairs that will be passed as a
hash-map to `edn/read`."
[& options]
(let [edn-options (merge {:eof nil}
(apply hash-map options))]
(fn [request]
(let [encoding (or (:character-encoding request) "UTF-8")]
(assoc request
:edn-params (->
(:body request)
(java.io.InputStreamReader. encoding)
java.io.PushbackReader.
(->> (edn/read edn-options))))))))
(def edn-parser
"Take a request and parse its body as edn."
(custom-edn-parser))
(defn- json-read
"Parse json stream, supports parser-options with key-val pairs:
:bigdec Boolean value which defines if numbers are parsed as BigDecimal
or as Number with simplest/smallest possible numeric value.
Defaults to false.
:key-fn Key coercion, where true coerces keys to keywords, false leaves
them as strings, or a function to provide custom coercion.
:array-coerce-fn Define a collection to be used for array values by name."
[reader & options]
(let [{:keys [bigdec key-fn array-coerce-fn]
:or {bigdec false
key-fn nil
array-coerce-fn nil}} options]
(binding [parse/*use-bigdecimals?* bigdec]
(json/parse-stream (java.io.PushbackReader. reader) key-fn array-coerce-fn))))
(defn custom-json-parser
"Return a json-parser fn that, given a request, will read the body of that
request with `json/read`. options are key-val pairs that will be passed along
to `json/read`."
[& options]
(fn [request]
(let [encoding (or (:character-encoding request) "UTF-8")]
(assoc request
:json-params
(apply json-read
(-> (:body request)
(java.io.InputStreamReader. encoding))
options)))))
(def json-parser
"Take a request and parse its body as json."
(custom-json-parser))
(defn form-parser
"Take a request and parse its body as a form."
[request]
(let [encoding (or (:character-encoding request) "UTF-8")]
(params/assoc-form-params request encoding)))
(defn default-parser-map
"Return a map of MIME-type to parsers. Included types are edn, json and
form-encoding. parser-options are key-val pairs, valid options are:
:edn-options A hash-map of options to be used when invoking `edn/read`.
:json-options A hash-map of options to be used when invoking `json/read`.
Examples:
(default-parser-map :json-options {:key-fn keyword})
;; This parser-map would parse the json body '{\"foo\": \"bar\"}' as
;; {:foo \"bar\"}
(default-parser-map :edn-options {:readers *data-readers*})
;; This parser-map will parse edn bodies using any custom edn readers you
;; define (in a data_readers.clj file, for example.)"
[& parser-options]
(let [{:keys [edn-options json-options]} (apply hash-map parser-options)
edn-options-vec (apply concat edn-options)
json-options-vec (apply concat json-options)]
{#"^application/edn" (apply custom-edn-parser edn-options-vec)
#"^application/json" (apply custom-json-parser json-options-vec)
#"^application/x-www-form-urlencoded" form-parser}))
(definterceptorfn body-params
([] (body-params (default-parser-map)))
([parser-map]
(interceptor/on-request ::body-params
(fn [request] (parse-content-type parser-map request)))))