class Excon::Connection

def setup_proxy

def setup_proxy
  if @data[:disable_proxy]
    if @data[:proxy]
      raise ArgumentError, "`:disable_proxy` parameter and `:proxy` parameter cannot both be set at the same time."
    end
    return
  end
  unless @data[:scheme] == UNIX
    if no_proxy_env = ENV["no_proxy"] || ENV["NO_PROXY"]
      no_proxy_list = no_proxy_env.scan(/\*?\.?([^\s,:]+)(?::(\d+))?/i).map { |s| [s[0], s[1]] }
    end
    unless no_proxy_env && no_proxy_list.index { |h| /(^|\.)#{h[0]}$/.match(@data[:host]) && (h[1].nil? || h[1].to_i == @data[:port]) }
      if @data[:scheme] == HTTPS && (ENV.has_key?('https_proxy') || ENV.has_key?('HTTPS_PROXY'))
        @data[:proxy] = ENV['https_proxy'] || ENV['HTTPS_PROXY']
      elsif (ENV.has_key?('http_proxy') || ENV.has_key?('HTTP_PROXY'))
        @data[:proxy] = ENV['http_proxy'] || ENV['HTTP_PROXY']
      end
    end
    case @data[:proxy]
    when nil
      @data.delete(:proxy)
    when ''
      @data.delete(:proxy)
    when Hash
      # no processing needed
    when String, URI
      uri = @data[:proxy].is_a?(String) ? URI.parse(@data[:proxy]) : @data[:proxy]
      @data[:proxy] = {
        :host       => uri.host,
        :hostname   => uri.hostname,
        # path is only sensible for a Unix socket proxy
        :path       => uri.scheme == UNIX ? uri.path : nil,
        :port       => uri.port,
        :scheme     => uri.scheme,
      }
      if uri.password
        @data[:proxy][:password] = uri.password
      end
      if uri.user
        @data[:proxy][:user] = uri.user
      end
      if @data[:proxy][:scheme] == UNIX
        if @data[:proxy][:host]
          raise ArgumentError, "The `:host` parameter should not be set for `unix://` proxies.\n" +
                               "When supplying a `unix://` URI, it should start with `unix:/` or `unix:///`."
        end
      else
        unless uri.host && uri.port && uri.scheme
          raise Excon::Errors::ProxyParseError, "Proxy is invalid"
        end
      end
    else
      raise Excon::Errors::ProxyParseError, "Proxy is invalid"
    end
    if @data.has_key?(:proxy) && @data[:scheme] == 'http'
      @data[:headers]['Proxy-Connection'] ||= 'Keep-Alive'
      # https credentials happen in handshake
      if @data[:proxy].has_key?(:user) || @data[:proxy].has_key?(:password)
        user, pass = Utils.unescape_form(@data[:proxy][:user].to_s), Utils.unescape_form(@data[:proxy][:password].to_s)
        auth = ["#{user}:#{pass}"].pack('m').delete(Excon::CR_NL)
        @data[:headers]['Proxy-Authorization'] = 'Basic ' + auth
      end
    end
  end
end