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

handle multiple accept headers #6

Open
wants to merge 1 commit 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
11 changes: 8 additions & 3 deletions src/ring/middleware/gzip.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
(dissoc "Content-Length")))
(update-in [:body] piped-gzipped-input-stream)))

(defn- gzip-accept? [accept]
(let [match (re-find #"(gzip|\*)(;q=((0|1)(.\d+)?))?" accept)]
(and match (not (contains? #{"0" "0.0" "0.00" "0.000"}
(match 3))))))

(defn wrap-gzip [handler]
(fn [req]
(let [{:keys [body status] :as resp} (handler req)]
Expand All @@ -71,9 +76,9 @@
(instance? InputStream body)
(instance? File body)))
(let [accepts (get-in req [:headers "accept-encoding"] "")
match (re-find #"(gzip|\*)(;q=((0|1)(.\d+)?))?" accepts)]
(if (and match (not (contains? #{"0" "0.0" "0.00" "0.000"}
(match 3))))
is-gzip (cond (string? accepts) (gzip-accept? accepts)
(coll? accepts) (some gzip-accept? accepts))]
(if is-gzip
(gzipped-response resp)
resp))
resp))))
4 changes: 4 additions & 0 deletions test/ring/middleware/gzip_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"gzip;q=0,deflate" "*;q=0"]]
(is (nil? (encoding (app (accepting ctype)))))))

(deftest test-multiple-accepts
(is (= "gzip" (encoding (app (accepting ["gzip,deflate" "deflate"])))))
(is (nil? (encoding (app (accepting ["deflate" "sdch"]))))))

(deftest test-min-length
"don't compress string bodies less than 200 characters long"
(let [output (apply str (repeat 10 "a"))
Expand Down