Skip to content

Commit 23f93e6

Browse files
authored
Merge pull request #2366 from nasa/CMR-11037
CMR-11037: Fix middleware for body-copy to work properly with form data
2 parents d930b28 + 177f693 commit 23f93e6

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

search-app/src/cmr/search/routes.clj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
[cmr.common.config :refer [defconfig]]
1717
[cmr.common.mime-types :as mt]
1818
[cmr.common.services.errors :as svc-errors]
19-
[cmr.search.api.routes :as api-routes]
19+
[cmr.search.api.routes :as api-routes]
2020
[cmr.search.data.granule-counts-cache :as granule-counts-cache]
2121
[cmr.search.middleware.shapefile :as shapefile]
2222
[cmr.search.middleware.shapefile-simplification :as shapefile-simplifier]
2323
[cmr.search.services.messages.common-messages :as msg]
2424
[cmr.search.site.routes :as site-routes]
2525
[compojure.core :refer [GET POST context routes]]
2626
[ring.middleware.keyword-params :as keyword-params]
27+
[ring.middleware.multipart-params :as multipart]
2728
[ring.middleware.nested-params :as nested-params]
2829
[ring.middleware.params :as params]))
2930

@@ -55,13 +56,21 @@
5556
[(msg/mixed-arity-parameter-msg mixed-param)]))
5657
(handler request)))
5758

59+
(defn parse-multipart-body [request]
60+
(let [parsed (multipart/multipart-params-request request)]
61+
(string/join "&" (for [[k v] (:multipart-params parsed)]
62+
(str (name k) "=" v)))))
63+
5864
(defn copy-of-body-handler
5965
"Copies the body into a new attribute called :body-copy so that after a post
6066
of form content type the original body can still be read. The default ring
6167
params reads the body and parses it and we don't have access to it."
6268
[handler]
6369
(fn [request]
64-
(let [^String body (slurp (:body request))]
70+
(let [content-type (get-in request [:headers "content-type"])
71+
body (if (and content-type (string/starts-with? content-type "multipart/form-data"))
72+
(parse-multipart-body request)
73+
(slurp (:body request)))]
6574
(handler (assoc
6675
request
6776
:body-copy body

0 commit comments

Comments
 (0)