Class: Chef::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/http.rb,
lib/chef/http/simple.rb,
lib/chef/http/cookie_jar.rb,
lib/chef/http/json_input.rb,
lib/chef/http/json_output.rb,
lib/chef/http/simple_json.rb,
lib/chef/http/api_versions.rb,
lib/chef/http/basic_client.rb,
lib/chef/http/decompressor.rb,
lib/chef/http/http_request.rb,
lib/chef/http/ssl_policies.rb,
lib/chef/http/authenticator.rb,
lib/chef/http/cookie_manager.rb,
lib/chef/http/auth_credentials.rb,
lib/chef/http/remote_request_id.rb,
lib/chef/http/json_to_model_output.rb,
lib/chef/http/validate_content_length.rb,
lib/chef/http/socketless_chef_zero_client.rb

Overview

== Chef::HTTP Basic HTTP client, with support for adding features via middleware

Direct Known Subclasses

Simple, SimpleJSON, ServerAPI

Defined Under Namespace

Classes: APISSLPolicy, APIVersions, AuthCredentials, Authenticator, BasicClient, CookieJar, CookieManager, Decompressor, DefaultSSLPolicy, HTTPRequest, JSONInput, JSONOutput, JSONToModelOutput, RemoteRequestID, Simple, SimpleJSON, SocketlessChefZeroClient, StreamHandler, ValidateContentLength, VerifyNoneSSLPolicy, VerifyPeerSSLPolicy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ HTTP

Create a HTTP client object. The supplied +url+ is used as the base for all subsequent requests. For example, when initialized with a base url http://localhost:4000, a call to +get+ with 'nodes' will make an HTTP GET request to http://localhost:4000/nodes



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/http.rb', line 92

def initialize(url, options = {})
  @url = url
  @default_headers = options[:headers] || {}
  @sign_on_redirect = true
  @redirects_followed = 0
  @redirect_limit = 10
  @keepalives = options[:keepalives] || false
  @options = options
  @nethttp_opts = options[:nethttp] || {}

  @middlewares = []
  self.class.middlewares.each do |middleware_class|
    @middlewares << middleware_class.new(options)
  end
end

Instance Attribute Details

#keepalivesObject (readonly)

[Boolean] if we're doing keepalives or not



83
84
85
# File 'lib/chef/http.rb', line 83

def keepalives
  @keepalives
end

#middlewaresObject (readonly)

Returns the value of attribute middlewares.



80
81
82
# File 'lib/chef/http.rb', line 80

def middlewares
  @middlewares
end

#nethttp_optsObject (readonly)

Returns the value of attribute nethttp_opts.



86
87
88
# File 'lib/chef/http.rb', line 86

def nethttp_opts
  @nethttp_opts
end

#optionsObject (readonly)

Returns the value of attribute options.



78
79
80
# File 'lib/chef/http.rb', line 78

def options
  @options
end

#redirect_limitObject (readonly)

Returns the value of attribute redirect_limit.



76
77
78
# File 'lib/chef/http.rb', line 76

def redirect_limit
  @redirect_limit
end

#sign_on_redirectObject (readonly)

Returns the value of attribute sign_on_redirect.



75
76
77
# File 'lib/chef/http.rb', line 75

def sign_on_redirect
  @sign_on_redirect
end

#urlObject (readonly)

Returns the value of attribute url.



74
75
76
# File 'lib/chef/http.rb', line 74

def url
  @url
end

Class Method Details

.middlewaresObject



66
67
68
# File 'lib/chef/http.rb', line 66

def self.middlewares
  @middlewares ||= []
end

.use(middleware_class) ⇒ Object



70
71
72
# File 'lib/chef/http.rb', line 70

def self.use(middleware_class)
  middlewares << middleware_class
end

Instance Method Details

#delete(path, headers = {}) ⇒ Object

Send an HTTP DELETE request to the path

=== Parameters path:: path part of the request URL



144
145
146
# File 'lib/chef/http.rb', line 144

def delete(path, headers = {})
  request(:DELETE, path, headers)
end

#get(path, headers = {}) ⇒ Object

Send an HTTP GET request to the path

=== Parameters path:: The path to GET



120
121
122
# File 'lib/chef/http.rb', line 120

def get(path, headers = {})
  request(:GET, path, headers)
end

#head(path, headers = {}) ⇒ Object

Send an HTTP HEAD request to the path

=== Parameters path:: path part of the request URL



112
113
114
# File 'lib/chef/http.rb', line 112

def head(path, headers = {})
  request(:HEAD, path, headers)
end

#http_client(base_url = nil) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/chef/http.rb', line 263

def http_client(base_url = nil)
  base_url ||= url
  if keepalives && !base_url.nil?
    # only reuse the http_client if we want keepalives and have a base_url
    @http_client ||= {}
    # the per-host per-port cache here gets persistent connections correct when
    # redirecting to different servers
    if base_url.is_a?(String) # sigh, this kind of abuse can't happen with strongly typed languages
      @http_client[base_url] ||= build_http_client(base_url)
    else
      @http_client[base_url.host] ||= {}
      @http_client[base_url.host][base_url.port] ||= build_http_client(base_url)
    end
  else
    build_http_client(base_url)
  end
end

#last_responseObject

DEPRECATED: This is only kept around to provide access to cache control data in lib/chef/provider/remote_file/http.rb FIXME: Find a better API.



284
285
286
# File 'lib/chef/http.rb', line 284

def last_response
  @last_response
end

#post(path, json, headers = {}) ⇒ Object

Send an HTTP POST request to the path

=== Parameters path:: path part of the request URL



136
137
138
# File 'lib/chef/http.rb', line 136

def post(path, json, headers = {})
  request(:POST, path, headers, json)
end

#put(path, json, headers = {}) ⇒ Object

Send an HTTP PUT request to the path

=== Parameters path:: path part of the request URL



128
129
130
# File 'lib/chef/http.rb', line 128

def put(path, json, headers = {})
  request(:PUT, path, headers, json)
end

#request(method, path, headers = {}, data = false) ⇒ Object

Makes an HTTP request to +path+ with the given +method+, +headers+, and +data+ (if applicable).



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/chef/http.rb', line 150

def request(method, path, headers = {}, data = false)
  http_attempts ||= 0
  url = create_url(path)
  processed_method, url, processed_headers, processed_data = apply_request_middleware(method, url, headers, data)

  response, rest_request, return_value = send_http_request(processed_method, url, processed_headers, processed_data)
  response, rest_request, return_value = apply_response_middleware(response, rest_request, return_value)

  response.error! unless success_response?(response)
  return_value

rescue Net::HTTPClientException => e
  http_attempts += 1
  response = e.response
  if response.is_a?(Net::HTTPNotAcceptable) && version_retries - http_attempts >= 0
    Chef::Log.trace("Negotiating protocol version with #{url}, retry #{http_attempts}/#{version_retries}")
    retry
  else
    raise
  end
rescue Exception => exception
  log_failed_request(response, return_value) unless response.nil?
  raise
end

#streaming_request(path, headers = {}, tempfile = nil) {|tempfile| ... } ⇒ Object

Makes a streaming download request, streaming the response body to a tempfile. If a block is given, the tempfile is passed to the block and the tempfile will automatically be unlinked after the block is executed.

If no block is given, the tempfile is returned, which means it's up to you to unlink the tempfile when you're done with it.

Yields:

  • (tempfile)

    block to process the tempfile

Yield Parameters:

  • tempfile (tempfile<Tempfile>)


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/chef/http.rb', line 219

def streaming_request(path, headers = {}, tempfile = nil)
  http_attempts ||= 0
  url = create_url(path)
  response, rest_request, return_value = nil, nil, nil
  data = nil

  method = :GET
  method, url, processed_headers, data = apply_request_middleware(method, url, headers, data)

  response, rest_request, return_value = send_http_request(method, url, processed_headers, data) do |http_response|
    if http_response.is_a?(Net::HTTPSuccess)
      tempfile = stream_to_tempfile(url, http_response, tempfile)
    end
    apply_stream_complete_middleware(http_response, rest_request, return_value)
  end

  return nil if response.is_a?(Net::HTTPRedirection)

  unless response.is_a?(Net::HTTPSuccess)
    response.error!
  end

  if block_given?
    begin
      yield tempfile
    ensure
      tempfile && tempfile.close!
    end
  end
  tempfile
rescue Net::HTTPClientException => e
  http_attempts += 1
  response = e.response
  if response.is_a?(Net::HTTPNotAcceptable) && version_retries - http_attempts >= 0
    Chef::Log.trace("Negotiating protocol version with #{url}, retry #{http_attempts}/#{version_retries}")
    retry
  else
    raise
  end
rescue Exception => e
  log_failed_request(response, return_value) unless response.nil?
  raise
end

#streaming_request_with_progress(path, headers = {}, tempfile = nil, &progress_block) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/chef/http.rb', line 175

def streaming_request_with_progress(path, headers = {}, tempfile = nil, &progress_block)
  http_attempts ||= 0
  url = create_url(path)
  response, rest_request, return_value = nil, nil, nil
  data = nil

  method = :GET
  method, url, processed_headers, data = apply_request_middleware(method, url, headers, data)

  response, rest_request, return_value = send_http_request(method, url, processed_headers, data) do |http_response|
    if http_response.is_a?(Net::HTTPSuccess)
      tempfile = stream_to_tempfile(url, http_response, tempfile, &progress_block)
    end
    apply_stream_complete_middleware(http_response, rest_request, return_value)
  end
  return nil if response.is_a?(Net::HTTPRedirection)

  unless response.is_a?(Net::HTTPSuccess)
    response.error!
  end
  tempfile
rescue Net::HTTPClientException => e
  http_attempts += 1
  response = e.response
  if response.is_a?(Net::HTTPNotAcceptable) && version_retries - http_attempts >= 0
    Chef::Log.trace("Negotiating protocol version with #{url}, retry #{http_attempts}/#{version_retries}")
    retry
  else
    raise
  end
rescue Exception => e
  log_failed_request(response, return_value) unless response.nil?
  raise
end