class Excon::Connection

def initialize(params = {})

Options Hash: (**params)
  • :instrumentor_name (String) -- Name prefix for #instrument events. Defaults to 'excon'
  • :instrumentor (Class) -- Responds to #instrument as in ActiveSupport::Notifications
  • :retry_limit (Fixnum) -- Set how many times we'll retry a failed request. (Default 4)
  • :proxy (String) -- Proxy server; e.g. 'http://myproxy.com:8888'
  • :ciphers (String) -- Only use the specified SSL/TLS cipher suites; use OpenSSL cipher spec format e.g. 'HIGH:!aNULL:!3DES' or 'AES256-SHA:DES-CBC3-SHA'
  • :socket (String) -- The path to the unix socket (required for 'unix://' connections)
  • :scheme (String) -- The protocol; 'https' causes OpenSSL to be used
  • :query (Hash) -- Default query; appended to the 'scheme://host:port/path/' in the form of '?key=value'. Will only be used if params[:query] is not supplied to Connection#request
  • :port (Fixnum) -- The port on which to connect, to the destination host
  • :path (String) -- Default path; appears after 'scheme://host:port/'. Only used if params[:path] is not supplied to Connection#request
  • :host (String) -- The destination host's reachable DNS name or IP, in the form of a String
  • :headers (Hash) -- The default headers to supply in a request. Only used if params[:headers] is not supplied to Connection#request
  • :body (String) -- Default text to be sent over a socket. Only used if :body absent in Connection#request params

Parameters:
  • params (Hash) -- One or more optional params
def initialize(params = {})
  validate_params!(:connection, params)
  @data = Excon.defaults.dup
  # merge does not deep-dup, so make sure headers is not the original
  @data[:headers] = @data[:headers].dup
  # the same goes for :middlewares
  @data[:middlewares] = @data[:middlewares].dup
  @data.merge!(params)
  unless @data[:scheme] == UNIX
    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]] }
    unless 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] = setup_proxy(ENV['https_proxy'] || ENV['HTTPS_PROXY'])
      elsif (ENV.has_key?('http_proxy') || ENV.has_key?('HTTP_PROXY'))
        @data[:proxy] = setup_proxy(ENV['http_proxy'] || ENV['HTTP_PROXY'])
      elsif @data.has_key?(:proxy)
        @data[:proxy] = setup_proxy(@data[:proxy])
      end
    end
  end
  if @data[:proxy]
    @data[:headers]['Proxy-Connection'] ||= 'Keep-Alive'
    # https credentials happen in handshake
    if @data[:scheme] == 'http' && (@data[:proxy][:user] || @data[:proxy][:password])
      user, pass = URI.decode_www_form_component(@data[:proxy][:user].to_s), URI.decode_www_form_component(@data[:proxy][:password].to_s)
      auth = ['' << user.to_s << ':' << pass.to_s].pack('m').delete(Excon::CR_NL)
      @data[:headers]['Proxy-Authorization'] = 'Basic ' << auth
    end
  end
  if ENV.has_key?('EXCON_DEBUG') || ENV.has_key?('EXCON_STANDARD_INSTRUMENTOR')
    @data[:instrumentor] = Excon::StandardInstrumentor
  end
  # Use Basic Auth if url contains a login
  if @data[:user] || @data[:password]
    user, pass = URI.decode_www_form_component(@data[:user].to_s), URI.decode_www_form_component(@data[:password].to_s)
    @data[:headers]['Authorization'] ||= 'Basic ' << ['' << user.to_s << ':' << pass.to_s].pack('m').delete(Excon::CR_NL)
  end
  @socket_key = '' << @data[:scheme]
  if @data[:scheme] == UNIX
    if @data[:host]
      raise ArgumentError, "The `:host` parameter should not be set for `unix://` connections.\n" +
                           "When supplying a `unix://` URI, it should start with `unix:/` or `unix:///`."
    elsif !@data[:socket]
      raise ArgumentError, 'You must provide a `:socket` for `unix://` connections'
    else
      @socket_key << '://' << @data[:socket]
    end
  else
    @socket_key << '://' << @data[:host]<< ':' << @data[:port].to_s
  end
  reset
end