lib/socketry/ssl/server.rb in socketry-0.4.0 vs lib/socketry/ssl/server.rb in socketry-0.5.0

- old
+ new

@@ -10,21 +10,17 @@ # @return [Socketry::SSL::Server] def initialize( hostname_or_port, port = nil, ssl_socket_class: OpenSSL::SSL::SSLSocket, - ssl_context: OpenSSL::SSL::SSLContext.new, ssl_params: nil, **args ) - raise TypeError, "invalid SSL context (#{ssl_context.class})" unless ssl_context.is_a?(OpenSSL::SSL::SSLContext) raise TypeError, "expected Hash, got #{ssl_params.class}" if ssl_params && !ssl_params.is_a?(Hash) @ssl_socket_class = ssl_socket_class - @ssl_context = ssl_context - @ssl_context.set_params(ssl_params) if ssl_params && !ssl_params.empty? - @ssl_context.freeze + @ssl_params = ssl_params super(hostname_or_port, port, **args) end # Accept a connection to the server @@ -34,31 +30,24 @@ # # Multithreaded servers should invoke this method after spawning a thread # to ensure a slow/malicious connection can't cause a denial-of-service # attack against the server. # - # @param timeout [Numeric, NilClass] seconds to wait before aborting the accept + # @param timeout [Numeric, NilClass] (default nil, unlimited) seconds to wait before aborting the accept + # # @return [Socketry::SSL::Socket] def accept(timeout: nil, **args) - ruby_socket = super(timeout: timeout, **args).to_io - ssl_socket = @ssl_socket_class.new(ruby_socket, @ssl_context) + tcp_socket = super(timeout: timeout, **args) - begin - ssl_socket.accept_nonblock - rescue IO::WaitReadable - retry if IO.select([ruby_socket], nil, nil, timeout) - raise Socketry::TimeoutError, "failed to complete handshake after #{timeout} seconds" - rescue IO::WaitWritable - retry if IO.select(nil, [ruby_socket], nil, timeout) - raise Socketry::TimeoutError, "failed to complete handshake after #{timeout} seconds" - end + ssl_socket = Socketry::SSL::Socket.new( + read_timeout: @read_timeout, + write_timeout: @write_timeout, + resolver: @resolver, + ssl_socket_class: @ssl_socket_class, + ssl_params: @ssl_params + ) - Socketry::SSL::Socket.new( - read_timeout: @read_timeout, - write_timeout: @write_timeout, - resolver: @resolver, - socket_class: @socket_class - ).from_socket(ruby_socket) + ssl_socket.accept(tcp_socket, timeout: timeout) end end end end