Skip to content

Commit

Permalink
Improve OpenSSL::SSL::Socket.
Browse files Browse the repository at this point in the history
  • Loading branch information
636f7374 committed Mar 17, 2022
1 parent 8213b34 commit 98c0379
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/dns/address/https.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract struct DNS::Address

begin
tls_socket = OpenSSL::SSL::Socket::Client.new socket, context: tls_context, sync_close: true, hostname: tls.try &.hostname
tls_socket.close_after_finalize = true
tls_socket.read_buffering = false
tls_socket.ssl_context = tls_context
tls_socket.sync = true
Expand Down
1 change: 1 addition & 0 deletions src/dns/address/tls.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ abstract struct DNS::Address

begin
tls_socket = OpenSSL::SSL::Socket::Client.new socket, context: tls_context, sync_close: true, hostname: tls.try &.hostname
tls_socket.close_after_finalize = true
tls_socket.read_buffering = false
tls_socket.ssl_context = tls_context
tls_socket.sync = true
Expand Down
23 changes: 18 additions & 5 deletions src/dns/extra/openssl/ssl/socket.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class OpenSSL::SSL::Socket
getter closeAfterFinalize : Bool
getter freed : Bool
getter freeMutex : Mutex

protected def initialize(io, context : Context, @sync_close : Bool = false)
@closeAfterFinalize = false
@freed = false
@freeMutex = Mutex.new :unchecked

Expand All @@ -29,12 +31,20 @@ class OpenSSL::SSL::Socket
@sslContext = value
end

def ssl_context : Context?
def ssl_context? : Context?
@sslContext
end

def close_after_finalize=(value : Bool)
@closeAfterFinalize = value
end

def close_after_finalize? : Bool
@closeAfterFinalize
end

private def free_ssl_context : Bool
ssl_context.try &.free
ssl_context?.try &.free
@sslContext = nil

true
Expand Down Expand Up @@ -97,10 +107,13 @@ class OpenSSL::SSL::Socket
exception = ex
end

LibSSL.ssl_free @ssl
free_ssl_context
if close_after_finalize?
LibSSL.ssl_free @ssl
free_ssl_context

@freed = true
end

@freed = true
exception.try { |_exception| raise _exception }
end
end
Expand Down

0 comments on commit 98c0379

Please sign in to comment.