|
2 | 2 | "Core HTTP request/response implementation."
|
3 | 3 | (:require [clojure.java.io :as io])
|
4 | 4 | (:import (java.io ByteArrayOutputStream InputStream IOException)
|
5 |
| - (java.net URI URL HttpURLConnection))) |
| 5 | + (java.net URL HttpURLConnection) |
| 6 | + (javax.net.ssl HttpsURLConnection SSLContext TrustManager X509TrustManager HostnameVerifier SSLSession) |
| 7 | + (java.security SecureRandom))) |
6 | 8 |
|
7 | 9 | (set! *warn-on-reflection* true)
|
8 | 10 |
|
|
41 | 43 | (.flush baos)
|
42 | 44 | (.toByteArray baos)))))
|
43 | 45 |
|
| 46 | +(defn my-host-verifier [] |
| 47 | + (proxy [HostnameVerifier] [] |
| 48 | + (verify [^String hostname ^SSLSession session] true))) |
| 49 | + |
| 50 | +(defn trust-invalid-manager [] |
| 51 | + "This allows the ssl socket to connect with invalid/self-signed SSL certs." |
| 52 | + (reify X509TrustManager |
| 53 | + (getAcceptedIssuers [this] nil) |
| 54 | + (checkClientTrusted [this certs authType]) |
| 55 | + (checkServerTrusted [this certs authType]))) |
| 56 | + |
44 | 57 | (defn request
|
45 | 58 | "Executes the HTTP request corresponding to the given Ring request map and
|
46 | 59 | returns the Ring response map corresponding to the resulting HTTP response.
|
|
55 | 68 | (when server-port (str ":" server-port))
|
56 | 69 | uri
|
57 | 70 | (when query-string (str "?" query-string)))
|
| 71 | + _ (when insecure? |
| 72 | + (do (HttpsURLConnection/setDefaultSSLSocketFactory |
| 73 | + (.getSocketFactory |
| 74 | + (doto (SSLContext/getInstance "SSL") |
| 75 | + (.init nil (into-array TrustManager [(trust-invalid-manager)]) |
| 76 | + (new SecureRandom))))) |
| 77 | + (HttpsURLConnection/setDefaultHostnameVerifier (my-host-verifier)))) |
58 | 78 | ^HttpURLConnection conn (.openConnection ^URL (URL. http-url))]
|
59 | 79 | (when (and content-type character-encoding)
|
60 | 80 | (.setRequestProperty conn "Content-Type" (str content-type
|
|
0 commit comments