core: small cleanup (#877)

Add some docs about normalize.Host and normalize.Name. They are used
correctly in the middleware even though they are somewhat confusing,
esp when you copy from ServerBlockKeys in your middleware.
This commit is contained in:
Miek Gieben 2017-08-10 05:30:18 -07:00 committed by GitHub
parent 28447234b1
commit 3654361be2
7 changed files with 326 additions and 341 deletions

View file

@ -81,7 +81,6 @@ func autoParse(c *caddy.Controller) (Auto, error) {
config := dnsserver.GetConfig(c) config := dnsserver.GetConfig(c)
for c.Next() { for c.Next() {
if c.Val() == "auto" {
// auto [ZONES...] // auto [ZONES...]
a.Zones.origins = make([]string, len(c.ServerBlockKeys)) a.Zones.origins = make([]string, len(c.ServerBlockKeys))
copy(a.Zones.origins, c.ServerBlockKeys) copy(a.Zones.origins, c.ServerBlockKeys)
@ -165,8 +164,6 @@ func autoParse(c *caddy.Controller) (Auto, error) {
} }
} }
} }
}
} }
return a, nil return a, nil
} }

View file

@ -42,7 +42,6 @@ func dnssecParse(c *caddy.Controller) ([]string, []*DNSKEY, int, error) {
capacity := defaultCap capacity := defaultCap
for c.Next() { for c.Next() {
if c.Val() == "dnssec" {
// dnssec [zones...] // dnssec [zones...]
zones = make([]string, len(c.ServerBlockKeys)) zones = make([]string, len(c.ServerBlockKeys))
copy(zones, c.ServerBlockKeys) copy(zones, c.ServerBlockKeys)
@ -73,7 +72,6 @@ func dnssecParse(c *caddy.Controller) ([]string, []*DNSKEY, int, error) {
} }
} }
}
for i := range zones { for i := range zones {
zones[i] = middleware.Host(zones[i]).Normalize() zones[i] = middleware.Host(zones[i]).Normalize()
} }

View file

@ -60,7 +60,6 @@ func etcdParse(c *caddy.Controller) (*Etcd, bool, error) {
stubzones = false stubzones = false
) )
for c.Next() { for c.Next() {
if c.Val() == "etcd" {
etc.Zones = c.RemainingArgs() etc.Zones = c.RemainingArgs()
if len(etc.Zones) == 0 { if len(etc.Zones) == 0 {
etc.Zones = make([]string, len(c.ServerBlockKeys)) etc.Zones = make([]string, len(c.ServerBlockKeys))
@ -125,7 +124,6 @@ func etcdParse(c *caddy.Controller) (*Etcd, bool, error) {
return &etc, stubzones, nil return &etc, stubzones, nil
} }
}
return &Etcd{}, false, nil return &Etcd{}, false, nil
} }

View file

@ -55,7 +55,6 @@ func fileParse(c *caddy.Controller) (Zones, error) {
config := dnsserver.GetConfig(c) config := dnsserver.GetConfig(c)
for c.Next() { for c.Next() {
if c.Val() == "file" {
// file db.file [zones...] // file db.file [zones...]
if !c.NextArg() { if !c.NextArg() {
return Zones{}, c.ArgErr() return Zones{}, c.ArgErr()
@ -127,7 +126,6 @@ func fileParse(c *caddy.Controller) (Zones, error) {
} }
} }
} }
}
return Zones{Z: z, Names: names}, nil return Zones{Z: z, Names: names}, nil
} }

View file

@ -41,7 +41,6 @@ func hostsParse(c *caddy.Controller) (Hosts, error) {
config := dnsserver.GetConfig(c) config := dnsserver.GetConfig(c)
for c.Next() { for c.Next() {
if c.Val() == "hosts" { // hosts [FILE] [ZONES...]
args := c.RemainingArgs() args := c.RemainingArgs()
if len(args) >= 1 { if len(args) >= 1 {
h.path = args[0] h.path = args[0]
@ -83,6 +82,5 @@ func hostsParse(c *caddy.Controller) (Hosts, error) {
} }
} }
} }
}
return h, nil return h, nil
} }

View file

@ -30,7 +30,8 @@ func (z Zones) Matches(qname string) string {
return zone return zone
} }
// Normalize fully qualifies all zones in z. // Normalize fully qualifies all zones in z. The zones in Z must be domain names, without
// a port or protocol prefix.
func (z Zones) Normalize() { func (z Zones) Normalize() {
for i := range z { for i := range z {
z[i] = Name(z[i]).Normalize() z[i] = Name(z[i]).Normalize()
@ -54,7 +55,7 @@ func (n Name) Normalize() string { return strings.ToLower(dns.Fqdn(string(n))) }
type ( type (
// Host represents a host from the Corefile, may contain port. // Host represents a host from the Corefile, may contain port.
Host string // Host represents a host from the Corefile, may contain port. Host string
) )
// Normalize will return the host portion of host, stripping // Normalize will return the host portion of host, stripping

View file

@ -34,19 +34,15 @@ func setupReverse(c *caddy.Controller) error {
} }
func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) { func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) {
// normalize zones, validation is almost done by dnsserver
// TODO(miek): need sane helpers for these.
zones := make([]string, len(c.ServerBlockKeys)) zones := make([]string, len(c.ServerBlockKeys))
wildcard := false wildcard := false
// We copy from the serverblock, these contains Hosts.
for i, str := range c.ServerBlockKeys { for i, str := range c.ServerBlockKeys {
zones[i] = middleware.Host(str).Normalize() zones[i] = middleware.Host(str).Normalize()
} }
for c.Next() { for c.Next() {
if c.Val() == "reverse" {
var cidrs []*net.IPNet var cidrs []*net.IPNet
// parse all networks // parse all networks
@ -144,7 +140,6 @@ func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) {
}) })
} }
} }
}
// sort by cidr // sort by cidr
sort.Sort(nets) sort.Sort(nets)