Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrap-content-type for requests of directories #452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions ring-core/src/ring/middleware/content_type.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
(ns ring.middleware.content-type
"Middleware for automatically adding a content type to response maps."
(:require [ring.util.mime-type :refer [ext-mime-type]]
[ring.util.response :refer [content-type get-header]]))
[ring.util.response :refer [content-type get-header]])
(:import [java.io File]))

(defn- mime-type-from-response-body [response mime-types]
(when (instance? File (:body response))
(ext-mime-type (.getAbsolutePath (:body response)) mime-types)))

(defn content-type-response
"Adds a content-type header to response. See: wrap-content-type."
Expand All @@ -12,8 +17,10 @@
(if response
(if (get-header response "Content-Type")
response
(let [mime-type (ext-mime-type (:uri request) (:mime-types options))]
(content-type response (or mime-type "application/octet-stream")))))))
(let [mime-type (or (ext-mime-type (:uri request) (:mime-types options))
(mime-type-from-response-body response (:mime-types options))
"application/octet-stream")]
(content-type response mime-type))))))

(defn wrap-content-type
"Middleware that adds a content-type header to the response if one is not
Expand Down
15 changes: 14 additions & 1 deletion ring-core/test/ring/middleware/test/content_type.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
(ns ring.middleware.test.content-type
(:require [clojure.test :refer :all]
[ring.middleware.content-type :refer :all]))
[ring.middleware.content-type :refer :all])
(:import [java.io File]))

(def public-dir "test/ring/assets")
(def index-html (File. ^String public-dir "index.html"))

(deftest wrap-content-type-test
(testing "response without content-type"
Expand All @@ -10,6 +14,15 @@
{:headers {"Content-Type" "image/png"}}))
(is (= (handler {:uri "/foo/bar.txt"})
{:headers {"Content-Type" "text/plain"}}))))

(testing "response body is java.io.File"
(let [response {:headers {}
:body index-html}
handler (wrap-content-type (constantly response))]
(is (= (get-in (handler {:uri "/index.html"}) [:headers "Content-Type"])
"text/html"))
(is (= (get-in (handler {:uri "/"}) [:headers "Content-Type"])
"text/html"))))

(testing "response with content-type"
(let [response {:headers {"Content-Type" "application/x-foo"}}
Expand Down