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

Preventing NPE on decode-body-headers when body is nil (e.g. HEAD request) #262

Merged
merged 1 commit into from
Apr 23, 2015
Merged
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
2 changes: 2 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ from clj-http's dependencies without causing any problems like so:
[clj-http "0.6.0" :exclusions [crouton]]])
#+END_SRC

Note also that HEAD requests will not return a body, in which case this setting will have no effect.

clj-http will automatically disable the =:decode-body-headers= option.

** Misc
Expand Down
16 changes: 7 additions & 9 deletions src/clj_http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -488,22 +488,20 @@
web page, adding them into the headers map of the response if any
are found. Only looks at the body if the :decode-body-headers option
is set to a truthy value. Will be silently disabled if crouton is excluded
from clj-http's dependencies."
from clj-http's dependencies. Will do nothing if no body is returned, e.g. HEAD requests"
[client]
(fn [req]
(if (opt req :decode-body-headers)
(if crouton-enabled?
(let [resp (client req)
body-bytes (util/force-byte-array (:body resp))
(let [resp (client req)]
(if (and (opt req :decode-body-headers) crouton-enabled? (:body resp))
(let [body-bytes (util/force-byte-array (:body resp))
body-stream1 (java.io.ByteArrayInputStream. body-bytes)
body-map (parse-html body-stream1)
additional-headers (get-headers-from-body body-map)
body-stream2 (java.io.ByteArrayInputStream. body-bytes)]
(assoc resp
:headers (merge (:headers resp) additional-headers)
:body body-stream2))
(client req))
(client req))))
:headers (merge (:headers resp) additional-headers)
:body body-stream2))
resp))))

(defn content-type-value [type]
(if (keyword? type)
Expand Down
6 changes: 4 additions & 2 deletions test/clj_http/test/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,14 @@
client (fn [req] {:body (.getBytes text)})
new-client (client/wrap-additional-header-parsing client)
resp (new-client {:decode-body-headers true})
resp2 (new-client {:decode-body-headers false})]
resp2 (new-client {:decode-body-headers false})
resp3 ((client/wrap-additional-header-parsing (fn [req] {:body nil})) {:decode-body-headers true})]
(is (= {"content-type" "text/html; charset=Shift_JIS"
"content-style-type" "text/css"
"content-script-type" "text/javascript"}
(:headers resp)))
(is (nil? (:headers resp2)))))
(is (nil? (:headers resp2)))
(is (nil? (:headers resp3)))))

(deftest t-wrap-additional-header-parsing-html5
(let [^String text (slurp (resource "header-html5-test.html"))
Expand Down