Permalink
Browse files

File and resource middleware prefer :path-info over :uri

  • Loading branch information...
1 parent adcc81a commit 140108705689cb762508bcb2e62c34b5e8a7a2e0 @weavejester weavejester committed Jan 13, 2013
@@ -1,8 +1,9 @@
(ns ring.middleware.file
"Static file serving."
+ (:import java.io.File)
(:require [ring.util.codec :as codec]
- [ring.util.response :as response])
- (:import java.io.File))
+ [ring.util.response :as response]
+ [ring.util.request :as request]))
(defn- ensure-dir
"Ensures that a directory exists at the given path, throwing if one does not."
@@ -24,6 +25,6 @@
(fn [req]
(if-not (= :get (:request-method req))
(app req)
- (let [path (.substring ^String (codec/url-decode (:uri req)) 1)]
+ (let [path (subs (codec/url-decode (request/path-info req)) 1)]
(or (response/file-response path opts)
(app req)))))))
@@ -1,7 +1,8 @@
(ns ring.middleware.resource
"Middleware for serving static resources."
(require [ring.util.codec :as codec]
- [ring.util.response :as response]))
+ [ring.util.response :as response]
+ [ring.util.request :as request]))
(defn wrap-resource
"Middleware that first checks to see whether the request map matches a static
@@ -12,6 +13,6 @@
(fn [request]
(if-not (= :get (:request-method request))
(handler request)
- (let [path (.substring (codec/url-decode (:uri request)) 1)]
+ (let [path (subs (codec/url-decode (request/path-info request)) 1)]
(or (response/resource-response path {:root root-path})
(handler request))))))
@@ -38,3 +38,13 @@
(let [app (wrap-file (constantly :response) public-dir {:index-files? false})
resp (app {:request-method :get :uri "/"})]
(is (= :response resp))))
+
+(deftest test-wrap-file-path-info
+ (let [request {:request-method :get
+ :uri "/bar/foo.html"
+ :context "/bar"
+ :path-info "/foo.html"}
+ {:keys [status headers body]} (app request)]
+ (is (= 200 status))
+ (is (= (into #{} (keys headers)) #{"Content-Length" "Last-Modified"}))
+ (is (= foo-html body))))
@@ -15,4 +15,6 @@
{:request-method :get, :uri "/index.html"} "index"
{:request-method :get, :uri "/bars/foo.html"} "foo"
{:request-method :get, :uri "/handler"} "handler"
- {:request-method :post, :uri "/foo.html"} "handler")))
+ {:request-method :post, :uri "/foo.html"} "handler"
+ {:request-method :get, :uri "/pre/foo.html"
+ :path-info "/foo.html", :context "/pre"} "foo")))

0 comments on commit 1401087

Please sign in to comment.