summaryrefslogtreecommitdiff
path: root/lib/chef/rest.rb
diff options
context:
space:
mode:
authordanielsdeleo <dan@getchef.com>2015-03-31 12:19:05 -0700
committerdanielsdeleo <dan@getchef.com>2015-04-01 13:35:01 -0700
commitaab0ccb5d41913e050707bd6b40e5b820649c566 (patch)
tree0eb3839f664afb6f5eade15e32f85313cf41defa /lib/chef/rest.rb
parent8b42ac0374fb075fbcb21df73742e81e69d9bf6f (diff)
downloadchef-aab0ccb5d41913e050707bd6b40e5b820649c566.tar.gz
Extract socketless client and add specs
Diffstat (limited to 'lib/chef/rest.rb')
-rw-r--r--lib/chef/rest.rb145
1 files changed, 0 insertions, 145 deletions
diff --git a/lib/chef/rest.rb b/lib/chef/rest.rb
index 855608385d..2612714a19 100644
--- a/lib/chef/rest.rb
+++ b/lib/chef/rest.rb
@@ -40,151 +40,6 @@ require 'chef/http/remote_request_id'
class Chef
- class SocketlessChefZeroClient
-
- module Response
-
- def read_body(dest = nil, &block)
- if dest
- raise "responses from socketless chef zero can't be written to specific destination"
- end
-
- if block_given?
- block.call(@body)
- else
- super
- end
- end
-
- end
-
- # copied verbatim from webrick
- #
- # HTTP status codes and descriptions
- STATUS_MESSAGE = { # :nodoc:
- 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',
- }
-
- STATUS_MESSAGE.values.each {|v| v.freeze }
- STATUS_MESSAGE.freeze
-
- def initialize(base_url)
- @url = base_url
- end
-
- def host
- @url.hostname
- end
-
- def port
- @url.port
- end
-
- # request, response = client.request(method, url, body, headers) {|r| r.read_body }
- def request(method, url, body, headers, &handler_block)
- #pp req: [method, url, body, headers]
- body_str = body || ""
- r = {}
- r["REQUEST_METHOD"] = method.to_s.upcase
- r["SCRIPT_NAME"] = ""
- r["PATH_INFO"] = url.path
- r["QUERY_STRING"] = url.query
- r["SERVER_NAME"] = "localhost"
- r["SERVER_PORT"] = url.port
- r["HTTP_HOST"] = "localhost:#{url.port}"
- r["rack.url_scheme"] = "chefzero"
- r["rack.input"] = StringIO.new(body_str)
-
- res = ChefZero::SocketlessServerMap.request(port, r)
-
- net_http_response = to_net_http(res[0], res[1], res[2])
-
- yield net_http_response if block_given?
-
- [self, net_http_response]
- end
-
- # TODO: this is copied verbatim from the fakeweb project, MIT licensed
- # Add credits where appropriate
-
- 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(Response)
- response
- end
-
- private
-
- def headers_extracted_from_options
- options.reject {|name, _| KNOWN_OPTIONS.include?(name) }.map { |name, value|
- [name.to_s.split("_").map { |segment| segment.capitalize }.join("-"), value]
- }
- end
-
-
- end
-
# == Chef::REST
# Chef's custom REST client with built-in JSON support and RSA signed header
# authentication.