Permalink
Browse files

Rename redirect-to -> redirect, introduce ring.util.response/response…

… and response augmenters, introduce ring.util.file.
  • Loading branch information...
1 parent 7bcaad4 commit 107924dd58314b4e2918eed15693c1f91c9ef23d @mmcgrana committed Feb 28, 2010
Showing with 57 additions and 37 deletions.
  1. +29 −0 ring-core/src/ring/util/file.clj
  2. +28 −37 ring-core/src/ring/util/response.clj
View
29 ring-core/src/ring/util/file.clj
@@ -0,0 +1,29 @@
+(ns ring.util.file)
+
+(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 ring.util.response/static-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))))
View
65 ring-core/src/ring/util/response.clj
@@ -1,42 +1,22 @@
(ns ring.util.response
- (:import java.io.File))
+ (:require (ring.util [file :as file])))
-(defn redirect-to
- "Returns a Ring response for an HTTP redirect.
- Options:
- :status, defaults to 302"
+; Ring responses
+
+(defn redirect
+ "Returns a Ring response for an HTTP 302 redirect."
[url & [opts]]
- {:status (:status opts 302)
+ {:status 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 response
+ "Returns a skeletal Ring response with the given body, status of 200, and no
+ headers."
+ [body & [opts]]
+ {:status 200
+ :body body
+ :headers {}})
(defn static-file
"Returns a Ring response to serve a static file, or nil if the file does
@@ -45,7 +25,18 @@
: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}))
+ (if-let [file (file/get-file filepath opts)]
+ (response file)))
+
+; Ring response augmenters
+
+(defn status
+ "Returns an updated Ring response with the given status."
+ [resp status]
+ (assoc resp :status status))
+
+(def content-type
+ "Returns an update Ring response with the a Content-Type header corresponding
+ to the given content-type."
+ [resp content-type]
+ (assoc-in resp [:headers "Content-Type"] content-type))

0 comments on commit 107924d

Please sign in to comment.