smortex/tls-checker

View on GitHub
lib/tls_checker/line_oriented_socket.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
95%
# frozen_string_literal: true

require 'socket'

module TLSChecker
  class SocketRecvTimeout < RuntimeError
    def message
      'Timeout while receiving message from socket'
    end
  end

  class LineOrientedSocket < TCPSocket
    def gets
      line = ''

      line += timed_getbyte until line.end_with?("\r\n")

      line
    end

    def gets_until_match(pattern)
      loop do
        line = gets
        break if line.match(pattern)
      end
    end

    def puts(data)
      send("#{data}\r\n", 0)
    end

    private

    TIMEOUT = 10

    def timed_getbyte
      recv_nonblock(1)
    rescue IO::EAGAINWaitReadable
      raise SocketRecvTimeout unless IO.select([self], nil, nil, 10)

      recv_nonblock(1)
    end
  end
end