Add option to disable index files served on paths that are not directories

Signed-off-by: Loukas Agorgianitis <loukas@agorgianitis.com>
This commit is contained in:
Loukas Agorgianitis 2025-03-27 14:17:30 +02:00
parent 89c05932dc
commit 12afe1dcaa
No known key found for this signature in database
GPG key ID: DDC6FA7D5BB332E6
3 changed files with 65 additions and 36 deletions

View file

@ -55,13 +55,14 @@ This way, they are only served if none of the actual routes have matched.
`reitit.ring/create-file-handler` and `reitit.ring/create-resource-handler` take optionally an options map to configure how the files are being served. `reitit.ring/create-file-handler` and `reitit.ring/create-resource-handler` take optionally an options map to configure how the files are being served.
| key | description | | key | description |
| -------------------|-------------| | --------------------|-------------|
| :parameter | optional name of the wildcard parameter, defaults to unnamed keyword `:` | :parameter | optional name of the wildcard parameter, defaults to unnamed keyword `:`
| :root | optional resource root, defaults to `\"public\"` | :root | optional resource root, defaults to `\"public\"`
| :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router. | :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router.
| :loader | optional class loader to resolve the resources | :loader | optional class loader to resolve the resources
| :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]` | :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]`
| :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly | :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly
| :canonicalize-uris? | optional boolean: if true (default), try to serve index files for non directory paths (paths that end with slash)
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found) | :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)

View file

@ -227,11 +227,12 @@
;; TODO: ring.middleware.head/wrap-head ;; TODO: ring.middleware.head/wrap-head
;; TODO: handle etags ;; TODO: handle etags
(defn -create-file-or-resource-handler (defn -create-file-or-resource-handler
[response-fn {:keys [parameter root path loader allow-symlinks? index-files index-redirect? paths not-found-handler] [response-fn {:keys [parameter root path loader allow-symlinks? index-files index-redirect? canonicalize-uris? paths not-found-handler]
:or {parameter (keyword "") :or {parameter (keyword "")
root "public" root "public"
index-files ["index.html"] index-files ["index.html"]
index-redirect? true index-redirect? true
canonicalize-uris? true
paths (constantly nil)}}] paths (constantly nil)}}]
(let [options {:root root (let [options {:root root
:loader loader :loader loader
@ -254,13 +255,14 @@
(response/content-type response (mime-type/ext-mime-type path)))) (response/content-type response (mime-type/ext-mime-type path))))
path-or-index-response (fn [path uri] path-or-index-response (fn [path uri]
(or (response path) (or (response path)
(when (or canonicalize-uris? (str/ends-with? uri "/"))
(loop [[file & files] index-files] (loop [[file & files] index-files]
(if file (if file
(if-let [resp (response (join-paths path file))] (if-let [resp (response (join-paths path file))]
(if index-redirect? (if index-redirect?
(response/redirect (join-paths uri file)) (response/redirect (join-paths uri file))
resp) resp)
(recur files)))))) (recur files)))))))
handler (if path handler (if path
(fn [request] (fn [request]
(let [uri (impl/url-decode (:uri request))] (let [uri (impl/url-decode (:uri request))]
@ -279,13 +281,14 @@
"A ring handler for serving classpath resources, configured via options: "A ring handler for serving classpath resources, configured via options:
| key | description | | key | description |
| -------------------|-------------| | --------------------|-------------|
| :parameter | optional name of the wildcard parameter, defaults to unnamed keyword `:` | :parameter | optional name of the wildcard parameter, defaults to unnamed keyword `:`
| :root | optional resource root, defaults to `\"public\"` | :root | optional resource root, defaults to `\"public\"`
| :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router. | :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router.
| :loader | optional class loader to resolve the resources | :loader | optional class loader to resolve the resources
| :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]` | :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]`
| :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly | :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly
| :canonicalize-uris? | optional boolean: if true (default), try to serve index files for non directory paths (paths that end with slash)
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)" | :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)"
([] ([]
(create-resource-handler nil)) (create-resource-handler nil))
@ -297,13 +300,14 @@
"A ring handler for serving file resources, configured via options: "A ring handler for serving file resources, configured via options:
| key | description | | key | description |
| -------------------|-------------| | --------------------|-------------|
| :parameter | optional name of the wildcard parameter, defaults to unnamed keyword `:` | :parameter | optional name of the wildcard parameter, defaults to unnamed keyword `:`
| :root | optional resource root, defaults to `\"public\"` | :root | optional resource root, defaults to `\"public\"`
| :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router. | :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router.
| :loader | optional class loader to resolve the resources | :loader | optional class loader to resolve the resources
| :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]` | :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]`
| :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly | :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly
| :canonicalize-uris? | optional boolean: if true (default), try to serve index files for non directory paths (paths that end with slash)
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)" | :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)"
([] ([]
(create-file-handler nil)) (create-file-handler nil))

View file

@ -697,7 +697,31 @@
(is (= "<h1>hello</h1>\n" (slurp (:body response))))) (is (= "<h1>hello</h1>\n" (slurp (:body response)))))
(let [response (app (request "/docs/"))] (let [response (app (request "/docs/"))]
(is (= 200 (:status response))) (is (= 200 (:status response)))
(is (= "<h1>hello</h1>\n" (slurp (:body response))))))))))))) (is (= "<h1>hello</h1>\n" (slurp (:body response))))))))
(testing "with canonicalize-uris"
(let [app (ring/ring-handler
(ring/router
["/*" (create {:canonicalize-uris? true})])
(ring/create-default-handler))]
(testing "index-files"
(let [response (app (request "/docs"))]
(is (= (redirect "/docs/index.html") response)))
(let [response (app (request "/docs/"))]
(is (= (redirect "/docs/index.html") response))))))
(testing "without canonicalize-uris"
(let [app (ring/ring-handler
(ring/router
["/*" (create {:canonicalize-uris? false})])
(ring/create-default-handler))]
(testing "index-files"
(let [response (app (request "/docs"))]
(is (= 404 (:status response))))
(let [response (app (request "/docs/"))]
(is (= (redirect "/docs/index.html") response)))))))))))
#?(:clj #?(:clj
(deftest file-resource-handler-not-found-test (deftest file-resource-handler-not-found-test