mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
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:
parent
89c05932dc
commit
12afe1dcaa
3 changed files with 65 additions and 36 deletions
|
|
@ -54,15 +54,16 @@ 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
|
||||||
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)
|
| :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)
|
||||||
|
|
||||||
|
|
||||||
### TODO
|
### TODO
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
(loop [[file & files] index-files]
|
(when (or canonicalize-uris? (str/ends-with? uri "/"))
|
||||||
(if file
|
(loop [[file & files] index-files]
|
||||||
(if-let [resp (response (join-paths path file))]
|
(if file
|
||||||
(if index-redirect?
|
(if-let [resp (response (join-paths path file))]
|
||||||
(response/redirect (join-paths uri file))
|
(if index-redirect?
|
||||||
resp)
|
(response/redirect (join-paths uri file))
|
||||||
(recur files))))))
|
resp)
|
||||||
|
(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))]
|
||||||
|
|
@ -278,15 +280,16 @@
|
||||||
(defn create-resource-handler
|
(defn create-resource-handler
|
||||||
"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
|
||||||
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)"
|
| :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)"
|
||||||
([]
|
([]
|
||||||
(create-resource-handler nil))
|
(create-resource-handler nil))
|
||||||
([opts]
|
([opts]
|
||||||
|
|
@ -296,15 +299,16 @@
|
||||||
(defn create-file-handler
|
(defn create-file-handler
|
||||||
"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
|
||||||
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)"
|
| :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)"
|
||||||
([]
|
([]
|
||||||
(create-file-handler nil))
|
(create-file-handler nil))
|
||||||
([opts]
|
([opts]
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue