class Puppet::Network::HTTP::RackREST

Constants

CHUNK_SIZE
ContentType

Public Instance Methods

body(request) click to toggle source

return the request body

# File lib/puppet/network/http/rack/rest.rb, line 84
def body(request)
  request.body.read
end
cleanup(request) click to toggle source

Passenger freaks out if we finish handling the request without reading any part of the body, so make sure we have.

# File lib/puppet/network/http/rack/rest.rb, line 105
def cleanup(request)
  request.body.read(1)
  nil
end
client_cert(request) click to toggle source
# File lib/puppet/network/http/rack/rest.rb, line 88
def client_cert(request)
  # This environment variable is set by mod_ssl, note that it
  # requires the `+ExportCertData` option in the `SSLOptions` directive
  cert = request.env['SSL_CLIENT_CERT']
  # NOTE: The SSL_CLIENT_CERT environment variable will be the empty string
  # when Puppet agent nodes have not yet obtained a signed certificate.
  if cert.nil? || cert.empty?
    nil
  else
    cert = Puppet::SSL::Certificate.from_instance(OpenSSL::X509::Certificate.new(cert))
    warn_if_near_expiration(cert)
    cert
  end
end
convert_singular_arrays_to_value(hash) click to toggle source
# File lib/puppet/network/http/rack/rest.rb, line 131
def convert_singular_arrays_to_value(hash)
  hash.each do |key, value|
    if value.size == 1
      hash[key] = value.first
    end
  end
end
extract_client_info(request) click to toggle source
# File lib/puppet/network/http/rack/rest.rb, line 110
def extract_client_info(request)
  result = {}
  result[:ip] = request.ip

  # if we find SSL info in the headers, use them to get a hostname from the CN.
  # try this with :ssl_client_header, which defaults should work for
  # Apache with StdEnvVars.
  subj_str = request.env[Puppet[:ssl_client_header]]
  subject = Puppet::Util::SSL.subject_from_dn(subj_str || "")

  if cn = Puppet::Util::SSL.cn_from_subject(subject)
    result[:node] = cn
    result[:authenticated] = (request.env[Puppet[:ssl_client_verify_header]] == 'SUCCESS')
  else
    result[:node] = resolve_node(result)
    result[:authenticated] = false
  end

  result
end
headers(request) click to toggle source

Retrieve all headers from the http request, as a map.

# File lib/puppet/network/http/rack/rest.rb, line 50
def headers(request)
  headers = request.env.select {|k,v| k.start_with? 'HTTP_'}.inject({}) do |m, (k,v)|
    m[k.sub(/^HTTP_/, '').gsub('_','-').downcase] = v
    m
  end
  headers['content-type'] = request.content_type
  headers
end
http_method(request) click to toggle source

Return which HTTP verb was used in this request.

# File lib/puppet/network/http/rack/rest.rb, line 60
def http_method(request)
  request.request_method
end
params(request) click to toggle source

Return the query params for this request.

# File lib/puppet/network/http/rack/rest.rb, line 65
def params(request)
  if request.post?
    params = request.params
  else
    # rack doesn't support multi-valued query parameters,
    # e.g. ignore, so parse them ourselves
    params = CGI.parse(request.query_string)
    convert_singular_arrays_to_value(params)
  end
  result = decode_params(params)
  result.merge(extract_client_info(request))
end
path(request) click to toggle source

what path was requested? (this is, without any query parameters)

# File lib/puppet/network/http/rack/rest.rb, line 79
def path(request)
  request.path
end
set_content_type(response, format) click to toggle source
# File lib/puppet/network/http/rack/rest.rb, line 34
def set_content_type(response, format)
  response[ContentType] = format_to_mime(format)
end
set_response(response, result, status = 200) click to toggle source

produce the body of the response

# File lib/puppet/network/http/rack/rest.rb, line 39
def set_response(response, result, status = 200)
  response.status = status
  unless result.is_a?(File)
    response.write result
  else
    response["Content-Length"] = result.stat.size.to_s
    response.body = RackFile.new(result)
  end
end

Public Class Methods

new(args={}) click to toggle source
# File lib/puppet/network/http/rack/rest.rb, line 29
def initialize(args={})
  super()
  register([Puppet::Network::HTTP::API::V2.routes, Puppet::Network::HTTP::API::V1.routes])
end