summaryrefslogtreecommitdiff
path: root/lib/chef/monkey_patches/webrick-utils.rb
blob: ff275a434d7cb7abe2554a9f473e984f9f6f7981 (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
require "webrick/utils"

module WEBrick
  module Utils
    ##
    # Creates TCP server sockets bound to +address+:+port+ and returns them.
    #
    # It will create IPV4 and IPV6 sockets on all interfaces.
    #
    # NOTE: We need to monkey patch this method because
    # create_listeners on Windows with Ruby > 2.0.0 does not
    # raise an error if we're already listening on a port.
    #
    def create_listeners(address, port, logger = nil)
      #
      # utils.rb -- Miscellaneous utilities
      #
      # Author: IPR -- Internet Programming with Ruby -- writers
      # Copyright 2001-2016, TAKAHASHI Masayoshi, GOTOU Yuuzou
      # Copyright 2002-2016, Internet Programming with Ruby writers. All rights
      # reserved.
      #
      # $IPR: utils.rb,v 1.10 2003/02/16 22:22:54 gotoyuzo Exp $
      unless port
        raise ArgumentError, "must specify port"
      end
      res = Socket::getaddrinfo(address, port,
                                Socket::AF_UNSPEC,   # address family
                                Socket::SOCK_STREAM, # socket type
                                0,                   # protocol
                                Socket::AI_PASSIVE)  # flag
      last_error = nil
      sockets = []
      res.each{|ai|
        begin
          logger.debug("TCPServer.new(#{ai[3]}, #{port})") if logger
          sock = TCPServer.new(ai[3], port)
          port = sock.addr[1] if port == 0
          Utils::set_close_on_exec(sock)
          sockets << sock
        rescue => ex
          logger.warn("TCPServer Error: #{ex}") if logger
          last_error = ex
        end
      }
      raise last_error if sockets.empty?
      return sockets
    end
    module_function :create_listeners
  end
end