Skip to content

Commit f6f0871

Browse files
author
Takahiro Hozumi
committed
support RFC1951 version deflate
1 parent b77d30a commit f6f0871

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/clj_http/util.clj

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(:import (java.net URLEncoder))
44
(:import (org.apache.commons.codec.binary Base64))
55
(:import (java.io ByteArrayInputStream ByteArrayOutputStream))
6-
(:import (java.util.zip InflaterInputStream DeflaterInputStream
6+
(:import (java.util.zip Inflater Deflater InflaterInputStream DeflaterInputStream
77
GZIPInputStream GZIPOutputStream))
88
(:import (org.apache.commons.io IOUtils)))
99

@@ -42,11 +42,19 @@
4242
(.toByteArray baos)))
4343

4444
(defn inflate
45-
"Returns a zlib inflate'd version of the given byte array."
45+
"Returns a zlib inflate'd version of the given byte array.
46+
Try to use RFC1950 first, if failed then RFC1951 next."
4647
[b]
47-
(IOUtils/toByteArray (InflaterInputStream. (ByteArrayInputStream. b))))
48+
(try
49+
(IOUtils/toByteArray (InflaterInputStream. (ByteArrayInputStream. b)))
50+
(catch java.util.zip.ZipException e
51+
(IOUtils/toByteArray (InflaterInputStream. (ByteArrayInputStream. b)
52+
(Inflater. true))))))
4853

4954
(defn deflate
5055
"Returns a deflate'd version of the given byte array."
51-
[b]
52-
(IOUtils/toByteArray (DeflaterInputStream. (ByteArrayInputStream. b))))
56+
([b]
57+
(IOUtils/toByteArray (DeflaterInputStream. (ByteArrayInputStream. b))))
58+
([b nowrap]
59+
(IOUtils/toByteArray (DeflaterInputStream. (ByteArrayInputStream. b)
60+
(Deflater. Deflater/DEFAULT_COMPRESSION nowrap)))))

test/clj_http/client_test.clj

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,42 +83,49 @@
8383

8484

8585
(defn compress-handler [req]
86-
(case (get-in req [:headers "accept-encoding"])
87-
"gzip"
86+
(case [(:uri req) (get-in req [:headers "accept-encoding"])]
87+
["/" "gzip"]
8888
{:body (util/gzip (util/utf8-bytes "foofoofoo"))
8989
:headers {"content-encoding" "gzip"}}
90-
"gzip, deflate"
90+
["/" "gzip, deflate"]
9191
{:body (util/gzip (util/utf8-bytes "foofoofoo"))
9292
:headers {"content-encoding" "gzip"}}
93-
"deflate"
93+
["/" "deflate"]
9494
{:body (util/deflate (util/utf8-bytes "barbarbar"))
9595
:headers {"content-encoding" "deflate"}}
96+
["/rfc1951" "deflate"]
97+
{:body (util/deflate (util/utf8-bytes "bazbazbaz") true)
98+
:headers {"content-encoding" "deflate"}}
9699
{:body "foo"}))
97100

98101
(deftest compress-test
99102
(let [client (client/wrap-decompression compress-handler)]
100-
(is (= "foofoofoo" (-> {:headers {"accept-encoding" "gzip"}}
103+
(is (= "foofoofoo" (-> {:uri "/", :headers {"accept-encoding" "gzip"}}
104+
client
105+
:body
106+
util/utf8-string)))
107+
(is (= "barbarbar" (-> {:uri "/", :headers {"accept-encoding" "deflate"}}
101108
client
102109
:body
103110
util/utf8-string)))
104-
(is (= "barbarbar" (-> {:headers {"accept-encoding" "deflate"}}
111+
(is (= "bazbazbaz" (-> {:uri "/rfc1951", :headers {"accept-encoding" "deflate"}}
105112
client
106113
:body
107114
util/utf8-string)))
108115
(is (= "foo" (-> {} client :body)))))
109116

110117
(deftest coerce-compression
111118
(let [client (client/wrap-coerce-compression compress-handler)]
112-
(is (= "gzip" (-> {:headers {"accept-encoding" "gzip"}}
119+
(is (= "gzip" (-> {:uri "/", :headers {"accept-encoding" "gzip"}}
113120
client
114121
(get-in [:headers "content-encoding"]))))
115-
(is (= "deflate" (-> {:headers {"accept-encoding" "deflate"}}
122+
(is (= "deflate" (-> {:uri "/", :headers {"accept-encoding" "deflate"}}
116123
client
117124
(get-in [:headers "content-encoding"]))))
118-
(is (= "gzip" (-> {}
125+
(is (= "gzip" (-> {:uri "/"}
119126
client
120127
(get-in [:headers "content-encoding"]))))
121-
(is (nil? (-> {:headers {"accept-encoding" "identity"}}
128+
(is (nil? (-> {:uri "/", :headers {"accept-encoding" "identity"}}
122129
client
123130
(get-in [:headers "content-encoding"]))))))
124131

0 commit comments

Comments
 (0)