Class: Chef::HTTP::SocketlessChefZeroClient

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/http/socketless_chef_zero_client.rb

Overview

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

Defined Under Namespace

Modules: ResponseExts

Constant Summary collapse

STATUS_MESSAGE =

copied verbatim from webrick (2-clause BSD License)

HTTP status codes and descriptions

{
  100 => 'Continue',
  101 => 'Switching Protocols',
  200 => 'OK',
  201 => 'Created',
  202 => 'Accepted',
  203 => 'Non-Authoritative Information',
  204 => 'No Content',
  205 => 'Reset Content',
  206 => 'Partial Content',
  207 => 'Multi-Status',
  300 => 'Multiple Choices',
  301 => 'Moved Permanently',
  302 => 'Found',
  303 => 'See Other',
  304 => 'Not Modified',
  305 => 'Use Proxy',
  307 => 'Temporary Redirect',
  400 => 'Bad Request',
  401 => 'Unauthorized',
  402 => 'Payment Required',
  403 => 'Forbidden',
  404 => 'Not Found',
  405 => 'Method Not Allowed',
  406 => 'Not Acceptable',
  407 => 'Proxy Authentication Required',
  408 => 'Request Timeout',
  409 => 'Conflict',
  410 => 'Gone',
  411 => 'Length Required',
  412 => 'Precondition Failed',
  413 => 'Request Entity Too Large',
  414 => 'Request-URI Too Large',
  415 => 'Unsupported Media Type',
  416 => 'Request Range Not Satisfiable',
  417 => 'Expectation Failed',
  422 => 'Unprocessable Entity',
  423 => 'Locked',
  424 => 'Failed Dependency',
  426 => 'Upgrade Required',
  428 => 'Precondition Required',
  429 => 'Too Many Requests',
  431 => 'Request Header Fields Too Large',
  500 => 'Internal Server Error',
  501 => 'Not Implemented',
  502 => 'Bad Gateway',
  503 => 'Service Unavailable',
  504 => 'Gateway Timeout',
  505 => 'HTTP Version Not Supported',
  507 => 'Insufficient Storage',
  511 => 'Network Authentication Required',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ SocketlessChefZeroClient

Returns a new instance of SocketlessChefZeroClient.



139
140
141
# File 'lib/chef/http/socketless_chef_zero_client.rb', line 139

def initialize(base_url)
  @url = base_url
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#hostObject



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

def host
  @url.hostname
end

#portObject



147
148
149
# File 'lib/chef/http/socketless_chef_zero_client.rb', line 147

def port
  @url.port
end

#req_to_rack(method, url, body, headers) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/chef/http/socketless_chef_zero_client.rb', line 162

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}",
    "rack.url_scheme" => "chefzero",
    "rack.input"      => StringIO.new(body_str),
  }
end

#request(method, url, body, headers) {|net_http_response| ... } ⇒ Object

Yields:

  • (net_http_response)


151
152
153
154
155
156
157
158
159
160
# File 'lib/chef/http/socketless_chef_zero_client.rb', line 151

def request(method, url, body, headers, &handler_block)
  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) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/chef/http/socketless_chef_zero_client.rb', line 177

def to_net_http(code, headers, chunked_body)
  body = chunked_body.join('')
  msg = STATUS_MESSAGE[code] or raise "Cannot determine HTTP status message for code #{code}"
  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