Browse files

Remove legacy support for built-in currying in ring.file, add support…

… for custom mime types in ring.file-info
  • Loading branch information...
1 parent d2a3017 commit e4a74d5175a1619ba547afdafb928aaf5ee69074 @mmcgrana committed Apr 10, 2009
Showing with 41 additions and 29 deletions.
  1. +15 −17 src/ring/file.clj
  2. +17 −12 src/ring/file_info.clj
  3. +9 −0 test/ring/file_info_test.clj
View
32 src/ring/file.clj
@@ -30,20 +30,18 @@
"Wrap an app such that a given directory is checked for a static file
with which to respond to the request, proxying the request to the
wrapped app if such a file does not exist."
- ([dir app]
- (ensure-dir dir)
- (fn [req]
- (if (#{:get :head} (:request-method req))
- (let [uri (url-decode (:uri req))]
- (if (str-includes? ".." uri)
- (forbidden)
- (let [path (cond
- (.endsWith "/" uri) (str uri "index.html")
- (re-match? #"\.[a-z]+$" uri) uri
- :else (str uri ".html"))]
- (if-let [file (maybe-file dir path)]
- (success file)
- (app req)))))
- (app req))))
- ([dir]
- (partial wrap dir)))
+ [dir app]
+ (ensure-dir dir)
+ (fn [req]
+ (if (#{:get :head} (:request-method req))
+ (let [uri (url-decode (:uri req))]
+ (if (str-includes? ".." uri)
+ (forbidden)
+ (let [path (cond
+ (.endsWith "/" uri) (str uri "index.html")
+ (re-match? #"\.[a-z]+$" uri) uri
+ :else (str uri ".html"))]
+ (if-let [file (maybe-file dir path)]
+ (success file)
+ (app req)))))
+ (app req))))
View
29 src/ring/file_info.clj
@@ -3,7 +3,7 @@
(:import org.apache.commons.io.FilenameUtils
java.io.File))
-(defvar- mime-type-map
+(defvar- base-mime-types
{"ai" "application/postscript"
"asc" "text/plain"
"avi" "video/x-msvideo"
@@ -62,19 +62,24 @@
(defn- guess-mime-type
"Returns a String corresponding to the guessed mime type for the given file,
or application/octet-stream if a type cannot be guessed."
- [#^File file]
- (get mime-type-map (FilenameUtils/getExtension (.getPath file))
+ [#^File file mime-types]
+ (get mime-types (FilenameUtils/getExtension (.getPath file))
"application/octet-stream"))
(defn wrap
"Wrap an app such that responses with a file a body will have
corresponding Content-Type and Content-Length headers added if they are not
- allready present and can be determined from the file."
- [app]
- (fn [req]
- (let [{:keys [headers body] :as response} (app req)]
- (if (instance? File body)
- (assoc response :headers
- (assoc headers "Content-Length" (str (.length body))
- "Content-Type" (guess-mime-type body)))
- response))))
+ allready present and can be determined from the file. If two arguments are
+ given the first is taken to be a map of file extensions to content types
+ that will supplement the default, built-in map."
+ ([custom-mime-types app]
+ (let [mime-types (merge base-mime-types custom-mime-types)]
+ (fn [req]
+ (let [{:keys [headers body] :as response} (app req)]
+ (if (instance? File body)
+ (assoc response :headers
+ (assoc headers "Content-Length" (str (.length body))
+ "Content-Type" (guess-mime-type body mime-types)))
+ response)))))
+ ([app]
+ (wrap {} app)))
View
9 test/ring/file_info_test.clj
@@ -10,6 +10,9 @@
(def unknown-file (File. "test/ring/assets/random.xyz"))
(def unknown-file-app (wrap (constantly {:headers {} :body unknown-file})))
+(def custom-type-app (wrap {"txt" "custom/type"}
+ (constantly {:headers {} :body known-file})))
+
(deftest "wrap: non-file response"
(assert= {:headers {} :body "body"} (non-file-app {})))
@@ -24,3 +27,9 @@
{:headers {"Content-Type" "application/octet-stream" "Content-Length" "7"}
:body unknown-file}
(unknown-file-app {})))
+
+(deftest "wrap: custom mime types"
+ (assert=
+ {:headers {"Content-Type" "custom/type" "Content-Length" "6"}
+ :body known-file}
+ (custom-type-app {})))

0 comments on commit e4a74d5

Please sign in to comment.