Skip to content

Commit 0f44131

Browse files
committed
Handle LoadError from require "openssl"
Not all systems have the OpenSSL extension. This handles the LoadError, and conditionally disables SSL support on such systems.
1 parent 02de89f commit 0f44131

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

lib/redis/connection/ruby.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
require "redis/connection/command_helper"
33
require "redis/errors"
44
require "socket"
5-
require "openssl"
5+
6+
begin
7+
require "openssl"
8+
rescue LoadError
9+
# Not all systems have OpenSSL support
10+
end
611

712
class Redis
813
module Connection
@@ -201,22 +206,24 @@ def self.connect(path, timeout)
201206

202207
end
203208

204-
class SSLSocket < ::OpenSSL::SSL::SSLSocket
205-
include SocketMixin
209+
if defined?(OpenSSL)
210+
class SSLSocket < ::OpenSSL::SSL::SSLSocket
211+
include SocketMixin
206212

207-
def self.connect(host, port, timeout, ssl_params)
208-
# Note: this is using Redis::Connection::TCPSocket
209-
tcp_sock = TCPSocket.connect(host, port, timeout)
213+
def self.connect(host, port, timeout, ssl_params)
214+
# Note: this is using Redis::Connection::TCPSocket
215+
tcp_sock = TCPSocket.connect(host, port, timeout)
210216

211-
ctx = OpenSSL::SSL::SSLContext.new
212-
ctx.set_params(ssl_params) if ssl_params && !ssl_params.empty?
217+
ctx = OpenSSL::SSL::SSLContext.new
218+
ctx.set_params(ssl_params) if ssl_params && !ssl_params.empty?
213219

214-
ssl_sock = new(tcp_sock, ctx)
215-
ssl_sock.hostname = host
216-
ssl_sock.connect
217-
ssl_sock.post_connection_check(host)
220+
ssl_sock = new(tcp_sock, ctx)
221+
ssl_sock.hostname = host
222+
ssl_sock.connect
223+
ssl_sock.post_connection_check(host)
218224

219-
ssl_sock
225+
ssl_sock
226+
end
220227
end
221228
end
222229

0 commit comments

Comments
 (0)