Class: Puppet::Network::HTTP::WEBrickREST

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Includes:
Handler
Defined in:
lib/puppet/network/http/webrick/rest.rb

Constant Summary

Constants included from Handler

Handler::DISALLOWED_KEYS

Constants included from Issues

Issues::ENVIRONMENT_NOT_FOUND, Issues::FAILED_AUTHORIZATION, Issues::HANDLER_NOT_FOUND, Issues::MISSING_HEADER_FIELD, Issues::NO_INDIRECTION_REMOTE_REQUESTS, Issues::RESOURCE_NOT_FOUND, Issues::RUNTIME_ERROR, Issues::UNSUPPORTED_FORMAT, Issues::UNSUPPORTED_MEDIA_TYPE, Issues::UNSUPPORTED_METHOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Handler

#format_to_mime, #process, #register, #resolve_node

Constructor Details

#initialize(server) ⇒ WEBrickREST

Returns a new instance of WEBrickREST.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
# File 'lib/puppet/network/http/webrick/rest.rb', line 15

def initialize(server)
  raise ArgumentError, _("server is required") unless server
  register([Puppet::Network::HTTP::API.master_routes,
            Puppet::Network::HTTP::API.ca_routes,
            Puppet::Network::HTTP::API.not_found_upgrade])
  super(server)
end

Class Method Details

.mutexObject



11
12
13
# File 'lib/puppet/network/http/webrick/rest.rb', line 11

def self.mutex
  @mutex ||= Mutex.new
end

Instance Method Details

#body(request) ⇒ Object



69
70
71
# File 'lib/puppet/network/http/webrick/rest.rb', line 69

def body(request)
  request.body
end

#client_cert(request) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/puppet/network/http/webrick/rest.rb', line 73

def client_cert(request)
  if cert = request.client_cert
    Puppet::SSL::Certificate.from_instance(cert)
  else
    nil
  end
end

#client_information(request) ⇒ Object

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/puppet/network/http/webrick/rest.rb', line 95

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) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/puppet/network/http/webrick/rest.rb', line 53

def headers(request)
  result = {}
  request.each do |k, v|
    result[k.downcase] = v
  end
  result
end

#http_method(request) ⇒ Object



61
62
63
# File 'lib/puppet/network/http/webrick/rest.rb', line 61

def http_method(request)
  request.request_method
end

#params(request) ⇒ Object

Retrieve the request parameters, including authentication information.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet/network/http/webrick/rest.rb', line 24

def params(request)
  query = request.query || {}
  params =
      if request.request_method == "PUT"
        # webrick doesn't look at the query string for PUT requests, it only
        # looks at the body, and then only if the body has a content type that
        # looks like url-encoded form data.  We need the query string data as well.
        WEBrick::HTTPUtils.parse_query(request.query_string).merge(query)
      else
        query
      end

  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) ⇒ Object



65
66
67
# File 'lib/puppet/network/http/webrick/rest.rb', line 65

def path(request)
  request.path
end

#service(request, response) ⇒ Object

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



47
48
49
50
51
# File 'lib/puppet/network/http/webrick/rest.rb', line 47

def service(request, response)
  self.class.mutex.synchronize do
    process(request, response)
  end
end

#set_content_type(response, format) ⇒ Object

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



82
83
84
# File 'lib/puppet/network/http/webrick/rest.rb', line 82

def set_content_type(response, format)
  response["content-type"] = format
end

#set_response(response, result, status = 200) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/puppet/network/http/webrick/rest.rb', line 86

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
end