|
13 | 13 | [ring.util.parsing :as parsing])
|
14 | 14 | (:import [org.apache.commons.fileupload
|
15 | 15 | UploadContext
|
16 |
| - FileItemIterator |
17 | 16 | FileItemStream
|
18 | 17 | FileUpload
|
| 18 | + FileUploadBase$FileUploadIOException |
19 | 19 | ProgressListener]
|
20 | 20 | [org.apache.commons.io IOUtils]))
|
21 | 21 |
|
|
119 | 119 | {:multipart-params params}
|
120 | 120 | {:params params}))))
|
121 | 121 |
|
| 122 | +(defn content-too-large-handler |
| 123 | + "A handler function that responds with a minimal 413 Content Too Large |
| 124 | + response." |
| 125 | + ([_] |
| 126 | + {:status 413 |
| 127 | + :headers {"Content-Type" "text/plain; charset=UTF-8"} |
| 128 | + :body "Uploaded content exceeded limits."}) |
| 129 | + ([request respond _] |
| 130 | + (respond (content-too-large-handler request)))) |
| 131 | + |
| 132 | +(defn- handle-request-and-errors [requestf handlef errorf] |
| 133 | + ((try |
| 134 | + (let [request (requestf)] |
| 135 | + #(handlef request)) |
| 136 | + (catch FileUploadBase$FileUploadIOException _ |
| 137 | + errorf) |
| 138 | + (catch clojure.lang.ExceptionInfo _ |
| 139 | + errorf)))) |
| 140 | + |
122 | 141 | (defn wrap-multipart-params
|
123 | 142 | "Middleware to parse multipart parameters from a request. Adds the
|
124 | 143 | following keys to the request map:
|
|
154 | 173 | nil or omitted, there is no limit.
|
155 | 174 |
|
156 | 175 | :max-file-count - the maximum number of files allowed in a single request.
|
157 |
| - If nil or omitted, there is no limit." |
| 176 | + If nil or omitted, there is no limit. |
| 177 | +
|
| 178 | + :error-handler - a handler that is invoked when the :max-file-size or |
| 179 | + :max-file-count limits are exceeded. Defaults to |
| 180 | + using the content-too-large-handler function." |
158 | 181 | ([handler]
|
159 | 182 | (wrap-multipart-params handler {}))
|
160 | 183 | ([handler options]
|
161 |
| - (fn |
162 |
| - ([request] |
163 |
| - (handler (multipart-params-request request options))) |
164 |
| - ([request respond raise] |
165 |
| - (handler (multipart-params-request request options) respond raise))))) |
| 184 | + (let [error-handler (:error-handler options content-too-large-handler)] |
| 185 | + (fn |
| 186 | + ([request] |
| 187 | + (handle-request-and-errors |
| 188 | + #(multipart-params-request request options) |
| 189 | + handler |
| 190 | + #(error-handler request))) |
| 191 | + ([request respond raise] |
| 192 | + (handle-request-and-errors |
| 193 | + #(multipart-params-request request options) |
| 194 | + #(handler % respond raise) |
| 195 | + #(error-handler request respond raise))))))) |
0 commit comments