Skip to content

Instantly share code, notes, and snippets.

@lrenn
Created July 29, 2009 04:45
Show Gist options
  • Save lrenn/157875 to your computer and use it in GitHub Desktop.
Save lrenn/157875 to your computer and use it in GitHub Desktop.
; Could use http://github.com/mmcgrana/ring/blob/24fe773f62026b95e275468f6875812f87989508/src/ring/file_info.clj
(def *default-mimetypes*
{"xml" "text/xml"})
; Being kind of lazy here :)
(defn- extension
[s]
(nth (re-find #"\.(.*$)" s) 1 ""))
; Note, this won't work on routes that return a string since we already default
; Content-Type to text/html elsewhere and with-headers won't overwrite a header that
; already exists. If we're going to use something like this, we should remove that.
(defn with-mimetypes
"Middleware to add the proper content type based on the uri of the request."
([handler]
(with-mimetypes handler *default-mimetypes*))
([handler mimetypes]
(fn [request]
(let [uri (:uri request)
type (get mimetypes (extension uri) "text/html")
headers {"Content-Type" type}]
((with-headers handler headers) request)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment