diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2019-12-06 11:45:50 -0700 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2019-12-06 11:45:50 -0700 |
commit | 33a318d17341fcaa95551710620540e00b0bb4b0 (patch) | |
tree | 5e56b998747e32f3636a9f6a11a992009137af0c | |
parent | 68adfdc559e3a97f19ec85ec2e1e5d3ceae20161 (diff) |
Don't append port to unix sockets
See https://caddy.community/t/caddy-v2-php-fpm-502-error/6571?u=matt
-rw-r--r-- | listeners.go | 15 | ||||
-rw-r--r-- | listeners_test.go | 49 |
2 files changed, 63 insertions, 1 deletions
diff --git a/listeners.go b/listeners.go index 4644642..e52561d 100644 --- a/listeners.go +++ b/listeners.go @@ -266,9 +266,18 @@ type ParsedAddress struct { EndPort uint } +// IsUnixNetwork returns true if pa.Network is +// unix, unixgram, or unixpacket. +func (pa ParsedAddress) IsUnixNetwork() bool { + return isUnixNetwork(pa.Network) +} + // JoinHostPort is like net.JoinHostPort, but where the port // is StartPort + offset. func (pa ParsedAddress) JoinHostPort(offset uint) string { + if pa.IsUnixNetwork() { + return pa.Host + } return net.JoinHostPort(pa.Host, strconv.Itoa(int(pa.StartPort+offset))) } @@ -290,6 +299,10 @@ func (pa ParsedAddress) String() string { return JoinNetworkAddress(pa.Network, pa.Host, port) } +func isUnixNetwork(netw string) bool { + return netw == "unix" || netw == "unixgram" || netw == "unixpacket" +} + // ParseNetworkAddress parses addr into its individual // components. The input string is expected to be of // the form "network/host:port-range" where any part is @@ -307,7 +320,7 @@ func ParseNetworkAddress(addr string) (ParsedAddress, error) { if err != nil { return ParsedAddress{}, err } - if network == "unix" || network == "unixgram" || network == "unixpacket" { + if isUnixNetwork(network) { return ParsedAddress{ Network: network, Host: host, diff --git a/listeners_test.go b/listeners_test.go index 076b365..25e1394 100644 --- a/listeners_test.go +++ b/listeners_test.go @@ -250,3 +250,52 @@ func TestParseNetworkAddress(t *testing.T) { } } } + +func TestJoinHostPort(t *testing.T) { + for i, tc := range []struct { + pa ParsedAddress + offset uint + expect string + }{ + { + pa: ParsedAddress{ + Network: "tcp", + Host: "localhost", + StartPort: 1234, + EndPort: 1234, + }, + expect: "localhost:1234", + }, + { + pa: ParsedAddress{ + Network: "tcp", + Host: "localhost", + StartPort: 1234, + EndPort: 1235, + }, + expect: "localhost:1234", + }, + { + pa: ParsedAddress{ + Network: "tcp", + Host: "localhost", + StartPort: 1234, + EndPort: 1235, + }, + offset: 1, + expect: "localhost:1235", + }, + { + pa: ParsedAddress{ + Network: "unix", + Host: "/run/php/php7.3-fpm.sock", + }, + expect: "/run/php/php7.3-fpm.sock", + }, + } { + actual := tc.pa.JoinHostPort(tc.offset) + if actual != tc.expect { + t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual) + } + } +} |