Class: Chef::HTTP::BasicClient

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/http/basic_client.rb

Constant Summary collapse

HTTPS =
"https".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, ssl_policy: DefaultSSLPolicy, keepalives: false, nethttp_opts: {}) ⇒ BasicClient

Instantiate a BasicClient.

Arguments:

url

An URI for the remote server.

Options:

ssl_policy

The SSL Policy to use, defaults to DefaultSSLPolicy



46
47
48
49
50
51
# File 'lib/chef/http/basic_client.rb', line 46

def initialize(url, ssl_policy: DefaultSSLPolicy, keepalives: false, nethttp_opts: {})
  @url = url
  @ssl_policy = ssl_policy
  @keepalives = keepalives
  @nethttp_opts = ChefUtils::Mash.new(nethttp_opts)
end

Instance Attribute Details

#keepalivesObject (readonly)

Returns the value of attribute keepalives.



38
39
40
# File 'lib/chef/http/basic_client.rb', line 38

def keepalives
  @keepalives
end

#nethttp_optsObject (readonly)

Returns the value of attribute nethttp_opts.



39
40
41
# File 'lib/chef/http/basic_client.rb', line 39

def nethttp_opts
  @nethttp_opts
end

#ssl_policyObject (readonly)

Returns the value of attribute ssl_policy.



37
38
39
# File 'lib/chef/http/basic_client.rb', line 37

def ssl_policy
  @ssl_policy
end

#urlObject (readonly)

Returns the value of attribute url.



36
37
38
# File 'lib/chef/http/basic_client.rb', line 36

def url
  @url
end

Instance Method Details

#build_http_clientObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/http/basic_client.rb', line 110

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

  opts = nethttp_opts.dup
  opts["read_timeout"] ||= config[:rest_timeout]
  opts["open_timeout"] ||= config[:rest_timeout]

  opts.each do |key, value|
    http_client.send(:"#{key}=", value)
  end

  if keepalives
    http_client.start
  else
    http_client
  end
end

#configObject



138
139
140
# File 'lib/chef/http/basic_client.rb', line 138

def config
  Chef::Config
end

#configure_ssl(http_client) ⇒ Object



160
161
162
163
# File 'lib/chef/http/basic_client.rb', line 160

def configure_ssl(http_client)
  http_client.use_ssl = true
  ssl_policy.apply_to(http_client)
end

#hostObject



57
58
59
# File 'lib/chef/http/basic_client.rb', line 57

def host
  @url.hostname
end

#http_clientObject



53
54
55
# File 'lib/chef/http/basic_client.rb', line 53

def http_client
  @http_client ||= build_http_client
end

#http_client_builderObject



142
143
144
145
146
147
148
149
150
# File 'lib/chef/http/basic_client.rb', line 142

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) ⇒ Object



156
157
158
# File 'lib/chef/http/basic_client.rb', line 156

def http_proxy_pass(proxy_uri)
  proxy_uri.password || config["#{proxy_uri.scheme}_proxy_pass"]
end

#http_proxy_user(proxy_uri) ⇒ Object



152
153
154
# File 'lib/chef/http/basic_client.rb', line 152

def http_proxy_user(proxy_uri)
  proxy_uri.user || config["#{proxy_uri.scheme}_proxy_user"]
end

#portObject



61
62
63
# File 'lib/chef/http/basic_client.rb', line 61

def port
  @url.port
end

#proxy_uriObject



106
107
108
# File 'lib/chef/http/basic_client.rb', line 106

def proxy_uri
  @proxy_uri ||= Chef::Config.proxy_uri(url.scheme, host, port)
end

#request(method, url, req_body, base_headers = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/chef/http/basic_client.rb', line 65

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