about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-03-31 00:19:47 -0700
committerEric Wong <normalperson@yhbt.net>2009-03-31 00:19:47 -0700
commit1d127ce0b2edcc8b03e4cb1031ff341ff91770fe (patch)
treeaae203a71adc0b0864092f017c79972f66acdf34
parent1bb4366c049a2d1c460cb08601865a24d7678dbe (diff)
downloadunicorn-1d127ce0b2edcc8b03e4cb1031ff341ff91770fe.tar.gz
* Expand addresses like "1:8080" to "127.0.0.1:8080"
  beforehand so sock_name() in SocketHelper will
  always return consistent results.

* Add support for "unix:/path/to/foo" paths for easier
  synchronization with nginx config files.
-rw-r--r--lib/unicorn/configurator.rb20
-rw-r--r--test/unit/test_configurator.rb22
2 files changed, 37 insertions, 5 deletions
diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb
index a69e971..79bbf4b 100644
--- a/lib/unicorn/configurator.rb
+++ b/lib/unicorn/configurator.rb
@@ -286,16 +286,26 @@ 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 address unless String === address
-      if address[0..0] == '~'
-        return File.expand_path(address)
-      elsif address =~ %r{\A\*?:(\d+)\z}
-        return "0.0.0.0:#$1"
+
+      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
-      address
     end
 
   end
diff --git a/test/unit/test_configurator.rb b/test/unit/test_configurator.rb
index 623d717..284d727 100644
--- a/test/unit/test_configurator.rb
+++ b/test/unit/test_configurator.rb
@@ -8,6 +8,28 @@ class TestConfigurator < Test::Unit::TestCase
     assert_nothing_raised { Unicorn::Configurator.new {} }
   end
 
+  def test_expand_addr
+    meth = Unicorn::Configurator.new.method(:expand_addr)
+
+    assert_equal "/var/run/unicorn.sock", meth.call("/var/run/unicorn.sock")
+    assert_equal "#{Dir.pwd}/foo/bar.sock", meth.call("unix:foo/bar.sock")
+
+    path = meth.call("~/foo/bar.sock")
+    assert_equal "/", path[0..0]
+    assert_match %r{/foo/bar\.sock\z}, path
+
+    path = meth.call("~root/foo/bar.sock")
+    assert_equal "/", path[0..0]
+    assert_match %r{/foo/bar\.sock\z}, path
+
+    assert_equal "1.2.3.4:2007", meth.call('1.2.3.4:2007')
+    assert_equal "0.0.0.0:2007", meth.call('0.0.0.0:2007')
+    assert_equal "0.0.0.0:2007", meth.call(':2007')
+    assert_equal "0.0.0.0:2007", meth.call('*:2007')
+    assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('1:2007')
+    assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('2:2007')
+  end
+
   def test_config_invalid
     tmp = Tempfile.new('unicorn_config')
     tmp.syswrite(%q(asdfasdf "hello-world"))