summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2013-11-25 16:54:05 -0800
committerdanielsdeleo <dan@opscode.com>2013-11-25 16:54:05 -0800
commit1248a9e7b05211684f0905641e9e3382daf90db8 (patch)
tree71044d3a55512daf0f25e485dfdb99b7d2eef200
parentb3983fbb49fb15e61840fc1535e4c7c4f0906636 (diff)
parent62f2308f32dd664048bdb3b75251f0230072f7eb (diff)
downloadchef-1248a9e7b05211684f0905641e9e3382daf90db8.tar.gz
Merge branch 'ipv6-host-header' into 10-stable
Ensure Host header is set manually for all requests. This works around an issue with Net::HTTP where the Host header is missing the necessary brackets when the Host is identified by IPv6 address. E.g., Good: Host: [::1]:8888 Bad: Host: ::1:8888
-rw-r--r--chef/lib/chef/rest.rb1
-rw-r--r--chef/spec/unit/rest_spec.rb23
2 files changed, 18 insertions, 6 deletions
diff --git a/chef/lib/chef/rest.rb b/chef/lib/chef/rest.rb
index f61801f730..48ef218e87 100644
--- a/chef/lib/chef/rest.rb
+++ b/chef/lib/chef/rest.rb
@@ -445,6 +445,7 @@ class Chef
def build_headers(method, url, headers={}, json_body=false, raw=false)
headers = @default_headers.merge(headers)
+ headers['Host'] = "#{url.host}:#{url.port}"
#headers['Accept'] = "application/json" unless raw
headers['Accept'] = "application/json" unless raw
headers["Content-Type"] = 'application/json' if json_body && !headers.keys.map(&:downcase).include?("content-type")
diff --git a/chef/spec/unit/rest_spec.rb b/chef/spec/unit/rest_spec.rb
index 91c994a816..f24ef1a08d 100644
--- a/chef/spec/unit/rest_spec.rb
+++ b/chef/spec/unit/rest_spec.rb
@@ -137,6 +137,7 @@ describe Chef::REST do
Chef::Config[:ssl_client_cert] = nil
Chef::Config[:ssl_client_key] = nil
@url = URI.parse("https://one:80/?foo=bar")
+ @expected_host_header = "one:80"
@http_response = Net::HTTPSuccess.new("1.1", "200", "successful rest req")
@http_response.stub!(:read_body)
@@ -149,7 +150,8 @@ describe Chef::REST do
@base_headers = { 'Accept' => 'application/json',
'X-Chef-Version' => Chef::VERSION,
- 'Accept-Encoding' => Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE}
+ 'Accept-Encoding' => Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE,
+ 'Host' => @expected_host_header }
@req_with_body_headers = @base_headers.merge("Content-Type" => "application/json", "Content-Length" => '13')
end
@@ -291,7 +293,9 @@ describe Chef::REST do
end
it "should build a new HTTP GET request without the application/json accept header" do
- expected_headers = {'X-Chef-Version' => Chef::VERSION, 'Accept-Encoding' => Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE}
+ expected_headers = { 'X-Chef-Version' => Chef::VERSION,
+ 'Accept-Encoding' => Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE,
+ 'Host' => @expected_host_header }
Net::HTTP::Get.should_receive(:new).with("/?foo=bar", expected_headers).and_return(@request_mock)
@rest.run_request(:GET, @url, {}, false, nil, true)
end
@@ -339,7 +343,8 @@ describe Chef::REST do
@base_headers = {"Accept" => "application/json",
"X-Chef-Version" => Chef::VERSION,
- "Accept-Encoding" => Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE
+ "Accept-Encoding" => Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE,
+ "Host" => @expected_host_header
}
end
@@ -472,8 +477,12 @@ describe Chef::REST do
redirect = redirect_with(response_name)
headers = { "X-Auth-Header" => "foo" }
- auto_headers = {"Accept"=>"application/json", "Accept-Encoding"=>"gzip;q=1.0,deflate;q=0.6,identity;q=0.3"}
+ auto_headers = { "Accept"=>"application/json",
+ "Accept-Encoding"=>"gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
+ "Host" => @expected_host_header}
expected_headers = auto_headers.merge(headers)
+ expected_headers_on_redirect = expected_headers.dup
+ expected_headers_on_redirect["Host"] = "chef.example.com:8443"
success = Net::HTTPSuccess.new("1.1",200, "it-works")
success.stub!(:read_body).and_return('{"foo": "bar"}')
@@ -484,7 +493,7 @@ describe Chef::REST do
# CHEF-1848: verify that headers get passed to redirects
@rest.should_receive(:retriable_rest_request).with(:GET, @url, nil, expected_headers).and_call_original
- @rest.should_receive(:retriable_rest_request).with(:GET, redirected_uri, nil, expected_headers).and_call_original
+ @rest.should_receive(:retriable_rest_request).with(:GET, redirected_uri, nil, expected_headers_on_redirect).and_call_original
@rest.api_request(:GET, @url, headers).should == {"foo" => "bar"}
end
@@ -557,7 +566,9 @@ describe Chef::REST do
end
it " build a new HTTP GET request without the application/json accept header" do
- expected_headers = {'X-Chef-Version' => Chef::VERSION, 'Accept-Encoding' => Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE}
+ expected_headers = { 'X-Chef-Version' => Chef::VERSION,
+ 'Accept-Encoding' => Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE,
+ 'Host' => @expected_host_header }
Net::HTTP::Get.should_receive(:new).with("/?foo=bar", expected_headers).and_return(@request_mock)
@rest.streaming_request(@url, {})
end