class Chef::HTTP::HTTPRequest
Constants
- ACCEPT_ENCODING
- DEFAULT_UA
- DELETE
- ENCODING_GZIP_DEFLATE
- GET
- HEAD
- HOST_LOWER
- HTTPS
- PATCH
- POST
- PUT
- SLASH
- UA_COMMON
- URI_SCHEME_DEFAULT_PORT
- USER_AGENT
Attributes
headers[R]
http_request[R]
method[R]
url[R]
Public Class Methods
new(method, url, req_body, base_headers = {})
click to toggle source
# File lib/chef/http/http_request.rb, line 76 def initialize(method, url, req_body, base_headers = {}) @method, @url = method, url @request_body = nil build_headers(base_headers) configure_http_request(req_body) end
user_agent()
click to toggle source
# File lib/chef/http/http_request.rb, line 70 def self.user_agent @user_agent ||= DEFAULT_UA end
user_agent=(ua)
click to toggle source
# File lib/chef/http/http_request.rb, line 66 def self.user_agent=(ua) @user_agent = ua end
Public Instance Methods
call() { |response| ... }
click to toggle source
DEPRECATED. Call request on an HTTP client object instead.
# File lib/chef/http/http_request.rb, line 104 def call hide_net_http_bug do http_client.request(http_request) do |response| yield response if block_given? response end end end
config()
click to toggle source
# File lib/chef/http/http_request.rb, line 113 def config Chef::Config end
host()
click to toggle source
# File lib/chef/http/http_request.rb, line 83 def host @url.hostname end
http_client()
click to toggle source
DEPRECATED. Call request on an HTTP client object instead.
# File lib/chef/http/http_request.rb, line 118 def http_client @http_client ||= BasicClient.new(url).http_client end
path()
click to toggle source
# File lib/chef/http/http_request.rb, line 99 def path @url.path.empty? ? SLASH : @url.path end
port()
click to toggle source
# File lib/chef/http/http_request.rb, line 91 def port @url.port end
query()
click to toggle source
# File lib/chef/http/http_request.rb, line 95 def query @url.query end
uri_safe_host()
click to toggle source
# File lib/chef/http/http_request.rb, line 87 def uri_safe_host @url.host end
Private Instance Methods
build_headers(headers)
click to toggle source
# File lib/chef/http/http_request.rb, line 139 def build_headers(headers) @headers = headers.dup # No response compression unless we asked for it explicitly: @headers[HTTPRequest::ACCEPT_ENCODING] ||= "identity" @headers["X-Chef-Version"] = ::Chef::VERSION # Only include port in Host header when it is not the default port # for the url scheme (80;443) - Fixes CHEF-5355 host_header = uri_safe_host.dup host_header << ":#{port}" unless URI_SCHEME_DEFAULT_PORT[@url.scheme] == port.to_i @headers["Host"] = host_header unless @headers.keys.any? { |k| k.downcase.to_s == HOST_LOWER } @headers end
configure_http_request(request_body = nil)
click to toggle source
# File lib/chef/http/http_request.rb, line 154 def configure_http_request(request_body = nil) req_path = "#{path}" req_path << "?#{query}" if query @http_request = case method.to_s.downcase when GET Net::HTTP::Get.new(req_path, headers) when POST Net::HTTP::Post.new(req_path, headers) when PUT Net::HTTP::Put.new(req_path, headers) when PATCH Net::HTTP::Patch.new(req_path, headers) when DELETE Net::HTTP::Delete.new(req_path, headers) when HEAD Net::HTTP::Head.new(req_path, headers) else raise ArgumentError, "You must provide :GET, :PUT, :POST, :DELETE or :HEAD as the method" end @http_request.body = request_body if request_body && @http_request.request_body_permitted? # Optionally handle HTTP Basic Authentication if url.user user = URI.unescape(url.user) password = URI.unescape(url.password) if url.password @http_request.basic_auth(user, password) end # Overwrite default UA @http_request[USER_AGENT] = self.class.user_agent end
hide_net_http_bug() { || ... }
click to toggle source
# File lib/chef/http/http_request.rb, line 124 def hide_net_http_bug yield rescue NoMethodError => e # http://redmine.ruby-lang.org/issues/show/2708 # http://redmine.ruby-lang.org/issues/show/2758 if e.to_s =~ /#{Regexp.escape(%q{undefined method `closed?' for nil:NilClass})}/ Chef::Log.trace("Rescued error in http connect, re-raising as Errno::ECONNREFUSED to hide bug in net/http") Chef::Log.trace("#{e.class.name}: #{e}") Chef::Log.trace(e.backtrace.join("\n")) raise Errno::ECONNREFUSED, "Connection refused attempting to contact #{url.scheme}://#{host}:#{port}" else raise end end