send_request

in lib/puma/control_cli.rb [138:194]


    def send_request
      uri = URI.parse @control_url

      
      server = case uri.scheme
                when "ssl"
                  require 'openssl'
                  OpenSSL::SSL::SSLSocket.new(
                    TCPSocket.new(uri.host, uri.port),
                    OpenSSL::SSL::SSLContext.new
                  ).tap(&:connect)
                when "tcp"
                  TCPSocket.new uri.host, uri.port
                when "unix"
                  UNIXSocket.new "#{uri.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 << "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 @command == "stats" || @command == "gc-stats"
      end
    ensure
      server.close if server && !server.closed?
    end