Skip to content

Commit fdf9d04

Browse files
committed
Add :max-file-count option to multipart middleware
1 parent 73da012 commit fdf9d04

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,20 @@
8888
(def ^:private default-store (delay (tf/temp-file-store)))
8989

9090
(defn- parse-multipart-params
91-
[request {:keys [encoding fallback-encoding store] :as options}]
91+
[request {:keys [encoding fallback-encoding store max-file-count]
92+
:as options}]
9293
(let [store (or store @default-store)
9394
fallback-encoding (or encoding
9495
fallback-encoding
9596
(req/character-encoding request)
9697
"UTF-8")]
9798
(->> (request-context request fallback-encoding)
9899
(file-item-seq (file-upload request options))
99-
(map #(parse-file-item % store))
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))))
100105
(build-param-map encoding fallback-encoding))))
101106

102107
(defn multipart-params-request
@@ -145,7 +150,10 @@
145150
bytes-read, content-length, and item-count.
146151
147152
:max-file-size - the maximum size allowed size of a file in bytes. If
148-
nil or omitted, there is no limit."
153+
nil or omitted, there is no limit.
154+
155+
:max-file-count - the maximum number of files allowed in a single request.
156+
If nil or omitted, there is no limit."
149157
([handler]
150158
(wrap-multipart-params handler {}))
151159
([handler options]

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,18 @@
182182
"name=\"upload\"; filename=\"test1.txt\"\r\n"
183183
"Content-Type: text/plain\r\n\r\n"
184184
"foobarbaz\r\n"
185+
"--XXXX\r\n"
186+
"Content-Disposition: form-data;"
187+
"name=\"upload\"; filename=\"test2.txt\"\r\n"
188+
"Content-Type: text/plain\r\n\r\n"
189+
"foobar\r\n"
185190
"--XXXX--")
186-
request {:headers {"content-type"
187-
(str "multipart/form-data; boundary=XXXX")}
188-
:body (string-input-stream form-body)}]
191+
headers {"content-type" (str "multipart/form-data; boundary=XXXX")}]
189192
(is (thrown? org.apache.commons.fileupload.FileUploadBase$FileUploadIOException
190-
(multipart-params-request request {:max-file-size 6})))))
193+
(multipart-params-request
194+
{:headers headers, :body (string-input-stream form-body)}
195+
{:max-file-size 6})))
196+
(is (thrown? clojure.lang.ExceptionInfo
197+
(multipart-params-request
198+
{:headers headers, :body (string-input-stream form-body)}
199+
{:max-file-count 1})))))

0 commit comments

Comments
 (0)