Skip to content

Commit cd881bd

Browse files
committed
Changes based on review 3
- Reformat docstring - Ensure we're not catching InvalidFileNameExceptions thrown by other middleware. - Move try/catch down the callstack to reduce code duplication.
1 parent 0457c5d commit cd881bd

File tree

1 file changed

+64
-58
lines changed

1 file changed

+64
-58
lines changed

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

+64-58
Original file line numberDiff line numberDiff line change
@@ -112,33 +112,38 @@
112112
func (load-var store)]
113113
(func))))
114114

115+
(defn default-invalid-filename-handler [request e]
116+
(response/bad-request (.getMessage e)))
117+
115118
(defn multipart-params-request
116119
"Adds :multipart-params and :params keys to request.
117120
See: wrap-multipart-params."
118121
{:added "1.2"}
119122
([request]
120123
(multipart-params-request request {}))
121124
([request options]
122-
(let [store (or (:store options) @default-store)
123-
forced-encoding (:encoding options)
124-
req-encoding (or forced-encoding
125-
(:fallback-encoding options)
126-
(req/character-encoding request)
127-
"UTF-8")
128-
progress (:progress-fn options)
129-
params (if (multipart-form? request)
130-
(parse-multipart-params request
131-
req-encoding
132-
forced-encoding
133-
store
134-
progress)
135-
{})]
136-
(merge-with merge request
137-
{:multipart-params params}
138-
{:params params}))))
139-
140-
(defn default-invalid-filename-handler [request e]
141-
(response/bad-request (.getMessage e)))
125+
(try
126+
(let [store (or (:store options) @default-store)
127+
forced-encoding (:encoding options)
128+
req-encoding (or forced-encoding
129+
(:fallback-encoding options)
130+
(req/character-encoding request)
131+
"UTF-8")
132+
progress (:progress-fn options)
133+
params (if (multipart-form? request)
134+
(parse-multipart-params request
135+
req-encoding
136+
forced-encoding
137+
store
138+
progress)
139+
{})]
140+
(merge-with merge request
141+
{:multipart-params params}
142+
{:params params}))
143+
(catch InvalidFileNameException e
144+
(let [invalid-filename-handler
145+
(:invalid-filename-handler options default-invalid-filename-handler)]
146+
(with-meta (invalid-filename-handler request e) {::error? true}))))))
142147

143148
(defn wrap-multipart-params
144149
"Middleware to parse multipart parameters from a request. Adds the
@@ -149,47 +154,48 @@
149154
150155
The following options are accepted
151156
152-
:encoding - character encoding to use for multipart parsing.
153-
Overrides the encoding specified in the request. If not
154-
specified, uses the encoding specified in a part named
155-
\"_charset_\", or the content type for each part, or
156-
request character encoding if the part has no encoding,
157-
or \"UTF-8\" if no request character encoding is set.
158-
159-
:fallback-encoding - specifies the character encoding used in parsing if a
160-
part of the request does not specify encoding in its
161-
content type or no part named \"_charset_\" is present.
162-
Has no effect if :encoding is also set.
163-
164-
:store - a function that stores a file upload. The function
165-
should expect a map with :filename, :content-type and
166-
:stream keys, and its return value will be used as the
167-
value for the parameter in the multipart parameter map.
168-
The default storage function is the temp-file-store.
169-
170-
:progress-fn - a function that gets called during uploads. The
171-
function should expect four parameters: request,
172-
bytes-read, content-length, and item-count.
173-
174-
:invalid-filename-handler - a function that gets called when the file being uploaded
175-
has an invalid name. The function should expect two
176-
parameters: request and an exception of type
177-
InvalidFileNameException."
157+
:encoding
158+
- Character encoding to use for multipart parsing.
159+
Overrides the encoding specified in the request. If not
160+
specified, uses the encoding specified in a part named
161+
\"_charset_\", or the content type for each part, or
162+
request character encoding if the part has no encoding,
163+
or \"UTF-8\" if no request character encoding is set.
164+
165+
:fallback-encoding
166+
- Specifies the character encoding used in parsing if a
167+
part of the request does not specify encoding in its
168+
content type or no part named \"_charset_\" is present.
169+
Has no effect if :encoding is also set.
170+
171+
:store
172+
- A function that stores a file upload. The function
173+
should expect a map with :filename, :content-type and
174+
:stream keys, and its return value will be used as the
175+
value for the parameter in the multipart parameter map.
176+
The default storage function is the temp-file-store.
177+
178+
:progress-fn
179+
- A function that gets called during uploads. The
180+
function should expect four parameters: request,
181+
bytes-read, content-length, and item-count.
182+
183+
:invalid-filename-handler
184+
- A function that gets called when the file being uploaded
185+
has an invalid name. The function should expect two
186+
parameters: request and an exception of type
187+
InvalidFileNameException."
178188
([handler]
179189
(wrap-multipart-params handler {}))
180190
([handler options]
181191
(fn
182192
([request]
183-
(let [invalid-file-name-handler
184-
(:invalid-filename-handler options default-invalid-filename-handler)]
185-
(try
186-
(handler (multipart-params-request request options))
187-
(catch InvalidFileNameException e
188-
(invalid-file-name-handler request e)))))
193+
(let [multipart-params-request (multipart-params-request request options)]
194+
(if (contains? (meta multipart-params-request) ::error?)
195+
multipart-params-request
196+
(handler multipart-params-request))))
189197
([request respond raise]
190-
(let [invalid-file-name-handler
191-
(:invalid-filename-handler options default-invalid-filename-handler)]
192-
(try
193-
(handler (multipart-params-request request options) respond raise)
194-
(catch InvalidFileNameException e
195-
(respond (invalid-file-name-handler request e)))))))))
198+
(let [multipart-params-request (multipart-params-request request options)]
199+
(if (contains? (meta multipart-params-request) ::error?)
200+
(handler multipart-params-request respond raise)
201+
(respond multipart-params-request)))))))

0 commit comments

Comments
 (0)