|
2 | 2 | "Core HTTP request/response implementation."
|
3 | 3 | (:require [clojure.java.io :as io])
|
4 | 4 | (: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))) |
6 | 7 |
|
7 | 8 | (set! *warn-on-reflection* true)
|
8 | 9 |
|
|
41 | 42 | (.flush baos)
|
42 | 43 | (.toByteArray baos)))))
|
43 | 44 |
|
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)))) |
68 | 49 |
|
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)))))) |
75 | 59 |
|
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)))) |
77 | 66 |
|
78 | 67 | (defn request
|
79 | 68 | "Executes the HTTP request corresponding to the given Ring `req` map and
|
|
0 commit comments