blob: 3fd6436123943e1ebeccd3e98a30a78d6dba88cf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
|
require 'webrick'
require 'webrick/https'
require 'puppet/network/http/webrick/rest'
require 'thread'
class Puppet::Network::HTTP::WEBrick
def initialize(args = {})
@listening = false
@mutex = Mutex.new
end
def listen(args = {})
raise ArgumentError, ":handlers must be specified." if !args[:handlers] or args[:handlers].empty?
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]
@handlers = args[:handlers]
@server = WEBrick::HTTPServer.new(:BindAddress => args[:address], :Port => args[:port])
setup_handlers
@mutex.synchronize do
raise "WEBrick server is already listening" if @listening
@listening = true
@thread = Thread.new { @server.start }
end
end
def unlisten
@mutex.synchronize do
raise "WEBrick server is not listening" unless @listening
@server.shutdown
@thread.join
@server = nil
@listening = false
end
end
def listening?
@mutex.synchronize do
@listening
end
end
private
def setup_handlers
@protocols.each do |protocol|
@handlers.each do |handler|
class_for_protocol(protocol).new(:server => @server, :handler => handler)
end
end
end
def class_for_protocol(protocol)
return Puppet::Network::HTTP::WEBrickREST if protocol.to_sym == :rest
raise ArgumentError, "Unknown protocol [#{protocol}]."
end
end
|