Skip to content

Commit 734660f

Browse files
authored
Merge pull request #587 from tatut/master
Fix NPE crash in "slurp" middleware
2 parents cfbfa3e + 7cd7ec6 commit 734660f

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

src/cider/nrepl/middleware/content_type.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
(with-open [bos (ByteArrayOutputStream.)]
109109
(merge response
110110
(when (ImageIO/write ^Image value "png" ^OutputStream bos)
111-
(slurp-reply ["image/png" {}] (.toByteArray bos)))))
111+
(slurp-reply "" ["image/png" {}] (.toByteArray bos)))))
112112

113113
:else response))
114114

src/cider/nrepl/middleware/slurp.clj

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565

6666
(defn get-file-content-type [^Path p]
6767
(or (get known-content-types (split-last (.toString p) "."))
68-
(Files/probeContentType p)))
68+
(Files/probeContentType p)
69+
"application/octet-stream"))
6970

7071
;; FIXME (arrdem 2018-04-11):
7172
;; Remove this if-class when we have jdk1.8 min
@@ -74,15 +75,23 @@
7475
(if-class java.util.Base64
7576
(.encodeToString (Base64/getEncoder) buff)))
7677

77-
(defn slurp-reply [content-type buff]
78+
(defn slurp-reply [location content-type buff]
7879
(let [^String real-type (first content-type)
80+
binary? (= "application/octet-stream" real-type)
7981
text? (.contains real-type "text")]
80-
(if-not text?
82+
(cond
83+
binary?
8184
{:content-type content-type
82-
:content-transfer-encoding "base64"
83-
:body (base64-bytes buff)}
85+
:body (str "#binary[location=" location ",size=" (count buff) "]")}
86+
87+
text?
88+
{:content-type content-type
89+
:body (String. buff "utf-8")}
90+
91+
:default
8492
{:content-type content-type
85-
:body (String. buff "utf-8")})))
93+
:content-transfer-encoding "base64"
94+
:body (base64-bytes buff)})))
8695

8796
(defn slurp-url-to-content+body
8897
"Attempts to parse and then to slurp a URL, producing a content-typed response."
@@ -91,10 +100,9 @@
91100
(catch MalformedURLException e nil))]
92101
(if (= (.getProtocol url) "file") ;; expected common case
93102
(let [^Path p (Paths/get (.toURI url))
94-
content-type (normalize-content-type
95-
(get-file-content-type p))
103+
content-type (normalize-content-type (get-file-content-type p))
96104
buff (Files/readAllBytes p)]
97-
(slurp-reply content-type buff))
105+
(slurp-reply p content-type buff))
98106

99107
;; It's not a file, so just try to open it on up
100108
(let [conn (.openConnection url)
@@ -109,7 +117,7 @@
109117
(when (<= 0 b)
110118
(.write os b)
111119
(recur))))
112-
(slurp-reply content-type (.toByteArray os))))))
120+
(slurp-reply url content-type (.toByteArray os))))))
113121

114122
;; FIXME (arrdem 2018-04-11):
115123
;; Remove this if-class when we have jdk1.8 min

test/clj/cider/nrepl/middleware/slurp_test.clj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
(:require
33
[cider.nrepl.middleware.slurp :refer [if-class slurp-url-to-content+body]]
44
[clojure.java.io :as io]
5-
[clojure.test :as t]))
5+
[clojure.test :as t]
6+
[clojure.string :as str]))
67

78
;; FIXME (arrdem 2018-04-11):
89
;; Remove these if-classes when we have jdk1.8 min
@@ -22,3 +23,11 @@
2223
(io/resource "sum-types-are-cool.jpg")))]
2324
(t/is (= ["image/jpeg" {}] (:content-type resp)))
2425
(t/is (= "base64" (:content-transfer-encoding resp))))))
26+
27+
(if-class java.util.Base64
28+
(t/deftest test-unrecognized-file
29+
(let [resp (slurp-url-to-content+body
30+
(.toString (io/resource "unknown.bin")))]
31+
(t/is (= ["application/octet-stream" {}] (:content-type resp)))
32+
(t/is (str/starts-with? (:body resp) "#binary[location="))
33+
(t/is (str/ends-with? (:body resp) ",size=3]")))))

test/resources/unknown.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo

0 commit comments

Comments
 (0)