Skip to content

Commit 0219dc9

Browse files
authored
Turf conditional insecure? option support (#50)
Babashka now includes all necessary classes to support the insecure? option. Closes #49
1 parent c7b4f7f commit 0219dc9

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

src/clj_http/lite/core.clj

+21-32
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"Core HTTP request/response implementation."
33
(:require [clojure.java.io :as io])
44
(:import (java.io ByteArrayOutputStream InputStream)
5-
(java.net URL HttpURLConnection)))
5+
(java.net HttpURLConnection URL)
6+
(javax.net.ssl HostnameVerifier HttpsURLConnection SSLContext SSLSession TrustManager X509TrustManager)))
67

78
(set! *warn-on-reflection* true)
89

@@ -41,39 +42,27 @@
4142
(.flush baos)
4243
(.toByteArray baos)))))
4344

44-
(defn- trust-all-ssl!
45-
[_conn]
46-
(throw (ex-info "insecure? option not supported in this environment"
47-
{})))
48-
49-
(defmacro ^:private def-insecure []
50-
(when (try (import '[javax.net.ssl
51-
HttpsURLConnection SSLContext TrustManager X509TrustManager HostnameVerifier SSLSession])
52-
(catch Exception _))
53-
'(do
54-
(def ^:private trust-all-hostname-verifier
55-
(delay
56-
(proxy [HostnameVerifier] []
57-
(verify [^String hostname ^SSLSession session] true))))
58-
59-
(def ^:private trust-all-ssl-socket-factory
60-
(delay
61-
(.getSocketFactory
62-
(doto (SSLContext/getInstance "SSL")
63-
(.init nil (into-array TrustManager [(reify X509TrustManager
64-
(getAcceptedIssuers [this] nil)
65-
(checkClientTrusted [this certs authType])
66-
(checkServerTrusted [this certs authType]))])
67-
(new java.security.SecureRandom))))))
45+
(def ^:private trust-all-hostname-verifier
46+
(delay
47+
(proxy [HostnameVerifier] []
48+
(verify [^String hostname ^SSLSession session] true))))
6849

69-
(defn- trust-all-ssl!
70-
[conn]
71-
(when (instance? HttpsURLConnection conn)
72-
(let [^HttpsURLConnection ssl-conn conn]
73-
(.setHostnameVerifier ssl-conn @trust-all-hostname-verifier)
74-
(.setSSLSocketFactory ssl-conn @trust-all-ssl-socket-factory)))))))
50+
(def ^:private trust-all-ssl-socket-factory
51+
(delay
52+
(.getSocketFactory
53+
(doto (SSLContext/getInstance "SSL")
54+
(.init nil (into-array TrustManager [(reify X509TrustManager
55+
(getAcceptedIssuers [_this] nil)
56+
(checkClientTrusted [_this _certs _authType])
57+
(checkServerTrusted [_this _certs _authType]))])
58+
(new java.security.SecureRandom))))))
7559

76-
(def-insecure)
60+
(defn- trust-all-ssl!
61+
[conn]
62+
(when (instance? HttpsURLConnection conn)
63+
(let [^HttpsURLConnection ssl-conn conn]
64+
(.setHostnameVerifier ssl-conn @trust-all-hostname-verifier)
65+
(.setSSLSocketFactory ssl-conn @trust-all-ssl-socket-factory))))
7766

7867
(defn request
7968
"Executes the HTTP request corresponding to the given Ring `req` map and

0 commit comments

Comments
 (0)