Skip to content

Commit 66a2190

Browse files
author
Shulang Lei
committed
Re-introduce save-request option in build-response-map
1 parent 67ef32c commit 66a2190

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/clj_http/core.clj

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -321,24 +321,47 @@
321321
(clojure.pprint/pprint (bean http-req)))
322322

323323
(defn- build-response-map
324-
[^HttpResponse response req conn-mgr ^HttpClientContext context]
324+
[^HttpResponse response req ^HttpUriRequest http-req
325+
conn-mgr ^HttpClientContext context]
325326
(let [^HttpEntity entity (.getEntity response)
326327
status (.getStatusLine response)
327-
protocol-version (.getProtocolVersion status)]
328-
{:body (coerce-body-entity entity conn-mgr response)
329-
:headers (parse-headers
330-
(.headerIterator response)
331-
(opt req :use-header-maps-in-response))
332-
:length (if (nil? entity) 0 (.getContentLength entity))
333-
:chunked? (if (nil? entity) false (.isChunked entity))
334-
:repeatable? (if (nil? entity) false (.isRepeatable entity))
335-
:streaming? (if (nil? entity) false (.isStreaming entity))
336-
:status (.getStatusCode status)
337-
:protocol-version {:name (.getProtocol protocol-version)
338-
:major (.getMajor protocol-version)
339-
:minor (.getMinor protocol-version)}
340-
:reason-phrase (.getReasonPhrase status)
341-
:trace-redirects (mapv str (.getRedirectLocations context))}))
328+
protocol-version (.getProtocolVersion status)
329+
body (:body req)
330+
response
331+
{:body (coerce-body-entity entity conn-mgr response)
332+
:headers (parse-headers
333+
(.headerIterator response)
334+
(opt req :use-header-maps-in-response))
335+
:length (if (nil? entity) 0 (.getContentLength entity))
336+
:chunked? (if (nil? entity) false (.isChunked entity))
337+
:repeatable? (if (nil? entity) false (.isRepeatable entity))
338+
:streaming? (if (nil? entity) false (.isStreaming entity))
339+
:status (.getStatusCode status)
340+
:protocol-version {:name (.getProtocol protocol-version)
341+
:major (.getMajor protocol-version)
342+
:minor (.getMinor protocol-version)}
343+
:reason-phrase (.getReasonPhrase status)
344+
:trace-redirects (mapv str (.getRedirectLocations context))}]
345+
(if (opt req :save-request)
346+
(-> response
347+
(assoc :request req)
348+
(assoc-in [:request :body-type] (type body))
349+
(update-in [:request]
350+
#(if (opt req :debug-body)
351+
(assoc % :body-content
352+
(cond
353+
(isa? (type (:body %)) String)
354+
(:body %)
355+
356+
(isa? (type (:body %)) HttpEntity)
357+
(let [baos (ByteArrayOutputStream.)]
358+
(.writeTo ^HttpEntity (:body %) baos)
359+
(.toString baos "UTF-8"))
360+
361+
:else nil))
362+
%))
363+
(assoc-in [:request :http-req] http-req))
364+
response)))
342365

343366
(defn- get-conn-mgr
344367
[async? req]
@@ -418,7 +441,7 @@
418441
client (http-client req conn-mgr http-url proxy-ignore-hosts)]
419442
(try
420443
(build-response-map (.execute client http-req context)
421-
req conn-mgr context)
444+
req http-req conn-mgr context)
422445
(catch Throwable t
423446
(when-not (conn/reusable? conn-mgr)
424447
(conn/shutdown-manager conn-mgr))
@@ -437,7 +460,7 @@
437460
(completed [this resp]
438461
(try
439462
(respond (build-response-map
440-
resp req conn-mgr context))
463+
resp req http-req conn-mgr context))
441464
(catch Throwable t
442465
(when-not (conn/reusable? conn-mgr)
443466
(conn/shutdown-manager conn-mgr))

0 commit comments

Comments
 (0)