/
client.clj
1024 lines (890 loc) · 35.5 KB
/
client.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns clj-http.client
"Batteries-included HTTP client."
(:require [clj-http.conn-mgr :as conn]
[clj-http.cookies :refer [wrap-cookies]]
[clj-http.core :as core]
[clj-http.headers :refer [wrap-header-map]]
[clj-http.links :refer [wrap-links]]
[clj-http.util :refer [opt] :as util]
[clojure.stacktrace :refer [root-cause]]
[clojure.string :as str]
[clojure.walk :refer [keywordize-keys prewalk]]
[slingshot.slingshot :refer [throw+]])
(:import (java.io InputStream File ByteArrayOutputStream ByteArrayInputStream)
(java.net URL UnknownHostException)
(org.apache.http.entity BufferedHttpEntity ByteArrayEntity
InputStreamEntity FileEntity StringEntity)
(org.apache.http.impl.conn PoolingHttpClientConnectionManager))
(:refer-clojure :exclude [get update]))
;; Cheshire is an optional dependency, so we check for it at compile time.
(def json-enabled?
(try
(require 'cheshire.core)
true
(catch Throwable _ false)))
;; Crouton is an optional dependency, so we check for it at compile time.
(def crouton-enabled?
(try
(require 'crouton.html)
true
(catch Throwable _ false)))
;; tools.reader is an optional dependency, so check at compile time.
(def edn-enabled?
(try
(require 'clojure.tools.reader.edn)
true
(catch Throwable _ false)))
;; Transit is an optional dependency, so check at compile time.
(def transit-enabled?
(try
(require 'cognitect.transit)
true
(catch Throwable _ false)))
;; ring-codec is an optional dependency, so we check for it at compile time.
(def ring-codec-enabled?
(try
(require 'ring.util.codec)
true
(catch Throwable _ false)))
(defn ^:dynamic parse-edn
"Resolve and apply tool.reader's EDN parsing."
[& args]
{:pre [edn-enabled?]}
(apply (ns-resolve (symbol "clojure.tools.reader.edn")
(symbol "read-string"))
{:readers @(or (resolve '*data-readers*) (atom {}))} args))
(defn ^:dynamic parse-html
"Resolve and apply crouton's HTML parsing."
[& args]
{:pre [crouton-enabled?]}
(apply (ns-resolve (symbol "crouton.html") (symbol "parse")) args))
(defn- transit-opts-by-type
"Return the Transit options by type."
[opts type class-name]
{:pre [transit-enabled?]}
(cond
(empty? opts)
opts
(contains? opts type)
(clojure.core/get opts type)
:else
(let [class (Class/forName class-name)]
(println "Deprecated use of :transit-opts found.")
(update-in opts [:handlers]
(fn [handlers]
(->> handlers
(filter #(instance? class (second %)))
(into {})))))))
(defn- transit-read-opts
"Return the Transit read options."
[opts]
{:pre [transit-enabled?]}
(transit-opts-by-type opts :decode "com.cognitect.transit.ReadHandler"))
(defn- transit-write-opts
"Return the Transit write options."
[opts]
{:pre [transit-enabled?]}
(transit-opts-by-type opts :encode "com.cognitect.transit.WriteHandler"))
(defn ^:dynamic parse-transit
"Resolve and apply Transit's JSON/MessagePack decoding."
[in type & [opts]]
{:pre [transit-enabled?]}
(let [reader (ns-resolve 'cognitect.transit 'reader)
read (ns-resolve 'cognitect.transit 'read)]
(read (reader in type (transit-read-opts opts)))))
(defn ^:dynamic transit-encode
"Resolve and apply Transit's JSON/MessagePack encoding."
[out type & [opts]]
{:pre [transit-enabled?]}
(let [output (ByteArrayOutputStream.)
writer (ns-resolve 'cognitect.transit 'writer)
write (ns-resolve 'cognitect.transit 'write)]
(write (writer output type (transit-write-opts opts)) out)
(.toByteArray output)))
(defn ^:dynamic json-encode
"Resolve and apply cheshire's json encoding dynamically."
[& args]
{:pre [json-enabled?]}
(apply (ns-resolve (symbol "cheshire.core") (symbol "encode")) args))
(defn ^:dynamic json-decode
"Resolve and apply cheshire's json decoding dynamically."
[& args]
{:pre [json-enabled?]}
(apply (ns-resolve (symbol "cheshire.core") (symbol "decode")) args))
(defn ^:dynamic json-decode-strict
"Resolve and apply cheshire's json decoding dynamically (with lazy parsing
disabled)."
[& args]
{:pre [json-enabled?]}
(apply (ns-resolve (symbol "cheshire.core") (symbol "decode-strict")) args))
(defn ^:dynamic form-decode
"Resolve and apply ring-codec's form decoding dynamically."
[& args]
{:pre [ring-codec-enabled?]}
(apply (ns-resolve (symbol "ring.util.codec") (symbol "form-decode")) args))
(defn update [m k f & args]
(assoc m k (apply f (m k) args)))
(defn when-pos [v]
(when (and v (pos? v)) v))
(defn dissoc-in
"Dissociates an entry from a nested associative structure returning a new
nested structure. keys is a sequence of keys. Any empty maps that result
will not be present in the new structure."
[m [k & ks :as keys]]
(if ks
(if-let [nextmap (clojure.core/get m k)]
(let [newmap (dissoc-in nextmap ks)]
(if (seq newmap)
(assoc m k newmap)
(dissoc m k)))
m)
(dissoc m k)))
(defn url-encode-illegal-characters
"Takes a raw url path or query and url-encodes any illegal characters.
Minimizes ambiguity by encoding space to %20."
[path-or-query]
(when path-or-query
(-> path-or-query
(str/replace " " "%20")
(str/replace #"[^a-zA-Z0-9\.\-\_\~\!\$\&\'\(\)\*\+\,\;\=\:\@\/\%\?]"
util/url-encode))))
(defn parse-url
"Parse a URL string into a map of interesting parts."
[url]
(let [url-parsed (URL. url)]
{:scheme (keyword (.getProtocol url-parsed))
:server-name (.getHost url-parsed)
:server-port (when-pos (.getPort url-parsed))
:uri (url-encode-illegal-characters (.getPath url-parsed))
:user-info (if-let [user-info (.getUserInfo url-parsed)]
(util/url-decode user-info))
:query-string (url-encode-illegal-characters (.getQuery url-parsed))}))
;; Statuses for which clj-http will not throw an exception
(def unexceptional-status?
#{200 201 202 203 204 205 206 207 300 301 302 303 304 307})
;; helper methods to determine realm of a response
(defn success?
[{:keys [status]}]
(<= 200 status 299))
(defn missing?
[{:keys [status]}]
(= status 404))
(defn conflict?
[{:keys [status]}]
(= status 409))
(defn redirect?
[{:keys [status]}]
(<= 300 status 399))
(defn client-error?
[{:keys [status]}]
(<= 400 status 499))
(defn server-error?
[{:keys [status]}]
(<= 500 status 599))
(defn wrap-exceptions
"Middleware that throws a slingshot exception if the response is not a
regular response. If :throw-entire-message? is set to true, the entire
response is used as the message, instead of just the status number."
[client]
(fn [req]
(let [{:keys [status] :as resp} (client req)]
(if (unexceptional-status? status)
resp
(if (false? (opt req :throw-exceptions))
resp
(if (opt req :throw-entire-message)
(throw+ resp "clj-http: status %d %s" (:status %) resp)
(throw+ resp "clj-http: status %s" (:status %))))))))
(declare wrap-redirects)
(defn follow-redirect
"Attempts to follow the redirects from the \"location\" header, if no such
header exists (bad server!), returns the response without following the
request."
[client {:keys [uri url scheme server-name server-port] :as req}
{:keys [trace-redirects ^InputStream body] :as resp}]
(let [url (or url (str (name scheme) "://" server-name
(when server-port (str ":" server-port)) uri))]
(if-let [raw-redirect (get-in resp [:headers "location"])]
(let [redirect (str (URL. (URL. url) raw-redirect))]
(try (.close body) (catch Exception _))
((wrap-redirects client) (-> req
(merge (parse-url redirect))
(dissoc :query-params)
(assoc :url redirect)
(assoc :trace-redirects trace-redirects))))
;; Oh well, we tried, but if no location is set, return the response
resp)))
(defn wrap-redirects
"Middleware that follows redirects in the response. A slingshot exception is
thrown if too many redirects occur. Options
:follow-redirects - default:true, whether to follow redirects
:max-redirects - default:20, maximum number of redirects to follow
:force-redirects - default:false, force redirecting methods to GET requests
In the response:
:redirects-count - number of redirects
:trace-redirects - vector of sites the request was redirected from"
[client]
(fn [{:keys [request-method max-redirects redirects-count trace-redirects url]
:or {redirects-count 1 trace-redirects []
;; max-redirects default taken from Firefox
max-redirects 20}
:as req}]
(let [{:keys [status] :as resp} (client req)
resp-r (assoc resp :trace-redirects
(if url
(conj trace-redirects url)
trace-redirects))]
(cond
(false? (opt req :follow-redirects))
resp
(not (redirect? resp-r))
resp-r
(and max-redirects (> redirects-count max-redirects))
(if (opt req :throw-exceptions)
(throw+ resp-r "Too many redirects: %s" redirects-count)
resp-r)
(= 303 status)
(follow-redirect client (assoc req :request-method :get
:redirects-count (inc redirects-count))
resp-r)
(#{301 302} status)
(cond
(#{:get :head} request-method)
(follow-redirect client (assoc req :redirects-count
(inc redirects-count)) resp-r)
(opt req :force-redirects)
(follow-redirect client (assoc req
:request-method :get
:redirects-count (inc redirects-count))
resp-r)
:else
resp-r)
(= 307 status)
(if (or (#{:get :head} request-method)
(opt req :force-redirects))
(follow-redirect client (assoc req :redirects-count
(inc redirects-count)) resp-r)
resp-r)
:else
resp-r))))
;; Multimethods for Content-Encoding dispatch automatically
;; decompressing response bodies
(defmulti decompress-body
(fn [resp] (get-in resp [:headers "content-encoding"])))
(defmethod decompress-body "gzip"
[resp]
(-> resp
(update :body util/gunzip)
(assoc :orig-content-encoding (get-in resp [:headers "content-encoding"]))
(dissoc-in [:headers "content-encoding"])))
(defmethod decompress-body "deflate"
[resp]
(-> resp
(update :body util/inflate)
(assoc :orig-content-encoding (get-in resp [:headers "content-encoding"]))
(dissoc-in [:headers "content-encoding"])))
(defmethod decompress-body :default [resp]
(assoc resp
:orig-content-encoding
(get-in resp [:headers "content-encoding"])))
(defn wrap-decompression
"Middleware handling automatic decompression of responses from web servers. If
:decompress-body is set to false, does not automatically set `Accept-Encoding`
header or decompress body."
[client]
(fn [req]
(if (false? (opt req :decompress-body))
(client req)
(let [req-c (update req :headers assoc "accept-encoding" "gzip, deflate")
resp-c (client req-c)]
(decompress-body resp-c)))))
;; Multimethods for coercing body type to the :as key
(defmulti coerce-response-body (fn [req _] (:as req)))
(defmethod coerce-response-body :byte-array [_ resp]
(assoc resp :body (util/force-byte-array (:body resp))))
(defmethod coerce-response-body :stream [_ resp]
(let [body (:body resp)]
(cond (instance? InputStream body) resp
;; This shouldn't happen, but we plan for it anyway
(instance? (Class/forName "[B") body)
(assoc resp :body (ByteArrayInputStream. body)))))
(defn coerce-json-body
[{:keys [coerce]} {:keys [body status] :as resp} keyword? strict? & [charset]]
(let [^String charset (or charset (-> resp :content-type-params :charset)
"UTF-8")
body (util/force-byte-array body)
decode-func (if strict? json-decode-strict json-decode)]
(if json-enabled?
(cond
(= coerce :always)
(assoc resp :body (decode-func (String. ^"[B" body charset) keyword?))
(and (unexceptional-status? status)
(or (nil? coerce) (= coerce :unexceptional)))
(assoc resp :body (decode-func (String. ^"[B" body charset) keyword?))
(and (not (unexceptional-status? status)) (= coerce :exceptional))
(assoc resp :body (decode-func (String. ^"[B" body charset) keyword?))
:else (assoc resp :body (String. ^"[B" body charset)))
(assoc resp :body (String. ^"[B" body charset)))))
(defn coerce-clojure-body
[request {:keys [body] :as resp}]
(let [^String charset (or (-> resp :content-type-params :charset) "UTF-8")
body (util/force-byte-array body)]
(assoc resp :body (cond
(empty? body) nil
edn-enabled? (parse-edn (String. ^"[B" body charset))
:else (binding [*read-eval* false]
(read-string (String. ^"[B" body charset)))))))
(defn coerce-transit-body
[{:keys [transit-opts] :as request} {:keys [body] :as resp} type]
(if transit-enabled?
(assoc resp :body (parse-transit body type transit-opts))
resp))
(defn coerce-form-urlencoded-body
[request {:keys [body] :as resp}]
(let [^String charset (or (-> resp :content-type-params :charset) "UTF-8")
body-bytes (util/force-byte-array body)]
(if ring-codec-enabled?
(assoc resp :body (-> (String. ^"[B" body-bytes charset)
form-decode keywordize-keys))
(assoc resp :body (String. ^"[B" body-bytes charset)))))
(defmulti coerce-content-type (fn [req resp] (:content-type resp)))
(defmethod coerce-content-type :application/clojure [req resp]
(coerce-clojure-body req resp))
(defmethod coerce-content-type :application/edn [req resp]
(coerce-clojure-body req resp))
(defmethod coerce-content-type :application/json [req resp]
(coerce-json-body req resp true false))
(defmethod coerce-content-type :application/transit+json [req resp]
(coerce-transit-body req resp :json))
(defmethod coerce-content-type :application/transit+msgpack [req resp]
(coerce-transit-body req resp :msgpack))
(defmethod coerce-content-type :application/x-www-form-urlencoded [req resp]
(coerce-form-urlencoded-body req resp))
(defmethod coerce-content-type :default [req resp]
(if-let [charset (-> resp :content-type-params :charset)]
(coerce-response-body {:as charset} resp)
(coerce-response-body {:as :default} resp)))
(defmethod coerce-response-body :auto [request resp]
(let [header (get-in resp [:headers "content-type"])]
(->> (merge resp (util/parse-content-type header))
(coerce-content-type request))))
(defmethod coerce-response-body :json [req resp]
(coerce-json-body req resp true false))
(defmethod coerce-response-body :json-strict [req resp]
(coerce-json-body req resp true true))
(defmethod coerce-response-body :json-strict-string-keys [req resp]
(coerce-json-body req resp false true))
(defmethod coerce-response-body :json-string-keys [req resp]
(coerce-json-body req resp false false))
(defmethod coerce-response-body :clojure [req resp]
(coerce-clojure-body req resp))
(defmethod coerce-response-body :transit+json [req resp]
(coerce-transit-body req resp :json))
(defmethod coerce-response-body :transit+msgpack [req resp]
(coerce-transit-body req resp :msgpack))
(defmethod coerce-response-body :x-www-form-urlencoded [req resp]
(coerce-form-urlencoded-body req resp))
(defmethod coerce-response-body :default
[{:keys [as]} {:keys [body] :as resp}]
(let [body-bytes (util/force-byte-array body)]
(cond
(string? as) (assoc resp :body (String. ^"[B" body-bytes ^String as))
:else (assoc resp :body (String. ^"[B" body-bytes "UTF-8")))))
(defn wrap-output-coercion
"Middleware converting a response body from a byte-array to a different
object. Defaults to a String if no :as key is specified, the
`coerce-response-body` multimethod may be extended to add
additional coercions."
[client]
(fn [req]
(let [{:keys [body] :as resp} (client req)]
(if body
(coerce-response-body req resp)
resp))))
(defn maybe-wrap-entity
"Wrap an HttpEntity in a BufferedHttpEntity if warranted."
[{:keys [entity-buffering]} entity]
(if (and entity-buffering (not= BufferedHttpEntity (class entity)))
(BufferedHttpEntity. entity)
entity))
(defn wrap-input-coercion
"Middleware coercing the :body of a request from a number of formats into an
Apache Entity. Currently supports Strings, Files, InputStreams
and byte-arrays."
[client]
(fn [{:keys [body body-encoding length]
:or {^String body-encoding "UTF-8"} :as req}]
(if body
(cond
(string? body)
(client (-> req (assoc :body (maybe-wrap-entity
req (StringEntity. ^String body
^String body-encoding))
:character-encoding (or body-encoding
"UTF-8"))))
(instance? File body)
(client (-> req (assoc :body
(maybe-wrap-entity
req (FileEntity. ^File body
^String body-encoding)))))
;; A length of -1 instructs HttpClient to use chunked encoding.
(instance? InputStream body)
(client (-> req
(assoc :body
(if length
(InputStreamEntity.
^InputStream body (long length))
(maybe-wrap-entity
req
(InputStreamEntity. ^InputStream body -1))))))
(instance? (Class/forName "[B") body)
(client (-> req (assoc :body (maybe-wrap-entity
req (ByteArrayEntity. body)))))
:else
(client req))
(client req))))
(defn get-headers-from-body
"Given a map of body content, return a map of header-name to header-value."
[body-map]
(let [;; parse out HTML content
h (or (:content body-map)
(:content (first (filter #(= (:tag %) :html) body-map))))
;; parse out <head> tags
heads (:content (first (filter #(= (:tag %) :head) h)))
;; parse out attributes of 'meta' head tags
attrs (map :attrs (filter #(= (:tag %) :meta) heads))
;; parse out the 'http-equiv' meta head tags
http-attrs (filter :http-equiv attrs)
;; parse out HTML5 charset meta tags
html5-charset (filter :charset attrs)
;; convert http-attributes into map of headers (lowercased)
headers (apply merge (map (fn [{:keys [http-equiv content]}]
{(.toLowerCase ^String http-equiv) content})
http-attrs))
;; merge in html5 charset setting
headers (merge headers
(when-let [cs (:charset (first html5-charset))]
{"content-type" (str "text/html; charset=" cs)}))]
headers))
(defn wrap-additional-header-parsing
"Middleware that parses additional http headers from the body of a 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. Will
do nothing if no body is returned, e.g. HEAD requests"
[client]
(fn [req]
(let [resp (client req)]
(if (and (opt req :decode-body-headers)
crouton-enabled?
(:body resp)
(let [^String content-type (get-in resp [:headers "content-type"])]
(or (str/blank? content-type)
(.startsWith content-type "text"))))
(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))
resp))))
(defn content-type-value [type]
(if (keyword? type)
(str "application/" (name type))
type))
(defn wrap-content-type
"Middleware converting a `:content-type <keyword>` option to the formal
application/<name> format and adding it as a header."
[client]
(fn [{:keys [content-type character-encoding] :as req}]
(if content-type
(let [ctv (content-type-value content-type)
ct (if character-encoding
(str ctv "; charset=" character-encoding)
ctv)]
(client (update-in req [:headers] assoc "content-type" ct)))
(client req))))
(defn wrap-accept
"Middleware converting the :accept key in a request to application/<type>"
[client]
(fn [{:keys [accept] :as req}]
(if accept
(client (-> req (dissoc :accept)
(assoc-in [:headers "accept"]
(content-type-value accept))))
(client req))))
(defn accept-encoding-value [accept-encoding]
(str/join ", " (map name accept-encoding)))
(defn wrap-accept-encoding
"Middleware converting the :accept-encoding option to an acceptable
Accept-Encoding header in the request."
[client]
(fn [{:keys [accept-encoding] :as req}]
(if accept-encoding
(client (-> req (dissoc :accept-encoding)
(assoc-in [:headers "accept-encoding"]
(accept-encoding-value accept-encoding))))
(client req))))
(defn detect-charset
"Given a charset header, detect the charset, returns UTF-8 if not found."
[content-type]
(or
(when-let [found (when content-type
(re-find #"(?i)charset\s*=\s*([^\s]+)" content-type))]
(second found))
"UTF-8"))
(defn generate-query-string-with-encoding [params encoding]
(str/join "&"
(mapcat (fn [[k v]]
(if (sequential? v)
(map #(str (util/url-encode (name %1) encoding)
"="
(util/url-encode (str %2) encoding))
(repeat k) v)
[(str (util/url-encode (name k) encoding)
"="
(util/url-encode (str v) encoding))]))
params)))
(defn generate-query-string [params & [content-type]]
(let [encoding (detect-charset content-type)]
(generate-query-string-with-encoding params encoding)))
(defn wrap-query-params
"Middleware converting the :query-params option to a querystring on
the request."
[client]
(fn [{:keys [query-params content-type]
:or {content-type :x-www-form-urlencoded}
:as req}]
(if query-params
(client (-> req (dissoc :query-params)
(update-in [:query-string]
(fn [old-query-string new-query-string]
(if-not (empty? old-query-string)
(str old-query-string "&" new-query-string)
new-query-string))
(generate-query-string
query-params
(content-type-value content-type)))))
(client req))))
(defn basic-auth-value [basic-auth]
(let [basic-auth (if (string? basic-auth)
basic-auth
(str (first basic-auth) ":" (second basic-auth)))]
(str "Basic " (util/base64-encode (util/utf8-bytes basic-auth)))))
(defn wrap-basic-auth
"Middleware converting the :basic-auth option into an Authorization header."
[client]
(fn [req]
(if-let [basic-auth (:basic-auth req)]
(client (-> req (dissoc :basic-auth)
(assoc-in [:headers "authorization"]
(basic-auth-value basic-auth))))
(client req))))
(defn wrap-oauth
"Middleware converting the :oauth-token option into an Authorization header."
[client]
(fn [req]
(if-let [oauth-token (:oauth-token req)]
(client (-> req (dissoc :oauth-token)
(assoc-in [:headers "authorization"]
(str "Bearer " oauth-token))))
(client req))))
(defn parse-user-info [user-info]
(when user-info
(str/split user-info #":")))
(defn wrap-user-info
"Middleware converting the :user-info option into a :basic-auth option"
[client]
(fn [req]
(if-let [[user password] (parse-user-info (:user-info req))]
(client (assoc req :basic-auth [user password]))
(client req))))
(defn wrap-method
"Middleware converting the :method option into the :request-method option"
[client]
(fn [req]
(if-let [m (:method req)]
(client (-> req (dissoc :method)
(assoc :request-method m)))
(client req))))
(defmulti coerce-form-params
(fn [req] (keyword (content-type-value (:content-type req)))))
(defmethod coerce-form-params :application/edn
[{:keys [form-params]}]
(pr-str form-params))
(defn- coerce-transit-form-params [type {:keys [form-params transit-opts]}]
(when-not transit-enabled?
(throw (ex-info (format (str "Can't encode form params as "
"\"application/transit+%s\". "
"Transit dependency not loaded.")
(name type))
{:type :transit-not-loaded
:form-params form-params
:transit-opts transit-opts
:transit-type type})))
(transit-encode form-params type transit-opts))
(defmethod coerce-form-params :application/transit+json [req]
(coerce-transit-form-params :json req))
(defmethod coerce-form-params :application/transit+msgpack [req]
(coerce-transit-form-params :msgpack req))
(defmethod coerce-form-params :application/json
[{:keys [form-params json-opts]}]
(when-not json-enabled?
(throw (ex-info (str "Can't encode form params as \"application/json\". "
"Cheshire dependency not loaded.")
{:type :cheshire-not-loaded
:form-params form-params
:json-opts json-opts})))
(json-encode form-params json-opts))
(defmethod coerce-form-params :default [{:keys [content-type form-params
form-param-encoding]}]
(if form-param-encoding
(generate-query-string-with-encoding form-params form-param-encoding)
(generate-query-string form-params (content-type-value content-type))))
(defn wrap-form-params
"Middleware wrapping the submission or form parameters."
[client]
(fn [{:keys [form-params content-type request-method]
:or {content-type :x-www-form-urlencoded}
:as req}]
(if (and form-params (#{:post :put :patch} request-method))
(client (-> req
(dissoc :form-params)
(assoc :content-type (content-type-value content-type)
:body (coerce-form-params req))))
(client req))))
(defn- nest-kv
[kv]
(if (and (vector? kv)
(or (map? (second kv))
(vector? (second kv))))
(let [[fk m] kv]
(reduce-kv (fn [m sk v]
(assoc m
(str (name fk)
\[ (if (integer? sk) sk (name sk)) \])
v))
{}
m))
kv))
(defn- nest-params
[request param-key]
(if-let [params (request param-key)]
(assoc request param-key (prewalk nest-kv params))
request))
(defn wrap-nested-params
"Middleware wrapping nested parameters for query strings."
[client]
(fn [{:keys [content-type]
:as req}]
(if (or (nil? content-type)
(= content-type :x-www-form-urlencoded))
(client (reduce
nest-params
req
[:query-params :form-params]))
(client req))))
(defn wrap-url
"Middleware wrapping request URL parsing."
[client]
(fn [req]
(if-let [url (:url req)]
(client (-> req (dissoc :url) (merge (parse-url url))))
(client req))))
(defn wrap-unknown-host
"Middleware ignoring unknown hosts when the :ignore-unknown-host? option
is set."
[client]
(fn [req]
(try
(client req)
(catch Exception e
(if (= (type (root-cause e)) UnknownHostException)
(when-not (opt req :ignore-unknown-host)
(throw (root-cause e)))
(throw (root-cause e)))))))
(defn wrap-lower-case-headers
"Middleware lowercasing all headers, as per RFC (case-insensitive) and
Ring spec."
[client]
(let [lower-case-headers
#(if-let [headers (:headers %1)]
(assoc %1 :headers (util/lower-case-keys headers))
%1)]
(fn [req]
(-> (client (lower-case-headers req))
(lower-case-headers)))))
(defn wrap-request-timing
"Middleware that times the request, putting the total time (in milliseconds)
of the request into the :request-time key in the response."
[client]
(fn [req]
(let [start (System/currentTimeMillis)
resp (client req)]
(assoc resp :request-time (- (System/currentTimeMillis) start)))))
(def default-middleware
"The default list of middleware clj-http uses for wrapping requests."
[wrap-request-timing
wrap-header-map
wrap-query-params
wrap-basic-auth
wrap-oauth
wrap-user-info
wrap-url
wrap-redirects
wrap-decompression
wrap-input-coercion
;; put this before output-coercion, so additional charset
;; headers can be used if desired
wrap-additional-header-parsing
wrap-output-coercion
wrap-exceptions
wrap-accept
wrap-accept-encoding
wrap-content-type
wrap-form-params
wrap-nested-params
wrap-method
wrap-cookies
wrap-links
wrap-unknown-host])
(def ^:dynamic
*current-middleware*
"Available at any time to retrieve the middleware being used.
Automatically bound when `with-middleware` is used."
default-middleware)
(defn wrap-request
"Returns a batteries-included HTTP request function corresponding to the given
core client. See default-middleware for the middleware wrappers that are used
by default"
[request]
(reduce (fn wrap-request* [request middleware]
(middleware request))
request
default-middleware))
(def ^:dynamic request
"Executes the HTTP request corresponding to the given map and returns
the response map for corresponding to the resulting HTTP response.
In addition to the standard Ring request keys, the following keys are also
recognized:
* :url
* :method
* :query-params
* :basic-auth
* :content-type
* :accept
* :accept-encoding
* :as
The following additional behaviors are also automatically enabled:
* Exceptions are thrown for status codes other than 200-207, 300-303, or 307
* Gzip and deflate responses are accepted and decompressed
* Input and output bodies are coerced as required and indicated by the :as
option."
(wrap-request #'core/request))
;; Inline function to throw a slightly more readable exception when
;; the URL is nil
(definline check-url! [url]
`(when (nil? ~url)
(throw (IllegalArgumentException. "Host URL cannot be nil"))))
(defn get
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(check-url! url)
(request (merge req {:method :get :url url})))
(defn head
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(check-url! url)
(request (merge req {:method :head :url url})))
(defn post
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(check-url! url)
(request (merge req {:method :post :url url})))
(defn put
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(check-url! url)
(request (merge req {:method :put :url url})))
(defn delete
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(check-url! url)
(request (merge req {:method :delete :url url})))
(defn options
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(check-url! url)
(request (merge req {:method :options :url url})))
(defn copy
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(check-url! url)
(request (merge req {:method :copy :url url})))
(defn move
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(check-url! url)
(request (merge req {:method :move :url url})))
(defn patch
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(check-url! url)
(request (merge req {:method :patch :url url})))
(defmacro with-middleware
"Perform the body of the macro with a custom middleware list.
It is highly recommended to at least include:
clj-http.client/wrap-url
clj-http.client/wrap-method
Unless you really know what you are doing."
[middleware & body]
`(let [m# ~middleware]
(binding [*current-middleware* m#
clj-http.client/request (reduce #(%2 %1)
clj-http.core/request
m#)]
~@body)))
(defmacro with-additional-middleware
"Perform the body of the macro with a list of additional middleware.
The given `middleware-seq' is concatenated to the beginning of the
`*current-middleware*' sequence."
[middleware-seq & body]
`(with-middleware (concat ~middleware-seq *current-middleware*)
~@body))
(defmacro with-connection-pool
"Macro to execute the body using a connection manager. Creates a
PoolingHttpClientConnectionManager to use for all requests within the
body of the expression. An option map is allowed to set options for the
connection manager.
The following options are supported:
:timeout - Time that connections are left open before automatically closing
default: 5
:threads - Maximum number of threads that will be used for connecting
default: 4
:default-per-route - Maximum number of simultaneous connections per host
default: 2
:insecure? - Boolean flag to specify allowing insecure HTTPS connections
default: false