Class: Puppet::Network::HTTPServer::Mongrel

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/vendor/puppet/network/http_server/mongrel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handlers) ⇒ Mongrel

Returns a new instance of Mongrel.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vendor/puppet/network/http_server/mongrel.rb', line 35

def initialize(handlers)
  if Puppet[:debug]
    $mongrel_debug_client = true
    Puppet.debug 'Mongrel client debugging enabled. [$mongrel_debug_client = true].'
  end
  # Create a new instance of BasicServer. We are supposed to subclass it
  # but that does not make sense since we would not introduce any new
  # behaviour and we have to subclass Mongrel::HttpHandler so our handler
  # works for Mongrel.
  @xmlrpc_server = Puppet::Network::XMLRPCServer.new
  handlers.each do |name|
    unless handler = Puppet::Network::Handler.handler(name)
      raise ArgumentError, "Invalid handler #{name}"
    end
    @xmlrpc_server.add_handler(handler.interface, handler.new({}))
  end
end

Instance Attribute Details

#xmlrpc_serverObject (readonly)

Returns the value of attribute xmlrpc_server.



33
34
35
# File 'lib/vendor/puppet/network/http_server/mongrel.rb', line 33

def xmlrpc_server
  @xmlrpc_server
end

Instance Method Details

#process(request, response) ⇒ Object

This method produces the same results as XMLRPC::CGIServer.serve from Ruby’s stdlib XMLRPC implementation.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vendor/puppet/network/http_server/mongrel.rb', line 55

def process(request, response)
  # Make sure this has been a POST as required for XMLRPC.
  request_method = request.params[Mongrel::Const::REQUEST_METHOD] || Mongrel::Const::GET
  if request_method != "POST"
    response.start(405) { |head, out| out.write("Method Not Allowed") }
    return
  end

  # Make sure the user has sent text/xml data.
  request_mime = request.params["CONTENT_TYPE"] || "text/plain"
  if parse_content_type(request_mime).first != "text/xml"
    response.start(400) { |head, out| out.write("Bad Request") }
    return
  end

  # Make sure there is data in the body at all.
  length = request.params[Mongrel::Const::CONTENT_LENGTH].to_i
  if length <= 0
    response.start(411) { |head, out| out.write("Length Required") }
    return
  end

  # Check the body to be valid.
  if request.body.nil? or request.body.size != length
    response.start(400) { |head, out| out.write("Bad Request") }
    return
  end

  info = client_info(request)

  # All checks above passed through
  response.start(200) do |head, out|
    head["Content-Type"] = "text/xml; charset=utf-8"
    begin
      out.write(@xmlrpc_server.process(request.body, info))
    rescue => detail
      puts detail.backtrace
      raise
    end
  end
end