class Chef::HTTP::BasicClient

Constants

HTTPS

Attributes

keepalives[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
  @keepalives = opts[:keepalives] || false
end

Public Instance Methods

build_http_client() click to toggle source
# File lib/chef/http/basic_client.rb, line 106
def build_http_client
  # Note: the last nil in the new below forces Net::HTTP to ignore the
  # no_proxy environment variable. This is a workaround for limitations
  # in Net::HTTP use of the no_proxy environment variable. We internally
  # match no_proxy with a fuzzy matcher, rather than letting Net::HTTP
  # do it.
  http_client = http_client_builder.new(host, port, nil)
  http_client.proxy_port = nil if http_client.proxy_address.nil?

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

  http_client.read_timeout = config[:rest_timeout]
  http_client.open_timeout = config[:rest_timeout]
  if keepalives
    http_client.start
  else
    http_client
  end
end
config() click to toggle source
# File lib/chef/http/basic_client.rb, line 128
def config
  Chef::Config
end
configure_ssl(http_client) click to toggle source
# File lib/chef/http/basic_client.rb, line 150
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 53
def host
  @url.hostname
end
http_client() click to toggle source
# File lib/chef/http/basic_client.rb, line 49
def http_client
  @http_client ||= build_http_client
end
http_client_builder() click to toggle source
# File lib/chef/http/basic_client.rb, line 132
def http_client_builder
  if proxy_uri.nil?
    Net::HTTP
  else
    Chef::Log.trace("Using #{proxy_uri.host}:#{proxy_uri.port} for proxy")
    Net::HTTP.Proxy(proxy_uri.host, proxy_uri.port, http_proxy_user(proxy_uri),
                    http_proxy_pass(proxy_uri))
  end
end
http_proxy_pass(proxy_uri) click to toggle source
# File lib/chef/http/basic_client.rb, line 146
def http_proxy_pass(proxy_uri)
  proxy_uri.password || Chef::Config["#{proxy_uri.scheme}_proxy_pass"]
end
http_proxy_user(proxy_uri) click to toggle source
# File lib/chef/http/basic_client.rb, line 142
def http_proxy_user(proxy_uri)
  proxy_uri.user || Chef::Config["#{proxy_uri.scheme}_proxy_user"]
end
port() click to toggle source
# File lib/chef/http/basic_client.rb, line 57
def port
  @url.port
end
proxy_uri() click to toggle source
# File lib/chef/http/basic_client.rb, line 102
def proxy_uri
  @proxy_uri ||= Chef::Config.proxy_uri(url.scheme, host, port)
end
request(method, url, req_body, base_headers = {}) { |response| ... } click to toggle source
# File lib/chef/http/basic_client.rb, line 61
def request(method, url, req_body, base_headers = {})
  http_request = HTTPRequest.new(method, url, req_body, base_headers).http_request
  Chef::Log.trace("Initiating #{method} to #{url}")
  Chef::Log.trace("---- HTTP Request Header Data: ----")
  base_headers.each do |name, value|
    Chef::Log.trace("#{name}: #{value}")
  end
  Chef::Log.trace("---- End HTTP Request Header Data ----")
  http_client.request(http_request) do |response|
    Chef::Log.trace("---- HTTP Status and Header Data: ----")
    Chef::Log.trace("HTTP #{response.http_version} #{response.code} #{response.msg}")

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

    # For non-400's, log the request and response bodies
    if !response.code || !response.code.start_with?("2")
      if response.body
        Chef::Log.trace("---- HTTP Response Body ----")
        Chef::Log.trace(response.body)
        Chef::Log.trace("---- End HTTP Response Body -----")
      end
      if req_body
        Chef::Log.trace("---- HTTP Request Body ----")
        Chef::Log.trace(req_body)
        Chef::Log.trace("---- End HTTP Request Body ----")
      end
    end

    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