Class: Puppet::Network::HTTP::Request

Inherits:
Struct
  • Object
show all
Defined in:
lib/puppet/network/http/request.rb

Overview

This class is effectively public API, because a Request object is passed as a parameter to the current Handler subclass. Puppetserver implements its own Handler github.com/puppetlabs/puppetserver/blob/8.3.0/src/ruby/puppetserver-lib/puppet/server/network/http/handler.rb#L9 and the Request object is passed to its Handler#body method github.com/puppetlabs/puppetserver/blob/8.3.0/src/ruby/puppetserver-lib/puppet/server/network/http/handler.rb#L36

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



9
10
11
# File 'lib/puppet/network/http/request.rb', line 9

def body
  @body
end

#client_certObject

Returns the value of attribute client_cert

Returns:

  • (Object)

    the current value of client_cert



9
10
11
# File 'lib/puppet/network/http/request.rb', line 9

def client_cert
  @client_cert
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



9
10
11
# File 'lib/puppet/network/http/request.rb', line 9

def headers
  @headers
end

#methodObject

Returns the value of attribute method

Returns:

  • (Object)

    the current value of method



9
10
11
# File 'lib/puppet/network/http/request.rb', line 9

def method
  @method
end

#paramsObject

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params



9
10
11
# File 'lib/puppet/network/http/request.rb', line 9

def params
  @params
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



9
10
11
# File 'lib/puppet/network/http/request.rb', line 9

def path
  @path
end

#routing_pathObject

Returns the value of attribute routing_path

Returns:

  • (Object)

    the current value of routing_path



9
10
11
# File 'lib/puppet/network/http/request.rb', line 9

def routing_path
  @routing_path
end

Class Method Details

.from_hash(hash) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puppet/network/http/request.rb', line 10

def self.from_hash(hash)
  symbol_members = members.collect(&:intern)
  unknown = hash.keys - symbol_members
  if unknown.empty?
    new(hash[:headers] || {},
        hash[:params] || {},
        hash[:method] || "GET",
        hash[:path],
        hash[:routing_path] || hash[:path],
        hash[:client_cert],
        hash[:body])
  else
    raise ArgumentError, _("Unknown arguments: %{args}") % { args: unknown.collect(&:inspect).join(', ') }
  end
end

Instance Method Details

#formatterObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppet/network/http/request.rb', line 30

def formatter
  header = headers['content-type']
  if header
    header.gsub!(/\s*;.*$/, '') # strip any charset
    format = Puppet::Network::FormatHandler.mime(header)

    return format if valid_network_format?(format)

    # TRANSLATORS "mime-type" is a keyword and should not be translated
    raise Puppet::Network::HTTP::Error::HTTPUnsupportedMediaTypeError.new(
      _("Client sent a mime-type (%{header}) that doesn't correspond to a format we support") % { header: headers['content-type'] },
      Puppet::Network::HTTP::Issues::UNSUPPORTED_MEDIA_TYPE
    )
  end

  raise Puppet::Network::HTTP::Error::HTTPBadRequestError.new(
    _("No Content-Type header was received, it isn't possible to unserialize the request"),
    Puppet::Network::HTTP::Issues::MISSING_HEADER_FIELD
  )
end

#response_formatters_for(supported_formats, default_accepted_formats = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/network/http/request.rb', line 51

def response_formatters_for(supported_formats, default_accepted_formats = nil)
  accepted_formats = headers['accept'] || default_accepted_formats

  if accepted_formats.nil?
    raise Puppet::Network::HTTP::Error::HTTPBadRequestError.new(_("Missing required Accept header"), Puppet::Network::HTTP::Issues::MISSING_HEADER_FIELD)
  end

  formats = Puppet::Network::FormatHandler.most_suitable_formats_for(
    accepted_formats.split(/\s*,\s*/),
    supported_formats
  )

  formats.find_all do |format|
    # we are only passed supported_formats that are suitable
    # and whose klass implements the required_methods
    valid_network_format?(format)
  end

  return formats unless formats.empty?

  raise Puppet::Network::HTTP::Error::HTTPNotAcceptableError.new(
    _("No supported formats are acceptable (Accept: %{accepted_formats})") % { accepted_formats: accepted_formats },
    Puppet::Network::HTTP::Issues::UNSUPPORTED_FORMAT
  )
end

#route_into(prefix) ⇒ Object



26
27
28
# File 'lib/puppet/network/http/request.rb', line 26

def route_into(prefix)
  self.class.new(headers, params, method, path, routing_path.sub(prefix, ''), client_cert, body)
end