class Chef::HTTP::BasicClient

Constants

HTTPS

Attributes

http_client[R]
ssl_policy[R]
url[R]

Public Class Methods

new(url, opts={}) click to toggle source

Instantiate a BasicClient.

Arguments:

url

An URI for the remote server.

Options:

#ssl_policy

The SSL Policy to use, defaults to DefaultSSLPolicy

# File lib/chef/http/basic_client.rb, line 43
def initialize(url, opts={})
  @url = url
  @ssl_policy = opts[:ssl_policy] || DefaultSSLPolicy
  @http_client = build_http_client
end

Public Instance Methods

build_http_client() click to toggle source
# File lib/chef/http/basic_client.rb, line 99
def build_http_client
  http_client = http_client_builder.new(host, port)

  if url.scheme == HTTPS
    configure_ssl(http_client)
  end

  http_client.read_timeout = config[:rest_timeout]
  http_client.open_timeout = config[:rest_timeout]
  http_client
end
config() click to toggle source
# File lib/chef/http/basic_client.rb, line 111
def config
  Chef::Config
end
configure_ssl(http_client) click to toggle source
# File lib/chef/http/basic_client.rb, line 127
def configure_ssl(http_client)
  http_client.use_ssl = true
  ssl_policy.apply_to(http_client)
end
host() click to toggle source
# File lib/chef/http/basic_client.rb, line 49
def host
  @url.hostname
end
http_client_builder() click to toggle source
# File lib/chef/http/basic_client.rb, line 115
def http_client_builder
  http_proxy = proxy_uri
  if http_proxy.nil?
    Net::HTTP
  else
    Chef::Log.debug("Using #{http_proxy.host}:#{http_proxy.port} for proxy")
    user = Chef::Config["#{url.scheme}_proxy_user"]
    pass = Chef::Config["#{url.scheme}_proxy_pass"]
    Net::HTTP.Proxy(http_proxy.host, http_proxy.port, user, pass)
  end
end
port() click to toggle source
# File lib/chef/http/basic_client.rb, line 53
def port
  @url.port
end
proxy_uri() click to toggle source

adapted from buildr/lib/buildr/core/transports.rb

# File lib/chef/http/basic_client.rb, line 85
def proxy_uri
  proxy = Chef::Config["#{url.scheme}_proxy"]
  # Check if the proxy string contains a scheme. If not, add the url's scheme to the
  # proxy before parsing. The regex /^.*:\/\// matches, for example, http://.
  proxy = if proxy.match(/^.*:\/\//)
    URI.parse(proxy)
  else
    URI.parse("#{url.scheme}://#{proxy}")
  end if String === proxy
  excludes = Chef::Config[:no_proxy].to_s.split(/\s*,\s*/).compact
  excludes = excludes.map { |exclude| exclude =~ /:\d+$/ ? exclude : "#{exclude}:*" }
  return proxy unless excludes.any? { |exclude| File.fnmatch(exclude, "#{host}:#{port}") }
end
request(method, url, req_body, base_headers={}) { |response| ... } click to toggle source
# File lib/chef/http/basic_client.rb, line 57
def request(method, url, req_body, base_headers={})
  http_request = HTTPRequest.new(method, url, req_body, base_headers).http_request
  Chef::Log.debug("Initiating #{method} to #{url}")
  Chef::Log.debug("---- HTTP Request Header Data: ----")
  base_headers.each do |name, value|
    Chef::Log.debug("#{name}: #{value}")
  end
  Chef::Log.debug("---- End HTTP Request Header Data ----")
  http_client.request(http_request) do |response|
    Chef::Log.debug("---- HTTP Status and Header Data: ----")
    Chef::Log.debug("HTTP #{response.http_version} #{response.code} #{response.msg}")

    response.each do |header, value|
      Chef::Log.debug("#{header}: #{value}")
    end
    Chef::Log.debug("---- End HTTP Status/Header Data ----")

    yield response if block_given?
    # http_client.request may not have the return signature we want, so
    # force the issue:
    return [http_request, response]
  end
rescue OpenSSL::SSL::SSLError => e
  Chef::Log.error("SSL Validation failure connecting to host: #{host} - #{e.message}")
  raise
end