core: Rename ParsedAddress -> NetworkAddress

This commit is contained in:
Matthew Holt 2020-04-07 08:33:45 -06:00
parent 657f0cab17
commit 8b2dbc52ec
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
6 changed files with 42 additions and 42 deletions

View file

@ -254,49 +254,49 @@ type globalListener struct {
pc net.PacketConn pc net.PacketConn
} }
// ParsedAddress contains the individual components // NetworkAddress contains the individual components
// for a parsed network address of the form accepted // for a parsed network address of the form accepted
// by ParseNetworkAddress(). Network should be a // by ParseNetworkAddress(). Network should be a
// network value accepted by Go's net package. Port // network value accepted by Go's net package. Port
// ranges are given by [StartPort, EndPort]. // ranges are given by [StartPort, EndPort].
type ParsedAddress struct { type NetworkAddress struct {
Network string Network string
Host string Host string
StartPort uint StartPort uint
EndPort uint EndPort uint
} }
// IsUnixNetwork returns true if pa.Network is // IsUnixNetwork returns true if na.Network is
// unix, unixgram, or unixpacket. // unix, unixgram, or unixpacket.
func (pa ParsedAddress) IsUnixNetwork() bool { func (na NetworkAddress) IsUnixNetwork() bool {
return isUnixNetwork(pa.Network) return isUnixNetwork(na.Network)
} }
// JoinHostPort is like net.JoinHostPort, but where the port // JoinHostPort is like net.JoinHostPort, but where the port
// is StartPort + offset. // is StartPort + offset.
func (pa ParsedAddress) JoinHostPort(offset uint) string { func (na NetworkAddress) JoinHostPort(offset uint) string {
if pa.IsUnixNetwork() { if na.IsUnixNetwork() {
return pa.Host return na.Host
} }
return net.JoinHostPort(pa.Host, strconv.Itoa(int(pa.StartPort+offset))) return net.JoinHostPort(na.Host, strconv.Itoa(int(na.StartPort+offset)))
} }
// PortRangeSize returns how many ports are in // PortRangeSize returns how many ports are in
// pa's port range. Port ranges are inclusive, // pa's port range. Port ranges are inclusive,
// so the size is the difference of start and // so the size is the difference of start and
// end ports plus one. // end ports plus one.
func (pa ParsedAddress) PortRangeSize() uint { func (na NetworkAddress) PortRangeSize() uint {
return (pa.EndPort - pa.StartPort) + 1 return (na.EndPort - na.StartPort) + 1
} }
// String reconstructs the address string to the form expected // String reconstructs the address string to the form expected
// by ParseNetworkAddress(). // by ParseNetworkAddress().
func (pa ParsedAddress) String() string { func (na NetworkAddress) String() string {
port := strconv.FormatUint(uint64(pa.StartPort), 10) port := strconv.FormatUint(uint64(na.StartPort), 10)
if pa.StartPort != pa.EndPort { if na.StartPort != na.EndPort {
port += "-" + strconv.FormatUint(uint64(pa.EndPort), 10) port += "-" + strconv.FormatUint(uint64(na.EndPort), 10)
} }
return JoinNetworkAddress(pa.Network, pa.Host, port) return JoinNetworkAddress(na.Network, na.Host, port)
} }
func isUnixNetwork(netw string) bool { func isUnixNetwork(netw string) bool {
@ -311,17 +311,17 @@ func isUnixNetwork(netw string) bool {
// //
// Network addresses are distinct from URLs and do not // Network addresses are distinct from URLs and do not
// use URL syntax. // use URL syntax.
func ParseNetworkAddress(addr string) (ParsedAddress, error) { func ParseNetworkAddress(addr string) (NetworkAddress, error) {
var host, port string var host, port string
network, host, port, err := SplitNetworkAddress(addr) network, host, port, err := SplitNetworkAddress(addr)
if network == "" { if network == "" {
network = "tcp" network = "tcp"
} }
if err != nil { if err != nil {
return ParsedAddress{}, err return NetworkAddress{}, err
} }
if isUnixNetwork(network) { if isUnixNetwork(network) {
return ParsedAddress{ return NetworkAddress{
Network: network, Network: network,
Host: host, Host: host,
}, nil }, nil
@ -333,19 +333,19 @@ func ParseNetworkAddress(addr string) (ParsedAddress, error) {
var start, end uint64 var start, end uint64
start, err = strconv.ParseUint(ports[0], 10, 16) start, err = strconv.ParseUint(ports[0], 10, 16)
if err != nil { if err != nil {
return ParsedAddress{}, fmt.Errorf("invalid start port: %v", err) return NetworkAddress{}, fmt.Errorf("invalid start port: %v", err)
} }
end, err = strconv.ParseUint(ports[1], 10, 16) end, err = strconv.ParseUint(ports[1], 10, 16)
if err != nil { if err != nil {
return ParsedAddress{}, fmt.Errorf("invalid end port: %v", err) return NetworkAddress{}, fmt.Errorf("invalid end port: %v", err)
} }
if end < start { if end < start {
return ParsedAddress{}, fmt.Errorf("end port must not be less than start port") return NetworkAddress{}, fmt.Errorf("end port must not be less than start port")
} }
if (end - start) > maxPortSpan { if (end - start) > maxPortSpan {
return ParsedAddress{}, fmt.Errorf("port range exceeds %d ports", maxPortSpan) return NetworkAddress{}, fmt.Errorf("port range exceeds %d ports", maxPortSpan)
} }
return ParsedAddress{ return NetworkAddress{
Network: network, Network: network,
Host: host, Host: host,
StartPort: uint(start), StartPort: uint(start),

View file

@ -153,7 +153,7 @@ func TestJoinNetworkAddress(t *testing.T) {
func TestParseNetworkAddress(t *testing.T) { func TestParseNetworkAddress(t *testing.T) {
for i, tc := range []struct { for i, tc := range []struct {
input string input string
expectAddr ParsedAddress expectAddr NetworkAddress
expectErr bool expectErr bool
}{ }{
{ {
@ -166,7 +166,7 @@ func TestParseNetworkAddress(t *testing.T) {
}, },
{ {
input: ":1234", input: ":1234",
expectAddr: ParsedAddress{ expectAddr: NetworkAddress{
Network: "tcp", Network: "tcp",
Host: "", Host: "",
StartPort: 1234, StartPort: 1234,
@ -175,7 +175,7 @@ func TestParseNetworkAddress(t *testing.T) {
}, },
{ {
input: "tcp/:1234", input: "tcp/:1234",
expectAddr: ParsedAddress{ expectAddr: NetworkAddress{
Network: "tcp", Network: "tcp",
Host: "", Host: "",
StartPort: 1234, StartPort: 1234,
@ -184,7 +184,7 @@ func TestParseNetworkAddress(t *testing.T) {
}, },
{ {
input: "tcp6/:1234", input: "tcp6/:1234",
expectAddr: ParsedAddress{ expectAddr: NetworkAddress{
Network: "tcp6", Network: "tcp6",
Host: "", Host: "",
StartPort: 1234, StartPort: 1234,
@ -193,7 +193,7 @@ func TestParseNetworkAddress(t *testing.T) {
}, },
{ {
input: "tcp4/localhost:1234", input: "tcp4/localhost:1234",
expectAddr: ParsedAddress{ expectAddr: NetworkAddress{
Network: "tcp4", Network: "tcp4",
Host: "localhost", Host: "localhost",
StartPort: 1234, StartPort: 1234,
@ -202,14 +202,14 @@ func TestParseNetworkAddress(t *testing.T) {
}, },
{ {
input: "unix//foo/bar", input: "unix//foo/bar",
expectAddr: ParsedAddress{ expectAddr: NetworkAddress{
Network: "unix", Network: "unix",
Host: "/foo/bar", Host: "/foo/bar",
}, },
}, },
{ {
input: "localhost:1234-1234", input: "localhost:1234-1234",
expectAddr: ParsedAddress{ expectAddr: NetworkAddress{
Network: "tcp", Network: "tcp",
Host: "localhost", Host: "localhost",
StartPort: 1234, StartPort: 1234,
@ -222,7 +222,7 @@ func TestParseNetworkAddress(t *testing.T) {
}, },
{ {
input: "localhost:0", input: "localhost:0",
expectAddr: ParsedAddress{ expectAddr: NetworkAddress{
Network: "tcp", Network: "tcp",
Host: "localhost", Host: "localhost",
StartPort: 0, StartPort: 0,
@ -253,12 +253,12 @@ func TestParseNetworkAddress(t *testing.T) {
func TestJoinHostPort(t *testing.T) { func TestJoinHostPort(t *testing.T) {
for i, tc := range []struct { for i, tc := range []struct {
pa ParsedAddress pa NetworkAddress
offset uint offset uint
expect string expect string
}{ }{
{ {
pa: ParsedAddress{ pa: NetworkAddress{
Network: "tcp", Network: "tcp",
Host: "localhost", Host: "localhost",
StartPort: 1234, StartPort: 1234,
@ -267,7 +267,7 @@ func TestJoinHostPort(t *testing.T) {
expect: "localhost:1234", expect: "localhost:1234",
}, },
{ {
pa: ParsedAddress{ pa: NetworkAddress{
Network: "tcp", Network: "tcp",
Host: "localhost", Host: "localhost",
StartPort: 1234, StartPort: 1234,
@ -276,7 +276,7 @@ func TestJoinHostPort(t *testing.T) {
expect: "localhost:1234", expect: "localhost:1234",
}, },
{ {
pa: ParsedAddress{ pa: NetworkAddress{
Network: "tcp", Network: "tcp",
Host: "localhost", Host: "localhost",
StartPort: 1234, StartPort: 1234,
@ -286,7 +286,7 @@ func TestJoinHostPort(t *testing.T) {
expect: "localhost:1235", expect: "localhost:1235",
}, },
{ {
pa: ParsedAddress{ pa: NetworkAddress{
Network: "unix", Network: "unix",
Host: "/run/php/php7.3-fpm.sock", Host: "/run/php/php7.3-fpm.sock",
}, },

View file

@ -82,7 +82,7 @@ func (app *App) automaticHTTPSPhase1(ctx caddy.Context, repl *caddy.Replacer) er
// this maps domain names for automatic HTTP->HTTPS // this maps domain names for automatic HTTP->HTTPS
// redirects to their destination server address // redirects to their destination server address
redirDomains := make(map[string]caddy.ParsedAddress) redirDomains := make(map[string]caddy.NetworkAddress)
for srvName, srv := range app.Servers { for srvName, srv := range app.Servers {
// as a prerequisite, provision route matchers; this is // as a prerequisite, provision route matchers; this is

View file

@ -138,7 +138,7 @@ func (u *Upstream) Full() bool {
// field is used. Note that the returned value is not a pointer. // field is used. Note that the returned value is not a pointer.
func (u *Upstream) fillDialInfo(r *http.Request) (DialInfo, error) { func (u *Upstream) fillDialInfo(r *http.Request) (DialInfo, error) {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
var addr caddy.ParsedAddress var addr caddy.NetworkAddress
if u.LookupSRV != "" { if u.LookupSRV != "" {
// perform DNS lookup for SRV records and choose one // perform DNS lookup for SRV records and choose one

View file

@ -370,12 +370,12 @@ func (s HeaderHashSelection) Select(pool UpstreamPool, req *http.Request) *Upstr
} }
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. // UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *HeaderHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { func (s *HeaderHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() { for d.Next() {
if !d.NextArg() { if !d.NextArg() {
return d.ArgErr() return d.ArgErr()
} }
r.Field = d.Val() s.Field = d.Val()
} }
return nil return nil
} }

View file

@ -31,7 +31,7 @@ func init() {
type NetWriter struct { type NetWriter struct {
Address string `json:"address,omitempty"` Address string `json:"address,omitempty"`
addr caddy.ParsedAddress addr caddy.NetworkAddress
} }
// CaddyModule returns the Caddy module information. // CaddyModule returns the Caddy module information.