From 23fcda1c47db663ab8b809dc1dcae2726709dcc5 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 27 Sep 2009 19:18:19 -0700 Subject: Use Configurator#expand_addr in HttpServer#listen This may be redundant for the "normal" configuration file directive, but allows the same syntax to be used in after_fork hooks where HttpServer#listen() may be called. --- lib/unicorn.rb | 1 + lib/unicorn/configurator.rb | 48 ++++++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/unicorn.rb b/lib/unicorn.rb index bc5ba23..392d301 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -182,6 +182,7 @@ module Unicorn # retried indefinitely, this is useful when workers belonging to # different masters are spawned during a transparent upgrade. def listen(address, opt = {}.merge(listener_opts[address] || {})) + address = config.expand_addr(address) return if String === address && listener_names.include?(address) delay = opt[:delay] || 0.5 diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb index 523765a..114f4fb 100644 --- a/lib/unicorn/configurator.rb +++ b/lib/unicorn/configurator.rb @@ -294,7 +294,30 @@ module Unicorn set_path(:stdout_path, path) end - private + # expands "unix:path/to/foo" to a socket relative to the current path + # expands pathnames of sockets if relative to "~" or "~username" + # expands "*:port and ":port" to "0.0.0.0:port" + def expand_addr(address) #:nodoc + return "0.0.0.0:#{address}" if Integer === address + return address unless String === address + + case address + when %r{\Aunix:(.*)\z} + File.expand_path($1) + when %r{\A~} + File.expand_path(address) + when %r{\A(?:\*:)?(\d+)\z} + "0.0.0.0:#$1" + when %r{\A(.*):(\d+)\z} + # canonicalize the name + packed = Socket.pack_sockaddr_in($2.to_i, $1) + Socket.unpack_sockaddr_in(packed).reverse!.join(':') + else + address + end + end + + private def set_path(var, path) #:nodoc: case path @@ -325,28 +348,5 @@ module Unicorn set[var] = my_proc end - # expands "unix:path/to/foo" to a socket relative to the current path - # expands pathnames of sockets if relative to "~" or "~username" - # expands "*:port and ":port" to "0.0.0.0:port" - def expand_addr(address) #:nodoc - return "0.0.0.0:#{address}" if Integer === address - return address unless String === address - - case address - when %r{\Aunix:(.*)\z} - File.expand_path($1) - when %r{\A~} - File.expand_path(address) - when %r{\A(?:\*:)?(\d+)\z} - "0.0.0.0:#$1" - when %r{\A(.*):(\d+)\z} - # canonicalize the name - packed = Socket.pack_sockaddr_in($2.to_i, $1) - Socket.unpack_sockaddr_in(packed).reverse!.join(':') - else - address - end - end - end end -- cgit v1.2.3-24-ge0c7