Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions ring-core/src/ring/util/response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
(if (= "file" (.getProtocol url))
(url-as-file url)))

(defn- directory-url? [^java.net.URL url]
(-> (str url) (.endsWith "/")))

(defn url-response
"Return a response for the supplied URL."
[^URL url]
Expand All @@ -185,11 +188,12 @@
(-> (response file)
(file-content-length)
(file-last-modified)))
(let [conn (.openConnection url)]
(if-let [stream (.getInputStream conn)]
(-> (response stream)
(connection-content-length conn)
(connection-last-modified conn))))))
(when-not (directory-url? url)
(let [conn (.openConnection url)]
(if-let [stream (.getInputStream conn)]
(-> (response stream)
(connection-content-length conn)
(connection-last-modified conn)))))))

(defn resource-response
"Returns a Ring response to serve a packaged resource, or nil if the
Expand Down
Binary file added ring-core/test/ring/assets/test-resource.jar
Binary file not shown.
20 changes: 17 additions & 3 deletions ring-core/test/ring/util/test/response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:use clojure.test
ring.util.response)
(:import [java.io File InputStream]
org.apache.commons.io.FileUtils))
org.apache.commons.io.FileUtils)
(:require [clojure.java.io :as io]))

(deftest test-redirect
(is (= {:status 302 :headers {"Location" "http://google.com"} :body ""}
Expand Down Expand Up @@ -72,6 +73,19 @@
(finally
(.setContextClassLoader current-thread# original-loader#)))))

(defn- make-jar-url [jar-path res-path]
(io/as-url (str "jar:file:" jar-path "!/" res-path)))

(deftest test-url-response
(testing "resources from a jar file"
(let [base-path (.getPath (io/resource "ring/assets/test-resource.jar"))
root-url (make-jar-url base-path "public/")
empty-resource (make-jar-url base-path "public/empty-resource")
non-empty-resource (make-jar-url base-path "public/hi-resource")]
(is (nil? (url-response root-url)))
(is (slurp (:body (url-response empty-resource))) "")
(is (slurp (:body (url-response non-empty-resource))) "hi"))))

(deftest test-resource-response
(testing "response map"
(let [resp (resource-response "/ring/assets/foo.html")]
Expand Down Expand Up @@ -122,7 +136,7 @@
;; This test requires the ability to have file names in the source
;; tree with non-ASCII characters in them encoded as UTF-8. That
;; may be platform-specific. Comment out for now.

;; If this fails on Mac OS X, try again with the command line:
;; LC_CTYPE="UTF-8" lein test
(testing "resource is a file with UTF-8 characters in path"
Expand All @@ -149,7 +163,7 @@
(is (= (into #{} (keys (resp :headers))) #{"Content-Length" "Last-Modified"}))
(is (= (get-in resp [:headers "Content-Length"]) "3"))
(is (= (slurp (resp :body)) "foo")))

(is (nil? (file-response "backlink/foo.html" {:root "test/ring/assets/bars"})))))

(deftest test-set-cookie
Expand Down