Skip to content

Commit 61980fd

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 68db1eb + d253a4f commit 61980fd

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

README.org

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@
4545
- [[#testimonials][Testimonials]]
4646
- [[#license][License]]
4747

48+
* HELP WANTED
49+
Hi Everyone, I'm in need of help with the 3.0.0 version (the master branch) of
50+
clj-http. In moving to the non-deprecated Apache HTTP client APIs I have broken
51+
a lot of the existing features for clj-http. Since 3.0.0 is basically a rewrite
52+
of =core.clj=, there are many things that still need implementing before 3.0 can
53+
be released. To see what needs fixing run =lein test :all=.
54+
55+
If you have interest, please do send PRs for fixing.
56+
4857
* Introduction
4958

5059
** Overview
@@ -54,7 +63,7 @@ library has taken over from mmcgrana's clj-http.
5463

5564
[[https://secure.travis-ci.org/dakrone/clj-http.png]]
5665

57-
** Philosophy
66+
** Philosophy
5867

5968
The design of =clj-http= is inspired by the [[http://github.com/mmcgrana/ring][Ring]] protocol for Clojure HTTP
6069
server applications.
@@ -160,11 +169,11 @@ Example requests:
160169
{:client-params {"http.protocol.allow-circular-redirects" false
161170
"http.protocol.version" HttpVersion/HTTP_1_0
162171
"http.useragent" "clj-http"}})
163-
172+
164173
;; Set your own cookie policy
165174
(client/post "http://example.com"
166175
{:client-params {:cookie-policy (fn [cookie origin] (your-validation cookie origin))}})
167-
176+
168177
;; Completely ignore cookies:
169178
(client/post "http://example.com"
170179
{:client-params {:cookie-policy (constantly nil)}})
@@ -218,7 +227,7 @@ content encodings.
218227
(client/put "http://example.com/api" {:body "my PUT body"})
219228

220229
#+END_SRC
221-
230+
222231
** POST
223232

224233
#+BEGIN_SRC clojure
@@ -470,9 +479,9 @@ APIs:
470479
:last {:href "https://api.github.com/gists?page=22884"}}
471480
#+END_SRC
472481

473-
** Redirects
482+
** Redirects
474483

475-
clj-http conforms its behaviour regarding automatic redirects to the [[https://tools.ietf.org/html/rfc2616#section-10.3][RFC]].
484+
clj-http conforms its behaviour regarding automatic redirects to the [[https://tools.ietf.org/html/rfc2616#section-10.3][RFC]].
476485

477486
It means that redirects on status =301=, =302= and =307= are not redirected on
478487
methods other than =GET= and =HEAD=. If you want a behaviour closer to what most
@@ -853,7 +862,7 @@ and clj-json can now live together without causing problems.
853862
Persistent connections kept alive by the connection manager become stale: the
854863
target server shuts down the connection on its end without HttpClient being able
855864
to react to that event, while the connection is being idle, thus rendering the
856-
connection half-closed or 'stale'.
865+
connection half-closed or 'stale'.
857866

858867
This can be solved by using (with-connection-pool) as described in the
859868
'Using Persistent Connection' section above.

src/clj_http/conn_mgr.clj

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,29 @@
198198
(defn ^javax.net.ssl.HostnameVerifier hostname-verifier []
199199
(BrowserCompatHostnameVerifier.))
200200

201-
(defn ^org.apache.http.config.Registry registry-builder []
201+
(defn ^org.apache.http.config.Registry insecure-registry-builder []
202202
(-> (RegistryBuilder/create)
203203
(.register "http" PlainConnectionSocketFactory/INSTANCE)
204-
(.register "https" (SSLConnectionSocketFactory.
205-
(ssl-context)
206-
(hostname-verifier)))
204+
(.register "https" insecure-socket-factory)
205+
(.build)))
206+
207+
(defn ^org.apache.http.config.Registry registry-builder [{:keys [insecure?] :as opts}]
208+
(-> (RegistryBuilder/create)
209+
(.register "http" PlainConnectionSocketFactory/INSTANCE)
210+
(.register "https"
211+
(cond
212+
insecure? insecure-socket-factory
213+
:default
214+
(SSLConnectionSocketFactory.
215+
(ssl-context)
216+
(hostname-verifier))))
207217
(.build)))
208218

209219
(defn pooling-conn-mgr []
210-
(PoolingHttpClientConnectionManager. (registry-builder)))
220+
(PoolingHttpClientConnectionManager. (registry-builder {})))
211221

212222
(defn basic-conn-mgr []
213-
(BasicHttpClientConnectionManager. (registry-builder)))
223+
(BasicHttpClientConnectionManager. (registry-builder {})))
214224

215225
(defn reusable? [^HttpClientConnectionManager conn-mgr]
216226
(instance? PoolingHttpClientConnectionManager conn-mgr))

src/clj_http/core.clj

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"Coerce the http-entity from an HttpResponse to a stream that closes itself
128128
and the connection manager when closed."
129129
[^HttpEntity http-entity ^HttpClientConnectionManager conn-mgr ^CloseableHttpResponse response]
130-
(when http-entity
130+
(if http-entity
131131
(proxy [FilterInputStream]
132132
[^InputStream (.getContent http-entity)]
133133
(close []
@@ -138,7 +138,9 @@
138138
(finally
139139
(.close response)
140140
(when-not (conn/reusable? conn-mgr)
141-
(.shutdown conn-mgr))))))))
141+
(.shutdown conn-mgr))))))
142+
(when-not (conn/reusable? conn-mgr)
143+
(.shutdown conn-mgr))))
142144

143145
(defn- print-debug!
144146
"Print out debugging information to *out* for a given request."
@@ -217,15 +219,20 @@
217219
(.addHeader http-req header-n header-vth))
218220
(.addHeader http-req header-n (str header-v))))
219221
(when (opt req :debug) (print-debug! req http-req))
220-
(let [^CloseableHttpResponse response (.execute client http-req context)
221-
^HttpEntity entity (.getEntity response)
222-
status (.getStatusLine response)]
223-
{:body (coerce-body-entity entity conn-mgr response)
224-
:headers (parse-headers
225-
(.headerIterator response)
226-
(opt req :use-header-maps-in-response))
227-
:length (if (nil? entity) 0 (.getContentLength entity))
228-
:chunked? (if (nil? entity) false (.isChunked entity))
229-
:repeatable? (if (nil? entity) false (.isRepeatable entity))
230-
:streaming? (if (nil? entity) false (.isStreaming entity))
231-
:status (.getStatusCode status)})))
222+
(try
223+
(let [^CloseableHttpResponse response (.execute client http-req context)
224+
^HttpEntity entity (.getEntity response)
225+
status (.getStatusLine response)]
226+
{:body (coerce-body-entity entity conn-mgr response)
227+
:headers (parse-headers
228+
(.headerIterator response)
229+
(opt req :use-header-maps-in-response))
230+
:length (if (nil? entity) 0 (.getContentLength entity))
231+
:chunked? (if (nil? entity) false (.isChunked entity))
232+
:repeatable? (if (nil? entity) false (.isRepeatable entity))
233+
:streaming? (if (nil? entity) false (.isStreaming entity))
234+
:status (.getStatusCode status)})
235+
(catch Throwable t
236+
(when-not (conn/reusable? conn-mgr)
237+
(.shutdown conn-mgr))
238+
(throw t)))))

test/clj_http/test/conn_mgr_test.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
:as :stream})
7979
(is false "request should have thrown an exception")
8080
(catch Exception e))
81-
(is @shutdown? "Connection manager has been shut down")))
81+
(is @shutdown? "Connection manager has been shutdown")))
8282

8383
(deftest ^:integration t-closed-conn-mgr-for-empty-body
8484
(run-server)
@@ -87,9 +87,9 @@
8787
(shutdown []
8888
(reset! shutdown? true)))
8989
response (core/request {:request-method :get :uri "/unmodified-resource"
90-
:server-port 18080 :scheme :http
91-
:server-name "localhost"
92-
:connection-manager cm })]
90+
:server-port 18080 :scheme :http
91+
:server-name "localhost"
92+
:connection-manager cm})]
9393
(is (nil? (:body response)) "response shouldn't have body")
9494
(is (= 304 (:status response)))
95-
(is @shutdown? "connection manager should be shut downed")))
95+
(is @shutdown? "connection manager should be shutdown")))

0 commit comments

Comments
 (0)