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


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 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| ... } ⇒ Object

FIXME: yard with @yield

Yields:

  • (net_http_response)

152
153
154
155
156
157
158
159
160
161
# 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) ⇒ Object


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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