summaryrefslogtreecommitdiff
path: root/lib/chef/http
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2013-10-04 17:27:51 -0700
committerdanielsdeleo <dan@opscode.com>2013-10-08 15:01:48 -0700
commit3b169bddbc3ba5c9759e01a6438d70e34d264cf0 (patch)
treeace89ef2cd976409ddaef3390df4fcc33db28f5b /lib/chef/http
parent9a98be5ad08813d532694f95c480b0bcbecd0da6 (diff)
downloadchef-3b169bddbc3ba5c9759e01a6438d70e34d264cf0.tar.gz
HTTP client creates request objects.
Changes the structure of HTTP requests so that we create an HTTP client object and then ask it to make requests. This allows for easier customization of the HTTP client object, in particular enabling use to make use of persistent connections.
Diffstat (limited to 'lib/chef/http')
-rw-r--r--lib/chef/http/basic_client.rb99
-rw-r--r--lib/chef/http/http_request.rb8
2 files changed, 66 insertions, 41 deletions
diff --git a/lib/chef/http/basic_client.rb b/lib/chef/http/basic_client.rb
index af7287a0e1..62f83cfa5c 100644
--- a/lib/chef/http/basic_client.rb
+++ b/lib/chef/http/basic_client.rb
@@ -22,6 +22,7 @@
#
require 'uri'
require 'net/http'
+require 'chef/http/http_request'
class Chef
class HTTP
@@ -30,9 +31,11 @@ class Chef
HTTPS = "https".freeze
attr_reader :url
+ attr_reader :http_client
def initialize(url)
@url = url
+ @http_client = build_http_client
end
def host
@@ -43,6 +46,16 @@ class Chef
@url.port
end
+ def request(method, url, req_body, base_headers={})
+ http_request = HTTPRequest.new(method, url, req_body, base_headers).http_request
+ http_client.request(http_request) do |response|
+ 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
+ end
+
#adapted from buildr/lib/buildr/core/transports.rb
def proxy_uri
proxy = Chef::Config["#{url.scheme}_proxy"]
@@ -52,58 +65,66 @@ class Chef
return proxy unless excludes.any? { |exclude| File.fnmatch(exclude, "#{host}:#{port}") }
end
- def http_client
+ 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
+ end
+
+ def config
+ Chef::Config
+ end
+
+ def http_client_builder
http_proxy = proxy_uri
if http_proxy.nil?
- @http_client = Net::HTTP.new(host, port)
+ 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"]
- @http_client = Net::HTTP.Proxy(http_proxy.host, http_proxy.port, user, pass).new(host, port)
+ Net::HTTP.Proxy(http_proxy.host, http_proxy.port, user, pass)
end
- if url.scheme == HTTPS
- @http_client.use_ssl = true
- if config[:ssl_verify_mode] == :verify_none
- @http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE
- elsif config[:ssl_verify_mode] == :verify_peer
- @http_client.verify_mode = OpenSSL::SSL::VERIFY_PEER
+ end
+
+ def configure_ssl(http_client)
+ http_client.use_ssl = true
+ if config[:ssl_verify_mode] == :verify_none
+ http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ elsif config[:ssl_verify_mode] == :verify_peer
+ http_client.verify_mode = OpenSSL::SSL::VERIFY_PEER
+ end
+ if config[:ssl_ca_path]
+ unless ::File.exist?(config[:ssl_ca_path])
+ raise Chef::Exceptions::ConfigurationError, "The configured ssl_ca_path #{config[:ssl_ca_path]} does not exist"
+ end
+ http_client.ca_path = config[:ssl_ca_path]
+ elsif config[:ssl_ca_file]
+ unless ::File.exist?(config[:ssl_ca_file])
+ raise Chef::Exceptions::ConfigurationError, "The configured ssl_ca_file #{config[:ssl_ca_file]} does not exist"
+ end
+ http_client.ca_file = config[:ssl_ca_file]
+ end
+ if (config[:ssl_client_cert] || config[:ssl_client_key])
+ unless (config[:ssl_client_cert] && config[:ssl_client_key])
+ raise Chef::Exceptions::ConfigurationError, "You must configure ssl_client_cert and ssl_client_key together"
end
- if config[:ssl_ca_path]
- unless ::File.exist?(config[:ssl_ca_path])
- raise Chef::Exceptions::ConfigurationError, "The configured ssl_ca_path #{config[:ssl_ca_path]} does not exist"
- end
- @http_client.ca_path = config[:ssl_ca_path]
- elsif config[:ssl_ca_file]
- unless ::File.exist?(config[:ssl_ca_file])
- raise Chef::Exceptions::ConfigurationError, "The configured ssl_ca_file #{config[:ssl_ca_file]} does not exist"
- end
- @http_client.ca_file = config[:ssl_ca_file]
+ unless ::File.exists?(config[:ssl_client_cert])
+ raise Chef::Exceptions::ConfigurationError, "The configured ssl_client_cert #{config[:ssl_client_cert]} does not exist"
end
- if (config[:ssl_client_cert] || config[:ssl_client_key])
- unless (config[:ssl_client_cert] && config[:ssl_client_key])
- raise Chef::Exceptions::ConfigurationError, "You must configure ssl_client_cert and ssl_client_key together"
- end
- unless ::File.exists?(config[:ssl_client_cert])
- raise Chef::Exceptions::ConfigurationError, "The configured ssl_client_cert #{config[:ssl_client_cert]} does not exist"
- end
- unless ::File.exists?(config[:ssl_client_key])
- raise Chef::Exceptions::ConfigurationError, "The configured ssl_client_key #{config[:ssl_client_key]} does not exist"
- end
- @http_client.cert = OpenSSL::X509::Certificate.new(::File.read(config[:ssl_client_cert]))
- @http_client.key = OpenSSL::PKey::RSA.new(::File.read(config[:ssl_client_key]))
+ unless ::File.exists?(config[:ssl_client_key])
+ raise Chef::Exceptions::ConfigurationError, "The configured ssl_client_key #{config[:ssl_client_key]} does not exist"
end
+ http_client.cert = OpenSSL::X509::Certificate.new(::File.read(config[:ssl_client_cert]))
+ http_client.key = OpenSSL::PKey::RSA.new(::File.read(config[:ssl_client_key]))
end
-
- @http_client.read_timeout = config[:rest_timeout]
- @http_client
- end
-
- def config
- Chef::Config
end
-
end
end
end
diff --git a/lib/chef/http/http_request.rb b/lib/chef/http/http_request.rb
index f67d966e5e..ec837f13f2 100644
--- a/lib/chef/http/http_request.rb
+++ b/lib/chef/http/http_request.rb
@@ -22,7 +22,6 @@
#
require 'uri'
require 'net/http'
-require 'chef/http/basic_client'
# To load faster, we only want ohai's version string.
# However, in ohai before 0.6.0, the version is defined
@@ -72,7 +71,6 @@ class Chef
def initialize(method, url, req_body, base_headers={})
@method, @url = method, url
@request_body = nil
- @http_client = BasicClient.new(url).http_client
build_headers(base_headers)
configure_http_request(req_body)
end
@@ -93,6 +91,7 @@ class Chef
@url.path.empty? ? SLASH : @url.path
end
+ # DEPRECATED. Call request on an HTTP client object instead.
def call
hide_net_http_bug do
http_client.request(http_request) do |response|
@@ -106,6 +105,11 @@ class Chef
Chef::Config
end
+ # DEPRECATED. Call request on an HTTP client object instead.
+ def http_client
+ @http_client ||= BasicClient.new(url).http_client
+ end
+
private
def hide_net_http_bug