Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-compressed .gz files support #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
user-agents."
:url "https://github.com/bertrandk/ring-gzip"
:license {:name "MIT-style license (see LICENSE for details)."}
:global-vars {*warn-on-reflection* true}
:profiles {:test {:resource-paths ["test"]}}
:dependencies [[org.clojure/clojure "1.5.1"]])
49 changes: 42 additions & 7 deletions src/ring/middleware/gzip.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
File
PipedInputStream
PipedOutputStream)
(java.net URL)
(java.util.zip GZIPOutputStream)))

(defn- accepts-gzip?
Expand All @@ -15,21 +16,25 @@
;; proxies, av software, buggy browsers, etc...)
(re-seq
#"(gzip\s*,?\s*(gzip|deflate)?|X{4,13}|~{4,13}|\-{4,13})"
accepts)))
(str accepts))))

;; Set Vary to make sure proxies don't deliver the wrong content.
(defn- set-response-headers
(defn- set-encoding-headers
[headers]
(if-let [vary (get headers "vary")]
(-> headers
(assoc "Vary" (str vary ", Accept-Encoding"))
(assoc "Content-Encoding" "gzip")
(dissoc "Content-Length")
(dissoc "vary"))
(-> headers
(assoc "Vary" "Accept-Encoding")
(assoc "Content-Encoding" "gzip")
(dissoc "Content-Length"))))
(assoc "Content-Encoding" "gzip"))))

(defn- set-response-headers
[headers]
(-> headers
(set-encoding-headers)
(dissoc "Content-Length")))

(def ^:private supported-status? #{200, 201, 202, 203, 204, 205 403, 404})

Expand All @@ -56,7 +61,7 @@
(cond
(string? body) (> (count body) min-length)
(seq? body) (> (count body) min-length)
(instance? File body) (> (.length body) min-length)
(instance? File body) (> (.length ^File body) min-length)
:else true)))

(defn- supported-response?
Expand All @@ -77,7 +82,7 @@
(doseq [string body] (io/copy (str string) out))
(io/copy body out)))
(when (instance? Closeable body)
(.close body)))
(.close ^Closeable body)))
p-in))

(defn- gzip-response
Expand All @@ -86,6 +91,12 @@
(update-in [:headers] set-response-headers)
(update-in [:body] compress-body)))

(defn- gzip-static-response
[resp gzfile]
(-> resp
(update-in [:headers] set-encoding-headers)
(assoc :body gzfile)))

(defn wrap-gzip
"Middleware that compresses responses with gzip for supported user-agents."
[handler]
Expand All @@ -96,3 +107,27 @@
(gzip-response resp)
resp))
(handler req))))

(defn wrap-gzip-static
"Middleware that returns pre-compressed files (if available) for supported user-agents.

Inspired by the NGiNX module: http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html

Given a resource or File body
and the same file with .gz appended exists
it will return the .gz one instead."
[handler]
(fn [req]
(let [{body :body :as resp} (handler req)
^File file (if (instance? File body)
body
(if (instance? URL body)
(try (io/file body) (catch IllegalArgumentException e))))]
(if (and file (accepts-gzip? req))
(let [gzfile (File. (str file ".gz"))]
(if (.exists gzfile)
(gzip-static-response resp gzfile)
;else
resp))
;else
resp))))
21 changes: 21 additions & 0 deletions test/ring/middleware/gzip_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,24 @@
resp ((wrap-gzip handler) req)]]
(is (= (get-in resp [:headers "Content-Encoding"])
"gzip")))))

(deftest test-wrap-gzip-static
(testing "Precompressed static resource file response"
(let [handler (constantly {:status 200
:headers {}
:body (io/resource "text.txt")})
req (req :headers {"accept-encoding" "gzip"})
resp ((wrap-gzip-static handler) req)]
(is (= (get-in resp [:headers "Content-Encoding"])
"gzip"))
(is (= (.toURI (:body resp)) (.toURI (io/resource "text.txt.gz"))))))

(testing "Precompressed static file response"
(let [handler (constantly {:status 200
:headers {}
:body (io/file "test/text.txt")})
req (req :headers {"accept-encoding" "gzip"})
resp ((wrap-gzip-static handler) req)]
(is (= (get-in resp [:headers "Content-Encoding"])
"gzip"))
(is (= (:body resp) (io/file "test/text.txt.gz"))))))
1 change: 1 addition & 0 deletions test/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing 123
Binary file added test/text.txt.gz
Binary file not shown.