Handle URL-encoded paths in file and resource handlers

This commit is contained in:
Miikka Koskinen 2021-04-30 14:28:12 +03:00
parent f212edfcd6
commit 1297cfd902
3 changed files with 26 additions and 5 deletions

View file

@ -0,0 +1 @@
hello

View file

@ -222,12 +222,12 @@
(recur files)))))) (recur files))))))
handler (if path handler (if path
(fn [request] (fn [request]
(let [uri (:uri request)] (let [uri (impl/url-decode (:uri request))]
(if (str/starts-with? uri path) (if (str/starts-with? uri path)
(or (path-or-index-response (subs uri path-size) uri) (or (path-or-index-response (subs uri path-size) uri)
(not-found-handler request))))) (not-found-handler request)))))
(fn [request] (fn [request]
(let [uri (:uri request) (let [uri (impl/url-decode (:uri request))
path (-> request :path-params parameter)] path (-> request :path-params parameter)]
(or (path-or-index-response path uri) (or (path-or-index-response path uri)
(not-found-handler request)))))] (not-found-handler request)))))]

View file

@ -471,9 +471,9 @@
(testing "from root" (testing "from root"
(let [app (ring/ring-handler (let [app (ring/ring-handler
(ring/router (ring/router
["/*" (create nil)]) ["/*" (create nil)])
(ring/create-default-handler))] (ring/create-default-handler))]
(testing "different file-types" (testing "different file-types"
(let [response (app (request "/hello.json"))] (let [response (app (request "/hello.json"))]
(is (= "application/json" (get-in response [:headers "Content-Type"]))) (is (= "application/json" (get-in response [:headers "Content-Type"])))
@ -484,6 +484,11 @@
(is (get-in response [:headers "Last-Modified"])) (is (get-in response [:headers "Last-Modified"]))
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response)))))) (is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
(testing "with url decoding"
(let [response (app (request "/with%20space.txt"))]
(is (= 200 (:status response)))
(is (= "hello\n" (slurp (:body response))))))
(testing "index-files" (testing "index-files"
(let [response (app (request "/docs"))] (let [response (app (request "/docs"))]
(is (= (redirect "/docs/index.html") response))) (is (= (redirect "/docs/index.html") response)))
@ -520,6 +525,11 @@
(is (get-in response [:headers "Last-Modified"])) (is (get-in response [:headers "Last-Modified"]))
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response)))))) (is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
(testing "with url decoding"
(let [response (app (request "/with%20space.txt"))]
(is (= 200 (:status response)))
(is (= "hello\n" (slurp (:body response))))))
(testing "index-files" (testing "index-files"
(let [response (app (request "/docs"))] (let [response (app (request "/docs"))]
(is (= (redirect "/docs/index.html") response))) (is (= (redirect "/docs/index.html") response)))
@ -557,6 +567,11 @@
(is (get-in response [:headers "Last-Modified"])) (is (get-in response [:headers "Last-Modified"]))
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response)))))) (is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
(testing "with url decoding"
(let [response (app (request "/with%20space.txt"))]
(is (= 200 (:status response)))
(is (= "hello\n" (slurp (:body response))))))
(testing "index-files" (testing "index-files"
(let [response (app (request "/docs"))] (let [response (app (request "/docs"))]
(is (= (redirect "/docs/index.html") response))) (is (= (redirect "/docs/index.html") response)))
@ -595,6 +610,11 @@
(is (get-in response [:headers "Last-Modified"])) (is (get-in response [:headers "Last-Modified"]))
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response)))))) (is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
(testing "with url decoding"
(let [response (app (request "/with%20space.txt"))]
(is (= 200 (:status response)))
(is (= "hello\n" (slurp (:body response))))))
(testing "index-files" (testing "index-files"
(let [response (app (request "/docs"))] (let [response (app (request "/docs"))]
(is (= (redirect "/docs/index.html") response))) (is (= (redirect "/docs/index.html") response)))