Permalink
Please sign in to comment.
Browse files
Refactor static file handling to unify ring.middleware.file and ring.…
…util.response/file-response.
- Loading branch information...
Showing
with
46 additions
and 80 deletions.
57
ring-core/src/ring/middleware/file.clj
| @@ -1,51 +1,28 @@ | ||
| (ns ring.middleware.file | ||
| "Static file serving." | ||
| (:use (clojure.contrib [def :only (defvar-)] | ||
| - [except :only (throw-if-not)]) | ||
| - ring.util.response) | ||
| - (:require [ring.util.codec :as codec] | ||
| + [except :only (throw-if-not)])) | ||
| + (:require (ring.util [codec :as codec] [response :as response]) | ||
| [clojure.contrib.java-utils :as ju]) | ||
| (:import java.io.File)) | ||
| -(defn- substring? | ||
| - "Returns true if sub is a substring of string." | ||
| - [sub #^String string] | ||
| - (.contains string sub)) | ||
| - | ||
| (defn- ensure-dir | ||
| - "Ensures that a given directory exists, throwing if it does not." | ||
| - [dir] | ||
| - (let [fdir (ju/file dir)] | ||
| - (throw-if-not (.exists fdir) "Directory does not exist: %s" fdir))) | ||
| - | ||
| -(defvar- forbidden | ||
| - (-> (response "<html><body><h1>403 Forbidden</h1></body></html>") | ||
| - (status 403) | ||
| - (content-type "text/html"))) | ||
| - | ||
| -(defn- maybe-file | ||
| - "Returns the File corresponding to the given relative path within the given | ||
| - dir if it exists, or nil if no such file exists." | ||
| - [dir path] | ||
| - (let [file (ju/file (str dir path))] | ||
| - (and (.exists file) (.canRead file) file))) | ||
| + "Ensures that a directory exists at the given path, throwing if one does not." | ||
| + [#^String dir-path] | ||
| + (let [dir (File. dir-path)] | ||
| + (throw-if-not (.exists dir) "Directory does not exist: %s" dir-path))) | ||
| (defn wrap-file | ||
| - "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 | ||
| + "Wrap an app such that the directory at the given root-path 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." | ||
| - [app dir] | ||
| - (ensure-dir dir) | ||
| + [app #^String root-path] | ||
| + (ensure-dir root-path) | ||
| (fn [req] | ||
| - (if (#{:get :head} (:request-method req)) | ||
| - (let [uri (codec/url-decode (:uri req))] | ||
| - (if (substring? ".." uri) | ||
| - forbidden | ||
| - (let [path (cond | ||
| - (.endsWith "/" uri) (str uri "index.html") | ||
| - (re-find #"\.[a-z]+$" uri) uri | ||
| - :else (str uri ".html"))] | ||
| - (if-let [file (maybe-file dir path)] | ||
| - (response file) | ||
| - (app req))))) | ||
| - (app req)))) | ||
| + (if-not (= :get (:request-method req)) | ||
| + (app req) | ||
| + (let [path (.substring (codec/url-decode (:uri req)) 1)] | ||
| + (or | ||
| + (response/file-response path | ||
| + {:root root-path :index-files? true :html-files? true}) | ||
| + (app req)))))) |
29
ring-core/src/ring/util/response.clj
25
ring-core/test/ring/middleware/file_test.clj
14
ring-core/test/ring/middleware/static_test.clj
1
ring-core/test/ring/util/response_test.clj
0 comments on commit
0949082