Skip to content

Commit febcad0

Browse files
Merge pull request #12 from gaberger/feature/fix-insecure
Feature/fix insecure
2 parents 7c5f87c + 49849a6 commit febcad0

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### unreleased
2+
Update to support self-signed certificates via insecure? option
3+
14
### 0.4.3
25

36
- **Feature:** Parse link headers from response and put them under `:links` ([#1](https://github.com/martinklepsch/clj-http-lite/pull/1))

Readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ A Clojure HTTP library similar to [clj-http](http://github.com/dakrone/clj-http)
2222
- No proxy-ing DELETEs with body
2323
- No multipart form uploads
2424
- No persistent connection support
25-
- No support for insecure HTTPS connection (yet)
2625
- namespace rename clj-http.* -> clj-http.lite.*
2726

2827
## Usage

deps.edn

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
{:paths ["src"]
2-
:deps {org.clojure/clojure {:mvn/version "1.6.0"}
3-
slingshot {:mvn/version "0.12.1"}}}
2+
:deps {org.clojure/clojure {:mvn/version "1.10.0"}}}

src/clj_http/lite/core.clj

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"Core HTTP request/response implementation."
33
(:require [clojure.java.io :as io])
44
(: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)))
68

79
(set! *warn-on-reflection* true)
810

@@ -41,6 +43,17 @@
4143
(.flush baos)
4244
(.toByteArray baos)))))
4345

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+
4457
(defn request
4558
"Executes the HTTP request corresponding to the given Ring request map and
4659
returns the Ring response map corresponding to the resulting HTTP response.
@@ -55,6 +68,13 @@
5568
(when server-port (str ":" server-port))
5669
uri
5770
(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))))
5878
^HttpURLConnection conn (.openConnection ^URL (URL. http-url))]
5979
(when (and content-type character-encoding)
6080
(.setRequestProperty conn "Content-Type" (str content-type

0 commit comments

Comments
 (0)