class Puma::ControlCLI

def send_request

def send_request
  uri = URI.parse @control_url
  host = uri.host
  # create server object by scheme
  server =
    case uri.scheme
    when 'ssl'
      require 'openssl'
      host = host[1..-2] if host&.start_with? '['
      OpenSSL::SSL::SSLSocket.new(
        TCPSocket.new(host, uri.port),
        OpenSSL::SSL::SSLContext.new)
        .tap { |ssl| ssl.sync_close = true }  # default is false
        .tap(&:connect)
    when 'tcp'
      host = host[1..-2] if host&.start_with? '['
      TCPSocket.new host, uri.port
    when 'unix'
      # check for abstract UNIXSocket
      UNIXSocket.new(@control_url.start_with?('unix://@') ?
        "\0#{host}#{uri.path}" : "#{host}#{uri.path}")
    else
      raise "Invalid scheme: #{uri.scheme}"
    end
  if @command == 'status'
    message 'Puma is started'
  else
    url = "/#{@command}"
    if @control_auth_token
      url = url + "?token=#{@control_auth_token}"
    end
    server.syswrite "GET #{url} HTTP/1.0\r\n\r\n"
    unless data = server.read
      raise 'Server closed connection before responding'
    end
    response = data.split("\r\n")
    if response.empty?
      raise "Server sent empty response"
    end
    @http, @code, @message = response.first.split(' ',3)
    if @code == '403'
      raise 'Unauthorized access to server (wrong auth token)'
    elsif @code == '404'
      raise "Command error: #{response.last}"
    elsif @code != '200'
      raise "Bad response from server: #{@code}"
    end
    message "Command #{@command} sent success"
    message response.last if PRINTABLE_COMMANDS.include?(@command)
  end
ensure
  if server
    if uri.scheme == 'ssl'
      server.sysclose
    else
      server.close unless server.closed?
    end
  end
end