Skip to content

Commit

Permalink
Make building hiredis/hiredis-rb with SSL optional
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-grunder committed Oct 17, 2019
1 parent 8665998 commit a9897ec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
30 changes: 15 additions & 15 deletions ext/hiredis_ext/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,24 +286,22 @@ static VALUE connection_connect_unix(int argc, VALUE *argv, VALUE self) {
return connection_generic_connect(self,c,arg_timeout);
}

static VALUE connection_secure(int argc, VALUE *argv, VALUE self) {
static VALUE connection_secure(VALUE self, VALUE capath, VALUE certpath,
VALUE keypath, VALUE servername)
{
redisParentContext *pc;
Data_Get_Struct(self,redisParentContext,pc);

if (argc == 4) {
Data_Get_Struct(self,redisParentContext,pc);
if (pc->context && !pc->context->err) {
if (redisSecureConnection(pc->context, StringValuePtr(argv[0]), StringValuePtr(argv[1]),
StringValuePtr(argv[2]), StringValuePtr(argv[3])) != REDIS_OK)
{
rb_raise(rb_eRuntimeError,"can't initialize ssl (%s)", pc->context->errstr);
}
} else if (!pc->context) {
rb_raise(rb_eRuntimeError, "%s", "not connected");
} else {
rb_raise(rb_eRuntimeError, "connection in error state (%s)", pc->context->errstr);
if (pc->context && !pc->context->err) {
if (redisSecureConnection(pc->context, StringValuePtr(capath), StringValuePtr(certpath),
StringValuePtr(keypath), StringValuePtr(servername)) != REDIS_OK)
{
rb_raise(rb_eRuntimeError,"can't initialize ssl (%s)", pc->context->errstr);
}
} else if (!pc->context) {
rb_raise(rb_eRuntimeError, "%s", "not connected");
} else {
rb_raise(rb_eArgError, "invalid number of arguments");
rb_raise(rb_eRuntimeError, "connection in error state (%s)", pc->context->errstr);
}

return Qnil;
Expand Down Expand Up @@ -525,8 +523,10 @@ void InitConnection(VALUE mod) {
klass_connection = rb_define_class_under(mod, "Connection", rb_cObject);
rb_global_variable(&klass_connection);
rb_define_alloc_func(klass_connection, connection_parent_context_alloc);
#ifdef USE_SSL
rb_define_method(klass_connection, "connect", connection_connect, -1);
rb_define_method(klass_connection, "secure", connection_secure, -1);
#endif
rb_define_method(klass_connection, "secure", connection_secure, 4);
rb_define_method(klass_connection, "connect_unix", connection_connect_unix, -1);
rb_define_method(klass_connection, "connected?", connection_is_connected, 0);
rb_define_method(klass_connection, "disconnect", connection_disconnect, 0);
Expand Down
28 changes: 18 additions & 10 deletions ext/hiredis_ext/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
require 'mkmf'

with_ssl = ENV['USE_SSL'] == '1' || false

build_hiredis = true
unless have_header('sys/socket.h')
puts "Could not find <sys/socket.h> (Likely Windows)."
build_hiredis = false
end

unless have_library('crypto')
puts "Can't find libcrypto. Install it to build with hiredis"
build_hiredis = false
end
# Don't need libcrypto or libssl if we're not building hiredis SSL support
if with_ssl
unless have_library('crypto')
puts "Can't find libcrypto. Install it to build with hiredis"
build_hiredis = false
end

unless have_library('ssl')
puts "Can't find libssl. Install it to build with hiredis"
puts
unless have_library('ssl')
puts "Can't find libssl. Install it to build with hiredis"
puts
end
end

if build_hiredis == false
Expand All @@ -40,15 +45,18 @@
end

if build_hiredis
ssl_make_arg = with_ssl ? 'USE_SSL=1' : ''
ssl_link_arg = with_ssl ? "#{hiredis_dir}/libhiredis_ssl.a -lssl -lcrypto" : ''

# Make sure hiredis is built...
Dir.chdir(hiredis_dir) do
success = system("#{make_program} USE_SSL=1 static")
success = system("#{make_program} #{ssl_make_arg} static")
raise "Building hiredis failed" if !success
end

# Statically link to hiredis (mkmf can't do this for us)
$CFLAGS << " -I#{hiredis_dir}"
$LDFLAGS << " #{hiredis_dir}/libhiredis.a #{hiredis_dir}/libhiredis_ssl.a -lssl -lcrypto"
$CFLAGS << " -I#{hiredis_dir} " << (with_ssl ? " -DUSE_SSL" : '')
$LDFLAGS << " #{hiredis_dir}/libhiredis.a #{ssl_link_arg}"

have_func("rb_thread_fd_select")
create_makefile('hiredis/ext/hiredis_ext')
Expand Down

0 comments on commit a9897ec

Please sign in to comment.