class Chef::HTTP::SocketlessChefZeroClient

HTTP Client class that talks directly to Zero via the Rack interface.

Constants

STATUS_MESSAGE

copied verbatim from webrick (2-clause BSD License)

HTTP status codes and descriptions

Attributes

url[R]

Public Class Methods

new(base_url) click to toggle source
# File lib/chef/http/socketless_chef_zero_client.rb, line 139
def initialize(base_url)
  @url = base_url
end

Public Instance Methods

host() click to toggle source
# File lib/chef/http/socketless_chef_zero_client.rb, line 143
def host
  @url.hostname
end
port() click to toggle source
# File lib/chef/http/socketless_chef_zero_client.rb, line 147
def port
  @url.port
end
req_to_rack(method, url, body, headers) click to toggle source
# File lib/chef/http/socketless_chef_zero_client.rb, line 163
def req_to_rack(method, url, body, headers)
  body_str = body || ""
  {
    "SCRIPT_NAME"     => "",
    "SERVER_NAME"     => "localhost",
    "REQUEST_METHOD"  => method.to_s.upcase,
    "PATH_INFO"       => url.path,
    "QUERY_STRING"    => url.query,
    "SERVER_PORT"     => url.port,
    "HTTP_HOST"       => "localhost:#{url.port}",
    "HTTP_X_OPS_SERVER_API_VERSION" => headers["X-Ops-Server-API-Version"],
    "rack.url_scheme" => "chefzero",
    "rack.input"      => StringIO.new(body_str),
  }
end
request(method, url, body, headers) { |net_http_response| ... } click to toggle source

FIXME: yard with @yield

# File lib/chef/http/socketless_chef_zero_client.rb, line 152
def request(method, url, body, headers)
  request = req_to_rack(method, url, body, headers)
  res = ChefZero::SocketlessServerMap.request(port, request)

  net_http_response = to_net_http(res[0], res[1], res[2])

  yield net_http_response if block_given?

  [self, net_http_response]
end
to_net_http(code, headers, chunked_body) click to toggle source
# File lib/chef/http/socketless_chef_zero_client.rb, line 179
def to_net_http(code, headers, chunked_body)
  body = chunked_body.join("")
  msg = STATUS_MESSAGE[code]
  raise "Cannot determine HTTP status message for code #{code}" unless msg
  response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
  response.instance_variable_set(:@body, body)
  headers.each do |name, value|
    if value.respond_to?(:each)
      value.each { |v| response.add_field(name, v) }
    else
      response[name] = value
    end
  end

  response.instance_variable_set(:@read, true)
  response.extend(ResponseExts)
  response
end

Private Instance Methods

headers_extracted_from_options() click to toggle source
# File lib/chef/http/socketless_chef_zero_client.rb, line 200
def headers_extracted_from_options
  options.reject { |name, _| KNOWN_OPTIONS.include?(name) }.map do |name, value|
    [name.to_s.split("_").map { |segment| segment.capitalize }.join("-"), value]
  end
end