Skip to content

Commit 13329bd

Browse files
committed
Add optional map syntax for :files and :resources
Fixes #27.
1 parent 808637b commit 13329bd

File tree

3 files changed

+77
-17
lines changed

3 files changed

+77
-17
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ The following configuration keys are supported:
123123
Set to true to add CSRF protection via the [ring-anti-forgery][5]
124124
library.
125125

126-
- `:content-type-options` -
126+
- `:contenta-type-options` -
127127
Prevents attacks based around media-type confusion. See:
128128
[wrap-content-type-options][6].
129129

@@ -156,14 +156,17 @@ The following configuration keys are supported:
156156
A map of options to configure how to find static content.
157157

158158
- `:files` -
159-
A string or collection of strings containing paths to directories
160-
to serve files from. Usually the `:resources` option below is
159+
A string or a map of options to be passed to the [file][14]
160+
middleware, where the `:root` key is passed as the first argument,
161+
and the rest of the map is passed as options. May also be a
162+
collection of the above. Usually the `:resources` option below is
161163
more useful.
162164

163165
- `:resources` -
164-
A string or collection of strings containing classpath
165-
prefixes. This will serve any resources in locations starting with
166-
the supplied prefix.
166+
A string or a map of options to be passed to the [resource][15]
167+
middleware, where the `:root` key is passed as the first argument,
168+
and the rest of the map is passed as options. May also be a
169+
collection of the above.
167170

168171

169172
[1]: https://ring-clojure.github.io/ring/ring.middleware.multipart-params.html
@@ -179,7 +182,8 @@ The following configuration keys are supported:
179182
[11]: https://ring-clojure.github.io/ring/ring.middleware.session.html
180183
[12]: https://ring-clojure.github.io/ring/ring.middleware.flash.html
181184
[13]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection
182-
185+
[14]: https://ring-clojure.github.io/ring/ring.middleware.file.html
186+
[15]: https://ring-clojure.github.io/ring/ring.middleware.resource.html
183187

184188
## License
185189

src/ring/middleware/defaults.clj

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@
7272
(middleware handler options)
7373
handler)))
7474

75-
(defn- wrap-multi [handler middleware args]
76-
(wrap handler
77-
(fn [handler args]
78-
(if (coll? args)
79-
(reduce middleware handler args)
80-
(middleware handler args)))
81-
args))
75+
(defn- wrap-static [handler middleware args]
76+
(let [middleware' (fn [handler args]
77+
(if (map? args)
78+
(middleware handler (:root args) (dissoc args :root))
79+
(middleware handler args)))]
80+
(wrap handler
81+
(fn [handler args]
82+
(if (and (coll? args) (not (map? args)))
83+
(reduce middleware' handler args)
84+
(middleware' handler args)))
85+
args)))
8286

8387
(defn- wrap-xss-protection [handler options]
8488
(x/wrap-xss-protection handler (:enable? options true) (dissoc options :enable?)))
@@ -108,8 +112,8 @@
108112
(wrap wrap-params (get-in config [:params :urlencoded] false))
109113
(wrap wrap-cookies (get-in config [:cookies] false))
110114
(wrap wrap-absolute-redirects (get-in config [:responses :absolute-redirects] false))
111-
(wrap-multi wrap-resource (get-in config [:static :resources] false))
112-
(wrap-multi wrap-file (get-in config [:static :files] false))
115+
(wrap-static wrap-resource (get-in config [:static :resources] false))
116+
(wrap-static wrap-file (get-in config [:static :files] false))
113117
(wrap wrap-content-type (get-in config [:responses :content-types] false))
114118
(wrap wrap-default-charset (get-in config [:responses :default-charset] false))
115119
(wrap wrap-not-modified (get-in config [:responses :not-modified-responses] false))

test/ring/middleware/defaults_test.clj

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns ring.middleware.defaults-test
22
(:require [clojure.test :refer :all]
33
[ring.middleware.defaults :refer :all]
4-
[ring.util.response :refer [response content-type]]
4+
[ring.util.response :refer [response content-type not-found]]
55
[ring.mock.request :refer [request header]]))
66

77
(deftest test-wrap-defaults
@@ -153,6 +153,32 @@
153153
(is (= (slurp (:body (handler (request :get "/foo.txt")))) "foo2\n"))
154154
(is (= (slurp (:body (handler (request :get "/bar.txt")))) "bar\n"))))
155155

156+
(testing "resource paths with options"
157+
(let [handler (wrap-defaults
158+
(fn [{:keys [uri]}]
159+
(if (= uri "/foo.txt")
160+
(response "foo-custom")
161+
(not-found "")))
162+
(assoc-in site-defaults [:static :resources]
163+
{:root "ring/assets/public2"
164+
:prefer-handler? true}))]
165+
(is (= (:body (handler (request :get "/foo.txt"))) "foo-custom"))
166+
(is (= (slurp (:body (handler (request :get "/bar.txt")))) "bar\n"))))
167+
168+
(testing "multiple resource paths with options"
169+
(let [handler (wrap-defaults
170+
(fn [{:keys [uri]}]
171+
(if (= uri "/bar.txt")
172+
(response "bar-custom")
173+
(not-found "")))
174+
(assoc-in site-defaults [:static :resources]
175+
[{:root "ring/assets/public2"
176+
:prefer-handler? true}
177+
{:root "ring/assets/public1"
178+
:prefer-handler? true}]))]
179+
(is (= (slurp (:body (handler (request :get "/foo.txt")))) "foo2\n"))
180+
(is (= (:body (handler (request :get "/bar.txt"))) "bar-custom"))))
181+
156182
(testing "single file path"
157183
(let [handler (wrap-defaults
158184
(constantly nil)
@@ -170,6 +196,32 @@
170196
(is (= (slurp (:body (handler (request :get "/foo.txt")))) "foo2\n"))
171197
(is (= (slurp (:body (handler (request :get "/bar.txt")))) "bar\n"))))
172198

199+
(testing "file paths with options"
200+
(let [handler (wrap-defaults
201+
(fn [{:keys [uri]}]
202+
(if (= uri "/foo.txt")
203+
(response "foo-custom")
204+
(not-found "")))
205+
(assoc-in site-defaults [:static :files]
206+
{:root "test/ring/assets/public2"
207+
:prefer-handler? true}))]
208+
(is (= (:body (handler (request :get "/foo.txt"))) "foo-custom"))
209+
(is (= (slurp (:body (handler (request :get "/bar.txt")))) "bar\n"))))
210+
211+
(testing "multiple file paths with options"
212+
(let [handler (wrap-defaults
213+
(fn [{:keys [uri]}]
214+
(if (= uri "/bar.txt")
215+
(response "bar-custom")
216+
(not-found "")))
217+
(assoc-in site-defaults [:static :files]
218+
[{:root "test/ring/assets/public2"
219+
:prefer-handler? true}
220+
{:root "test/ring/assets/public1"
221+
:prefer-handler? true}]))]
222+
(is (= (slurp (:body (handler (request :get "/foo.txt")))) "foo2\n"))
223+
(is (= (:body (handler (request :get "/bar.txt"))) "bar-custom"))))
224+
173225
(testing "async handlers"
174226
(let [handler (-> (fn [_ respond _] (respond (response "foo")))
175227
(wrap-defaults api-defaults))

0 commit comments

Comments
 (0)