Skip to content

Commit

Permalink
[new] [#282] Add support for TLS URIs to URI parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Jul 17, 2023
1 parent 2e6722b commit 19d97eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/taoensso/carmine.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
`spec` describes the connection details, e.g.:
- {:host \"127.0.0.1\" :port 6379} ; Default
- {:uri \"redis://redistogo:[email protected].com:9475/\"}
- {:uri \"redis://username:[email protected].com:9475/3\" ; /3 for db 3
- {:host \"127.0.0.1\"
:port 6379
:ssl-fn :default ; [1]
Expand Down
31 changes: 21 additions & 10 deletions src/taoensso/carmine/connections.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"Handles socket connection lifecycle. Pool is implemented with Apache Commons
pool. Originally adapted from redis-clojure."
{:author "Peter Taoussanis"}
(:require [taoensso.encore :as enc]
[taoensso.carmine.protocol :as protocol])
(:import [java.net InetSocketAddress Socket URI]
[org.apache.commons.pool2 KeyedPooledObjectFactory]
[org.apache.commons.pool2.impl GenericKeyedObjectPool DefaultPooledObject]))
(:require
[clojure.string :as str]
[taoensso.encore :as enc]
[taoensso.carmine.protocol :as protocol])

(:import
[java.net InetSocketAddress Socket URI]
[org.apache.commons.pool2 KeyedPooledObjectFactory]
[org.apache.commons.pool2.impl GenericKeyedObjectPool DefaultPooledObject]))

(enc/declare-remote
taoensso.carmine/ping
Expand Down Expand Up @@ -233,18 +237,25 @@
[username password] (.split (str (.getUserInfo uri)) ":")
port (.getPort uri)
db (when-let [[_ db-str] (re-matches #"/(\d+)$" (.getPath uri))]
(Integer. ^String db-str))]
(Integer. ^String db-str))

ssl-fn
(when-let [scheme (.getScheme uri)]
(when (contains? #{"rediss" "https"} (str/lower-case scheme))
:default))]

(-> {:host (.getHost uri)}
(#(if (pos? port) (assoc % :port port) %))
(#(if (and db (pos? (long db))) (assoc % :db db) %))
(#(if (enc/nempty-str? username) (assoc % :username username) %))
(#(if (enc/nempty-str? password) (assoc % :password password) %))))))
(#(if (enc/nempty-str? password) (assoc % :password password) %))
(#(if ssl-fn (assoc % :ssl-fn ssl-fn) %))))))

(comment
[(parse-uri "redis://user:[email protected]:9475/7")
(parse-uri "redis://:[email protected]:9475/7")
(parse-uri "redis://user:@x.y.com:9475/7")])
[(parse-uri "redis://user:[email protected]:9475/3")
(parse-uri "redis://:[email protected]:9475/3")
(parse-uri "redis://user:@x.y.com:9475/3")
(parse-uri "rediss://user:@x.y.com:9475/3")])

(def conn-spec
(enc/memoize
Expand Down

0 comments on commit 19d97eb

Please sign in to comment.