Class: Puppet::Network::HTTP::WEBrick

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/network/http/webrick.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ WEBrick

Returns a new instance of WEBrick.



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

def initialize(args = {})
  @listening = false
  @mutex = Mutex.new
end

Class Method Details

.class_for_protocol(protocol) ⇒ Object



16
17
18
19
# File 'lib/vendor/puppet/network/http/webrick.rb', line 16

def self.class_for_protocol(protocol)
  return Puppet::Network::HTTP::WEBrickREST if protocol.to_sym == :rest
  raise "Unknown protocol [#{protocol}]."
end

Instance Method Details

#listen(args = {}) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vendor/puppet/network/http/webrick.rb', line 21

def listen(args = {})
  raise ArgumentError, ":protocols must be specified." if !args[:protocols] or args[:protocols].empty?
  raise ArgumentError, ":address must be specified." unless args[:address]
  raise ArgumentError, ":port must be specified." unless args[:port]

  @protocols = args[:protocols]
  @xmlrpc_handlers = args[:xmlrpc_handlers]

  arguments = {:BindAddress => args[:address], :Port => args[:port]}
  arguments.merge!(setup_logger)
  arguments.merge!(setup_ssl)

  @server = WEBrick::HTTPServer.new(arguments)
  @server.listeners.each { |l| l.start_immediately = false }

  setup_handlers

  @mutex.synchronize do
    raise "WEBrick server is already listening" if @listening
    @listening = true
    @thread = Thread.new {
      @server.start { |sock|
        raise "Client disconnected before connection could be established" unless IO.select([sock],nil,nil,6.2)
        sock.accept
        @server.run(sock)
      }
    }
    sleep 0.1 until @server.status == :Running
  end
end

#listening?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/vendor/puppet/network/http/webrick.rb', line 62

def listening?
  @mutex.synchronize do
    @listening
  end
end

#setup_loggerObject

Configure our http log file.



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
# File 'lib/vendor/puppet/network/http/webrick.rb', line 69

def setup_logger
  # Make sure the settings are all ready for us.
  Puppet.settings.use(:main, :ssl, Puppet[:name])

  if Puppet.run_mode.master?
    file = Puppet[:masterhttplog]
  else
    file = Puppet[:httplog]
  end

  # open the log manually to prevent file descriptor leak
  file_io = ::File.open(file, "a+")
  file_io.sync = true
  if defined?(Fcntl::FD_CLOEXEC)
    file_io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
  end

  args = [file_io]
  args << WEBrick::Log::DEBUG if Puppet::Util::Log.level == :debug

  logger = WEBrick::Log.new(*args)
  return :Logger => logger, :AccessLog => [
    [logger, WEBrick::AccessLog::COMMON_LOG_FORMAT ],
    [logger, WEBrick::AccessLog::REFERER_LOG_FORMAT ]
  ]
end

#setup_sslObject

Add all of the ssl cert information.

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/vendor/puppet/network/http/webrick.rb', line 97

def setup_ssl
  results = {}

  # Get the cached copy.  We know it's been generated, too.
  host = Puppet::SSL::Host.localhost

  raise Puppet::Error, "Could not retrieve certificate for #{host.name} and not running on a valid certificate authority" unless host.certificate

  results[:SSLPrivateKey] = host.key.content
  results[:SSLCertificate] = host.certificate.content
  results[:SSLStartImmediately] = true
  results[:SSLEnable] = true

  raise Puppet::Error, "Could not find CA certificate" unless Puppet::SSL::Certificate.indirection.find(Puppet::SSL::CA_NAME)

  results[:SSLCACertificateFile] = Puppet[:localcacert]
  results[:SSLVerifyClient] = OpenSSL::SSL::VERIFY_PEER

  results[:SSLCertificateStore] = host.ssl_store

  results
end

#unlistenObject



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

def unlisten
  @mutex.synchronize do
    raise "WEBrick server is not listening" unless @listening
    @server.shutdown
    @thread.join
    @server = nil
    @listening = false
  end
end