Skip to content

Commit fa5057e

Browse files
camsauldanielcompton
authored andcommitted
Add async Ring middleware support
1 parent 3c270e6 commit fa5057e

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

src/ring/middleware/gzip.clj

+28-17
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,31 @@
6060
(dissoc "Content-Length")))
6161
(update-in [:body] piped-gzipped-input-stream)))
6262

63-
(defn wrap-gzip [handler]
64-
(fn [req]
65-
(let [{:keys [body status] :as resp} (handler req)]
66-
(if (and (= status 200)
67-
(not (get-in resp [:headers "Content-Encoding"]))
68-
(or
69-
(and (string? body) (> (count body) 200))
70-
(and (seq? body) @flushable-gzip?)
71-
(instance? InputStream body)
72-
(instance? File body)))
73-
(let [accepts (get-in req [:headers "accept-encoding"] "")
74-
match (re-find #"(gzip|\*)(;q=((0|1)(.\d+)?))?" accepts)]
75-
(if (and match (not (contains? #{"0" "0.0" "0.00" "0.000"}
76-
(match 3))))
77-
(gzipped-response resp)
78-
resp))
79-
resp))))
63+
(defn- gzip-response [req {:keys [body status] :as resp}]
64+
(if (and (= status 200)
65+
(not (get-in resp [:headers "Content-Encoding"]))
66+
(or
67+
(and (string? body) (> (count body) 200))
68+
(and (seq? body) @flushable-gzip?)
69+
(instance? InputStream body)
70+
(instance? File body)))
71+
(let [accepts (get-in req [:headers "accept-encoding"] "")
72+
match (re-find #"(gzip|\*)(;q=((0|1)(.\d+)?))?" accepts)]
73+
(if (and match (not (contains? #{"0" "0.0" "0.00" "0.000"}
74+
(match 3))))
75+
(gzipped-response resp)
76+
resp))
77+
resp))
78+
79+
(defn wrap-gzip
80+
"Ring middleware that GZIPs response if client can handle it."
81+
[handler]
82+
(fn
83+
([request]
84+
(gzip-response request (handler request)))
85+
([request respond raise]
86+
(handler
87+
request
88+
(fn [response]
89+
(respond (gzip-response request response)))
90+
raise))))

test/ring/middleware/gzip_test.clj

+13-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
(is (= "gzip" (encoding resp)))
3636
(is (Arrays/equals (unzip (resp :body)) (.getBytes output)))))
3737

38+
(deftest test-basic-gzip-async
39+
"middleware should work with 3-arg async handlers as well"
40+
(let [app (wrap-gzip
41+
(fn [request respond raise]
42+
(respond {:status 200
43+
:body output
44+
:headers {}})))
45+
resp (app (accepting "gzip") identity identity)]
46+
(is (= 200 (:status resp)))
47+
(is (= "gzip" (encoding resp)))
48+
(is (Arrays/equals (unzip (resp :body)) (.getBytes output)))))
49+
3850
(deftest test-inputstream-gzip
3951
(let [app (wrap-gzip (fn [req] {:status 200
4052
:body (StringBufferInputStream. output)
@@ -57,7 +69,7 @@
5769
(println "Running on JDK7+, testing gzipping of seq response bodies.")
5870
(is (= "gzip" (encoding resp)))
5971
(is (Arrays/equals (unzip (resp :body)) (.getBytes output))))
60-
(do
72+
(do
6173
(println "Running on <=JDK6, testing non-gzipping of seq response bodies.")
6274
(is (nil? (encoding resp)))
6375
(is (= seq-body (resp :body)))))))

0 commit comments

Comments
 (0)