Permalink
Browse files

Added static-file response to r.u.response

  • Loading branch information...
1 parent 59b0a0e commit 7bcaad40f34aff1039688996c3e765b3957b0ee0 @weavejester weavejester committed with Feb 27, 2010
Showing with 42 additions and 1 deletion.
  1. +42 −1 ring-core/src/ring/util/response.clj
View
43 ring-core/src/ring/util/response.clj
@@ -1,4 +1,5 @@
-(ns ring.util.response)
+(ns ring.util.response
+ (:import java.io.File))
(defn redirect-to
"Returns a Ring response for an HTTP redirect.
@@ -8,3 +9,43 @@
{:status (:status opts 302)
:headers {"Location" url}
:body ""})
+
+(defn- safe-path?
+ "Is a filepath safe for a particular root?"
+ [root path]
+ (.startsWith (.getCanonicalPath (File. root path))
+ (.getCanonicalPath (File. root))))
+
+(defn- find-index-file
+ "Search the directory for an index file."
+ [dir]
+ (first
+ (filter
+ #(.startsWith (.toLowerCase (.getName %)) "index.")
+ (.listFiles dir))))
+
+(defn- get-file
+ "Safely retrieve the correct file. See serve-file for an explanation of
+ options."
+ [path opts]
+ (let [file (if-let [root (:root opts)]
+ (if (safe-path? root path)
+ (File. root path))
+ (File. path))]
+ (if (.exists file)
+ (if (.isDirectory file)
+ (if (:index-files? opts true)
+ (find-index-file file))
+ file))))
+
+(defn static-file
+ "Returns a Ring response to serve a static file, or nil if the file does
+ not exist.
+ Options:
+ :root - take the filepath relative to this root path
+ :index-files? - look for index.* files in directories, defaults to true"
+ [filepath & [opts]]
+ (if-let [file (get-file filepath opts)]
+ {:status 200
+ :headers {}
+ :body file}))

0 comments on commit 7bcaad4

Please sign in to comment.