Skip to content

Commit

Permalink
Fix whitespace, indentation, and lein-bikeshed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dakrone committed Mar 29, 2017
1 parent 118e584 commit 72f531f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
14 changes: 10 additions & 4 deletions src/clj_http/conn_mgr.clj
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@
(apply get-keystore* keystore args)))

(defn get-keystore-context-verifier
[{:keys [keystore keystore-type ^String keystore-pass keystore-instance ; Note: JVM strings aren't ideal for passwords - see http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords
;; TODO: use something else for passwords
;; Note: JVM strings aren't ideal for passwords - see
;; https://tinyurl.com/azm3ab9
[{:keys [keystore keystore-type ^String keystore-pass keystore-instance
trust-store trust-store-type trust-store-pass]
:as req}]
(let [ks (get-keystore keystore keystore-type keystore-pass)
Expand Down Expand Up @@ -280,9 +283,12 @@
(defmulti shutdown-manager
"Shut down the given connection manager, if it is not nil"
class)
(defmethod shutdown-manager nil [conn-mgr] nil)
(defmethod shutdown-manager org.apache.http.conn.HttpClientConnectionManager [^HttpClientConnectionManager conn-mgr] (.shutdown conn-mgr))
(defmethod shutdown-manager org.apache.http.nio.conn.NHttpClientConnectionManager [^NHttpClientConnectionManager conn-mgr] (.shutdown conn-mgr))
(defmethod shutdown-manager nil [conn-mgr] nil)
(defmethod shutdown-manager org.apache.http.conn.HttpClientConnectionManager
[^HttpClientConnectionManager conn-mgr] (.shutdown conn-mgr))
(defmethod shutdown-manager
org.apache.http.nio.conn.NHttpClientConnectionManager
[^NHttpClientConnectionManager conn-mgr] (.shutdown conn-mgr))

(def ^:dynamic *connection-manager*
"connection manager to be rebound during request execution"
Expand Down
16 changes: 10 additions & 6 deletions src/clj_http/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
HttpRequestInterceptor HttpResponseInterceptor)
(org.apache.http.auth UsernamePasswordCredentials AuthScope
NTCredentials)
(org.apache.http.client HttpRequestRetryHandler RedirectStrategy CredentialsProvider)
(org.apache.http.client HttpRequestRetryHandler RedirectStrategy
CredentialsProvider)
(org.apache.http.client.config RequestConfig CookieSpecs)
(org.apache.http.client.methods HttpDelete HttpGet HttpPost HttpPut
HttpOptions HttpPatch
Expand Down Expand Up @@ -110,7 +111,8 @@
(when max-redirects (.setMaxRedirects config max-redirects))
(.build config)))

(defmulti ^:private construct-http-host (fn [proxy-host proxy-port] (class proxy-host)))
(defmulti ^:private construct-http-host (fn [proxy-host proxy-port]
(class proxy-host)))
(defmethod construct-http-host String
[^String proxy-host ^Long proxy-port]
(if proxy-port
Expand Down Expand Up @@ -388,10 +390,11 @@
(.addHeader http-req header-n (str header-v))))
(when (opt req :debug) (print-debug! req http-req))
(if-not async?
(let [^CloseableHttpClient client (http-client req conn-mgr http-url
proxy-ignore-hosts)]
(let [^CloseableHttpClient
client (http-client req conn-mgr http-url proxy-ignore-hosts)]
(try
(build-response-map (.execute client http-req context) req conn-mgr context)
(build-response-map (.execute client http-req context)
req conn-mgr context)
(catch Throwable t
(when-not (conn/reusable? conn-mgr)
(conn/shutdown-manager conn-mgr))
Expand All @@ -409,7 +412,8 @@
(raise ex)))
(completed [this resp]
(try
(respond (build-response-map resp req conn-mgr context))
(respond (build-response-map
resp req conn-mgr context))
(catch Throwable t
(when-not (conn/reusable? conn-mgr)
(conn/shutdown-manager conn-mgr))
Expand Down
23 changes: 13 additions & 10 deletions src/clj_http/multipart.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
(type (:content multipart))))))

(defmethod make-multipart-body File
; Create a FileBody object from the given map, requiring at least :content
;; Create a FileBody object from the given map, requiring at least :content
[{:keys [^String name ^String mime-type ^File content ^String encoding]}]
(cond
(and name mime-type content encoding)
Expand All @@ -56,10 +56,10 @@
(throw (Exception. "Multipart file body must contain at least :content"))))

(defmethod make-multipart-body InputStream
; Create an InputStreamBody object from the given map, requiring at least
; :content and :name. If no :length is specified, clj-http will use
; chunked transfer-encoding, if :length is specified, clj-http will
; workaround things be proxying the InputStreamBody to return a length.
;; Create an InputStreamBody object from the given map, requiring at least
;; :content and :name. If no :length is specified, clj-http will use
;; chunked transfer-encoding, if :length is specified, clj-http will
;; workaround things be proxying the InputStreamBody to return a length.
[{:keys [^String name ^String mime-type ^InputStream content length]}]
(cond
(and content name length)
Expand Down Expand Up @@ -97,9 +97,10 @@
"at least :content and :name")))))

(defmulti ^java.nio.charset.Charset encoding-to-charset class)
(defmethod encoding-to-charset nil [encoding] nil)
(defmethod encoding-to-charset nil [encoding] nil)
(defmethod encoding-to-charset java.nio.charset.Charset [encoding] encoding)
(defmethod encoding-to-charset java.lang.String [encoding] (java.nio.charset.Charset/forName encoding))
(defmethod encoding-to-charset java.lang.String [encoding]
(java.nio.charset.Charset/forName encoding))

(defmethod make-multipart-body String
;; Create a StringBody object from the given map, requiring at least :content.
Expand All @@ -108,16 +109,18 @@
[{:keys [^String mime-type ^String content encoding]}]
(cond
(and content mime-type encoding)
(StringBody. content (ContentType/create mime-type (encoding-to-charset encoding)))
(StringBody.
content (ContentType/create mime-type (encoding-to-charset encoding)))

(and content encoding)
(StringBody. content (ContentType/create "text/plain" (encoding-to-charset encoding)))
(StringBody.
content (ContentType/create "text/plain" (encoding-to-charset encoding)))

content
(StringBody. content (ContentType/create "text/plain" Consts/ASCII))))

(defmethod make-multipart-body ContentBody
; Use provided org.apache.http.entity.mime.content.ContentBody directly
;; Use provided org.apache.http.entity.mime.content.ContentBody directly
[{:keys [^ContentBody content]}]
content)

Expand Down
21 changes: 12 additions & 9 deletions test/clj_http/test/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -569,16 +569,19 @@
(client/request {:method :get :url (localhost "/get") :headers {"foo" 2}}))

(deftest ^:integration t-empty-response-coercion
(run-server)
(let [resp (client/get (localhost "/empty") {:as :clojure})]
(is (= (:body resp) nil))))
(run-server)
(let [resp (client/get (localhost "/empty") {:as :clojure})]
(is (= (:body resp) nil))))

(deftest ^:integration t-trace-redirects
(run-server)
(let [resp-with-redirects (client/request {:method :get
:url (localhost "/redirect-to-get")})
resp-without-redirects (client/request {:method :get
:url (localhost "/redirect-to-get")
:follow-redirects false})]
(is (= (:trace-redirects resp-with-redirects) ["http://localhost:18080/get"]))
(let [resp-with-redirects
(client/request {:method :get
:url (localhost "/redirect-to-get")})
resp-without-redirects
(client/request {:method :get
:url (localhost "/redirect-to-get")
:follow-redirects false})]
(is (= (:trace-redirects resp-with-redirects)
["http://localhost:18080/get"]))
(is (= (:trace-redirects resp-without-redirects) []))))

0 comments on commit 72f531f

Please sign in to comment.