Skip to content

Commit 1297cfd

Browse files
author
Miikka Koskinen
committed
Handle URL-encoded paths in file and resource handlers
1 parent f212edf commit 1297cfd

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

dev-resources/public/with space.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello

modules/reitit-ring/src/reitit/ring.cljc

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@
222222
(recur files))))))
223223
handler (if path
224224
(fn [request]
225-
(let [uri (:uri request)]
225+
(let [uri (impl/url-decode (:uri request))]
226226
(if (str/starts-with? uri path)
227227
(or (path-or-index-response (subs uri path-size) uri)
228228
(not-found-handler request)))))
229229
(fn [request]
230-
(let [uri (:uri request)
230+
(let [uri (impl/url-decode (:uri request))
231231
path (-> request :path-params parameter)]
232232
(or (path-or-index-response path uri)
233233
(not-found-handler request)))))]

test/cljc/reitit/ring_test.cljc

+23-3
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,9 @@
471471

472472
(testing "from root"
473473
(let [app (ring/ring-handler
474-
(ring/router
475-
["/*" (create nil)])
476-
(ring/create-default-handler))]
474+
(ring/router
475+
["/*" (create nil)])
476+
(ring/create-default-handler))]
477477
(testing "different file-types"
478478
(let [response (app (request "/hello.json"))]
479479
(is (= "application/json" (get-in response [:headers "Content-Type"])))
@@ -484,6 +484,11 @@
484484
(is (get-in response [:headers "Last-Modified"]))
485485
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
486486

487+
(testing "with url decoding"
488+
(let [response (app (request "/with%20space.txt"))]
489+
(is (= 200 (:status response)))
490+
(is (= "hello\n" (slurp (:body response))))))
491+
487492
(testing "index-files"
488493
(let [response (app (request "/docs"))]
489494
(is (= (redirect "/docs/index.html") response)))
@@ -520,6 +525,11 @@
520525
(is (get-in response [:headers "Last-Modified"]))
521526
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
522527

528+
(testing "with url decoding"
529+
(let [response (app (request "/with%20space.txt"))]
530+
(is (= 200 (:status response)))
531+
(is (= "hello\n" (slurp (:body response))))))
532+
523533
(testing "index-files"
524534
(let [response (app (request "/docs"))]
525535
(is (= (redirect "/docs/index.html") response)))
@@ -557,6 +567,11 @@
557567
(is (get-in response [:headers "Last-Modified"]))
558568
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
559569

570+
(testing "with url decoding"
571+
(let [response (app (request "/with%20space.txt"))]
572+
(is (= 200 (:status response)))
573+
(is (= "hello\n" (slurp (:body response))))))
574+
560575
(testing "index-files"
561576
(let [response (app (request "/docs"))]
562577
(is (= (redirect "/docs/index.html") response)))
@@ -595,6 +610,11 @@
595610
(is (get-in response [:headers "Last-Modified"]))
596611
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
597612

613+
(testing "with url decoding"
614+
(let [response (app (request "/with%20space.txt"))]
615+
(is (= 200 (:status response)))
616+
(is (= "hello\n" (slurp (:body response))))))
617+
598618
(testing "index-files"
599619
(let [response (app (request "/docs"))]
600620
(is (= (redirect "/docs/index.html") response)))

0 commit comments

Comments
 (0)