Skip to content

Commit 45d4c62

Browse files
committed
Make parse-multipart-params function public
Fixes #520.
1 parent d908b6a commit 45d4c62

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

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

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,30 @@
8686

8787
(def ^:private default-store (delay (tf/temp-file-store)))
8888

89-
(defn- parse-multipart-params
90-
[request {:keys [encoding fallback-encoding store max-file-count]
91-
:as options}]
92-
(let [store (or store @default-store)
93-
fallback-encoding (or encoding
94-
fallback-encoding
95-
(req/character-encoding request)
96-
"UTF-8")]
97-
(->> (request-context request fallback-encoding)
98-
(file-item-iterable (file-upload request options))
99-
(sequence
100-
(map-indexed (fn [i item]
101-
(if (and max-file-count (>= i max-file-count))
102-
(throw (ex-info "Max file count exceeded"
103-
{:max-file-count max-file-count}))
104-
(parse-file-item item store)))))
105-
(build-param-map encoding fallback-encoding))))
89+
(defn parse-multipart-params
90+
"Parse a multipart request map and return a map of parameters. For a list of
91+
available options, see: wrap-multipart-params."
92+
{:added "1.14"}
93+
([request]
94+
(parse-multipart-params request {}))
95+
([request options]
96+
(when (multipart-form? request)
97+
(let [store (or (:store options) @default-store)
98+
max-file-count (:max-file-count options)
99+
encoding (:encoding options)
100+
fallback-encoding (or encoding
101+
(:fallback-encoding options)
102+
(req/character-encoding request)
103+
"UTF-8")]
104+
(->> (request-context request fallback-encoding)
105+
(file-item-iterable (file-upload request options))
106+
(sequence
107+
(map-indexed (fn [i item]
108+
(if (and max-file-count (>= i max-file-count))
109+
(throw (ex-info "Max file count exceeded"
110+
{:max-file-count max-file-count}))
111+
(parse-file-item item store)))))
112+
(build-param-map encoding fallback-encoding))))))
106113

107114
(defn multipart-params-request
108115
"Adds :multipart-params and :params keys to request.
@@ -111,9 +118,7 @@
111118
([request]
112119
(multipart-params-request request {}))
113120
([request options]
114-
(let [params (if (multipart-form? request)
115-
(parse-multipart-params request options)
116-
{})]
121+
(let [params (or (parse-multipart-params request options) {})]
117122
(merge-with merge request
118123
{:multipart-params params}
119124
{:params params}))))

ring-core/test/ring/middleware/test/multipart_params.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@
5050
(is (= (get-in response [:params "foo"])
5151
["bar" "baz"]))))
5252

53+
(deftest test-parse-multipart-params
54+
(let [form-body (str "--XXXX\r\n"
55+
"Content-Disposition: form-data;"
56+
"name=\"foo\"\r\n\r\n"
57+
"bar\r\n"
58+
"--XXXX")
59+
request {:headers {"content-type" "multipart/form-data; boundary=XXXX"
60+
"content-length" (str (count form-body))}
61+
:body (string-input-stream form-body)}]
62+
(is (= {"foo" "bar"} (parse-multipart-params request)))
63+
(is (nil? (parse-multipart-params {:headers {}})))))
64+
5365
(defn all-threads []
5466
(.keySet (Thread/getAllStackTraces)))
5567

0 commit comments

Comments
 (0)