class Puppet::Network::HTTP::WEBrickREST

Public Instance Methods

body(request) click to toggle source
# File lib/puppet/network/http/webrick/rest.rb, line 57
def body(request)
  request.body
end
client_cert(request) click to toggle source
# File lib/puppet/network/http/webrick/rest.rb, line 61
def client_cert(request)
  if cert = request.client_cert
    cert = Puppet::SSL::Certificate.from_instance(cert)
    warn_if_near_expiration(cert)
    cert
  else
    nil
  end
end
client_information(request) click to toggle source

Retrieve node/cert/ip information from the request object.

# File lib/puppet/network/http/webrick/rest.rb, line 88
def client_information(request)
  result = {}
  if peer = request.peeraddr and ip = peer[3]
    result[:ip] = ip
  end

  # If they have a certificate (which will almost always be true)
  # then we get the hostname from the cert, instead of via IP
  # info
  result[:authenticated] = false
  if cert = request.client_cert and cn = Puppet::Util::SSL.cn_from_subject(cert.subject)
    result[:node] = cn
    result[:authenticated] = true
  else
    result[:node] = resolve_node(result)
  end

  result
end
headers(request) click to toggle source
# File lib/puppet/network/http/webrick/rest.rb, line 41
def headers(request)
  result = {}
  request.each do |k, v|
    result[k.downcase] = v
  end
  result
end
http_method(request) click to toggle source
# File lib/puppet/network/http/webrick/rest.rb, line 49
def http_method(request)
  request.request_method
end
params(request) click to toggle source

Retrieve the request parameters, including authentication information.

# File lib/puppet/network/http/webrick/rest.rb, line 21
def params(request)
  params = request.query || {}

  params = Hash[params.collect do |key, value|
    all_values = value.list
    [key, all_values.length == 1 ? value : all_values]
  end]

  params = decode_params(params)
  params.merge(client_information(request))
end
path(request) click to toggle source
# File lib/puppet/network/http/webrick/rest.rb, line 53
def path(request)
  request.path
end
service(request, response) click to toggle source

WEBrick uses a service method to respond to requests. Simply delegate to the handler response method.

# File lib/puppet/network/http/webrick/rest.rb, line 35
def service(request, response)
  self.class.mutex.synchronize do
    process(request, response)
  end
end
set_content_type(response, format) click to toggle source

Set the specified format as the content type of the response.

# File lib/puppet/network/http/webrick/rest.rb, line 72
def set_content_type(response, format)
  response["content-type"] = format_to_mime(format)
end
set_response(response, result, status = 200) click to toggle source
# File lib/puppet/network/http/webrick/rest.rb, line 76
def set_response(response, result, status = 200)
  response.status = status
  if status >= 200 and status != 304
    response.body = result
    response["content-length"] = result.stat.size if result.is_a?(File)
  end
  if RUBY_VERSION[0,3] == "1.8"
    response["connection"] = 'close'
  end
end

Public Class Methods

mutex() click to toggle source
# File lib/puppet/network/http/webrick/rest.rb, line 10
def self.mutex
  @mutex ||= Mutex.new
end
new(server) click to toggle source
# File lib/puppet/network/http/webrick/rest.rb, line 14
def initialize(server)
  raise ArgumentError, "server is required" unless server
  register([Puppet::Network::HTTP::API::V2.routes, Puppet::Network::HTTP::API::V1.routes])
  super(server)
end