Add Up method

This commit is contained in:
Miek Gieben 2012-08-05 07:43:13 +02:00
parent 3e33a3cb3a
commit 4668fcfee4
1 changed files with 12 additions and 46 deletions

View File

@ -9,7 +9,7 @@ package dns
import ( import (
"io" "io"
"net" "net"
"strings" "radix"
"time" "time"
) )
@ -51,11 +51,11 @@ type response struct {
// registered patterns add calls the handler for the pattern // registered patterns add calls the handler for the pattern
// that most closely matches the zone name. // that most closely matches the zone name.
type ServeMux struct { type ServeMux struct {
m map[string]Handler m *radix.Radix
} }
// NewServeMux allocates and returns a new ServeMux. // NewServeMux allocates and returns a new ServeMux.
func NewServeMux() *ServeMux { return &ServeMux{make(map[string]Handler)} } func NewServeMux() *ServeMux { return &ServeMux{m: radix.New()} }
// DefaultServeMux is the default ServeMux used by Serve. // DefaultServeMux is the default ServeMux used by Serve.
var DefaultServeMux = NewServeMux() var DefaultServeMux = NewServeMux()
@ -97,50 +97,16 @@ func ListenAndServeTsig(addr string, network string, handler Handler, tsig map[s
return server.ListenAndServe() return server.ListenAndServe()
} }
// Maybe use a radix tree here too...?
func (mux *ServeMux) match(zone string, t uint16) Handler { func (mux *ServeMux) match(zone string, t uint16) Handler {
var h Handler // Exact match
var n = 0 if h := mux.m.Find(zone); h.Value != nil {
for k, v := range mux.m { return h.Value.(Handler)
if !zoneMatch(k, zone) {
continue
}
if h == nil || len(k) > n {
n = len(k)
h = v
}
} }
// Zone has been found // Best matching
if t != TypeDS { if h := mux.m.Predecessor(zone); h.Value != nil {
return h return h.Value.(Handler)
} }
// Uberhack: if we are matching DS records, we chop of the return nil
// first label. This way we will not match the zone
// but the first parent.
// Check if we also are authoritative for the parent
// TODO(mg): second time we 'range' the map
// TODO(mg): root check
var p Handler
xs := SplitLabels(zone)
zone = Fqdn(strings.Join(xs[1:], "."))
println(zone)
n = 0
for k, v := range mux.m {
if !zoneMatch(k, zone) {
continue
}
if p == nil || len(k) > n {
println("setting", string(k))
n = len(k)
p = v
}
}
if p != nil {
println("returning p")
return p
}
return h
} }
// Handle adds a handler to the ServeMux for pattern. // Handle adds a handler to the ServeMux for pattern.
@ -148,7 +114,7 @@ func (mux *ServeMux) Handle(pattern string, handler Handler) {
if pattern == "" { if pattern == "" {
panic("dns: invalid pattern " + pattern) panic("dns: invalid pattern " + pattern)
} }
mux.m[Fqdn(pattern)] = handler mux.m.Insert(Fqdn(pattern), handler)
} }
// Handle adds a handler to the ServeMux for pattern. // Handle adds a handler to the ServeMux for pattern.
@ -162,7 +128,7 @@ func (mux *ServeMux) HandleRemove(pattern string) {
panic("dns: invalid pattern " + pattern) panic("dns: invalid pattern " + pattern)
} }
// if its there, its gone // if its there, its gone
delete(mux.m, Fqdn(pattern)) mux.m.Remove(Fqdn(pattern))
} }
// ServeDNS dispatches the request to the handler whose // ServeDNS dispatches the request to the handler whose