Skip to content

Commit b411fc4

Browse files
mknoszligdakrone
authored andcommitted
force conversion to byte array before inflating to avoid ClassCastException.
1 parent 9bf6132 commit b411fc4

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/clj_http/util.clj

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,25 @@
5555
(.close gos)
5656
(.toByteArray baos))))
5757

58+
(defn- force-byte-array
59+
"force b as byte array if it is an InputStream."
60+
[b]
61+
(if (instance? java.io.InputStream b)
62+
(IOUtils/toByteArray b)
63+
b))
64+
5865
(defn inflate
59-
"Returns a zlib inflate'd version of the given byte array."
66+
"Returns a zlib inflate'd version of the given byte array or InputStream."
6067
[b]
61-
(when b
68+
(when-let [data-arr (force-byte-array b)]
6269
(try
63-
(IOUtils/toByteArray (InflaterInputStream. (ByteArrayInputStream. b)))
64-
;; broken inflate implementation server-side,
70+
(IOUtils/toByteArray (InflaterInputStream. (ByteArrayInputStream. data-arr)))
71+
;; broken inflate implementation server-side
6572
;; see [http://stackoverflow.com/questions/3932117/handling-http
6673
;; -contentencoding-deflate]
6774
(catch java.util.zip.ZipException e
6875
(IOUtils/toByteArray
69-
(InflaterInputStream. (ByteArrayInputStream. b)
76+
(InflaterInputStream. (ByteArrayInputStream. data-arr)
7077
(java.util.zip.Inflater. true)))))))
7178

7279
(defn deflate

0 commit comments

Comments
 (0)