mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 08:01:10 +00:00
Add mime-types option to static handler
This commit is contained in:
parent
91fa60324f
commit
e16c95c5b3
4 changed files with 30 additions and 7 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -15,3 +15,4 @@ figwheel_server.log
|
||||||
/.idea
|
/.idea
|
||||||
.clj-kondo
|
.clj-kondo
|
||||||
.shadow-cljs
|
.shadow-cljs
|
||||||
|
.cache
|
||||||
|
|
|
||||||
3
dev-resources/public/site.webmanifest
Normal file
3
dev-resources/public/site.webmanifest
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"name": "Example"
|
||||||
|
}
|
||||||
|
|
@ -227,7 +227,7 @@
|
||||||
;; 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? canonicalize-uris? paths not-found-handler]
|
[response-fn {:keys [parameter root path loader allow-symlinks? index-files index-redirect? canonicalize-uris? paths not-found-handler mime-types]
|
||||||
:or {parameter (keyword "")
|
:or {parameter (keyword "")
|
||||||
root "public"
|
root "public"
|
||||||
index-files ["index.html"]
|
index-files ["index.html"]
|
||||||
|
|
@ -250,9 +250,11 @@
|
||||||
join-paths (fn [& paths]
|
join-paths (fn [& paths]
|
||||||
(str/replace (str/replace (str/join "/" paths) #"([/]+)" "/") #"/$" ""))
|
(str/replace (str/replace (str/join "/" paths) #"([/]+)" "/") #"/$" ""))
|
||||||
response (fn [path]
|
response (fn [path]
|
||||||
(if-let [response (or (paths (join-paths "/" path))
|
(when-let [response (or (paths (join-paths "/" path))
|
||||||
(response-fn path options))]
|
(response-fn path options))]
|
||||||
(response/content-type response (mime-type/ext-mime-type path))))
|
(if-let [content-type (mime-type/ext-mime-type path mime-types)]
|
||||||
|
(response/content-type response content-type)
|
||||||
|
response)))
|
||||||
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 "/"))
|
(when (or canonicalize-uris? (str/ends-with? uri "/"))
|
||||||
|
|
@ -295,7 +297,8 @@
|
||||||
| :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 false), redirect to index file, if false serve it directly
|
| :index-redirect? | optional boolean: if true (default false), 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)
|
| :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)
|
||||||
|
| :mime-types | optional map of filename extensions to mime-types that will be used to guess the content type in addition to the ones defined in ring.util.mime-type/default-mime-types"
|
||||||
([]
|
([]
|
||||||
(create-resource-handler nil))
|
(create-resource-handler nil))
|
||||||
([opts]
|
([opts]
|
||||||
|
|
@ -314,7 +317,8 @@
|
||||||
| :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 false), redirect to index file, if false serve it directly
|
| :index-redirect? | optional boolean: if true (default false), 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)
|
| :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)
|
||||||
|
| :mime-types | optional map of filename extensions to mime-types that will be used to guess the content type in addition to the ones defined in ring.util.mime-type/default-mime-types"
|
||||||
([]
|
([]
|
||||||
(create-file-handler nil))
|
(create-file-handler nil))
|
||||||
([opts]
|
([opts]
|
||||||
|
|
|
||||||
|
|
@ -742,7 +742,22 @@
|
||||||
(let [response (app (request "/docs/"))]
|
(let [response (app (request "/docs/"))]
|
||||||
(is (= (redirect "/docs/index.html") response)))
|
(is (= (redirect "/docs/index.html") response)))
|
||||||
(let [response (app (request "/foobar"))]
|
(let [response (app (request "/foobar"))]
|
||||||
(is (= 404 (:status response))))))))))))
|
(is (= 404 (:status response)))))))
|
||||||
|
|
||||||
|
(testing "with additional mime types"
|
||||||
|
(let [app (ring/ring-handler
|
||||||
|
(ring/router
|
||||||
|
["/*" (create {:mime-types {"webmanifest" "application/manifest+json"}})])
|
||||||
|
(ring/create-default-handler))
|
||||||
|
response (app (request "/site.webmanifest"))]
|
||||||
|
(is (= "application/manifest+json" (get-in response [:headers "Content-Type"])))))
|
||||||
|
(testing "when content type cannot be guessed"
|
||||||
|
(let [app (ring/ring-handler
|
||||||
|
(ring/router
|
||||||
|
["/*" (create nil)])
|
||||||
|
(ring/create-default-handler))
|
||||||
|
response (app (request "/site.webmanifest"))]
|
||||||
|
(is (not (contains? (:headers response) "Content-Type"))))))))))
|
||||||
|
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(deftest file-resource-handler-not-found-test
|
(deftest file-resource-handler-not-found-test
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue