mirror of
https://github.com/coredns/coredns.git
synced 2025-11-02 10:13:14 -05:00
Fix max-age in http server (#1890)
* Fix max-age in http server Move the minMsgTTL to dnsutil and rename it MinimalTTL, move some constants there as well. Use these new function in server_https to correctly set the max-age HTTP header. Fixes: #1823 * Linter
This commit is contained in:
8
plugin/cache/cache.go
vendored
8
plugin/cache/cache.go
vendored
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/pkg/cache"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/plugin/pkg/response"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
@@ -158,7 +159,7 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
|
||||
duration = w.nttl
|
||||
}
|
||||
|
||||
msgTTL := minMsgTTL(res, mt)
|
||||
msgTTL := dnsutil.MinimalTTL(res, mt)
|
||||
if msgTTL < duration {
|
||||
duration = msgTTL
|
||||
}
|
||||
@@ -226,9 +227,8 @@ func (w *ResponseWriter) Write(buf []byte) (int, error) {
|
||||
}
|
||||
|
||||
const (
|
||||
maxTTL = 1 * time.Hour
|
||||
maxNTTL = 30 * time.Minute
|
||||
failSafeTTL = 5 * time.Second
|
||||
maxTTL = dnsutil.MaximumDefaulTTL
|
||||
maxNTTL = dnsutil.MaximumDefaulTTL / 2
|
||||
|
||||
defaultCap = 10000 // default capacity of the cache.
|
||||
|
||||
|
||||
56
plugin/cache/item.go
vendored
56
plugin/cache/item.go
vendored
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin/cache/freq"
|
||||
"github.com/coredns/coredns/plugin/pkg/response"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
@@ -87,58 +86,3 @@ func (i *item) ttl(now time.Time) int {
|
||||
ttl := int(i.origTTL) - int(now.UTC().Sub(i.stored).Seconds())
|
||||
return ttl
|
||||
}
|
||||
|
||||
func minMsgTTL(m *dns.Msg, mt response.Type) time.Duration {
|
||||
if mt != response.NoError && mt != response.NameError && mt != response.NoData {
|
||||
return 0
|
||||
}
|
||||
|
||||
// No data to examine, return a short ttl as a fail safe.
|
||||
if len(m.Answer)+len(m.Ns)+len(m.Extra) == 0 {
|
||||
return failSafeTTL
|
||||
}
|
||||
|
||||
minTTL := maxTTL
|
||||
for _, r := range m.Answer {
|
||||
switch mt {
|
||||
case response.NameError, response.NoData:
|
||||
if r.Header().Rrtype == dns.TypeSOA {
|
||||
minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
|
||||
}
|
||||
case response.NoError, response.Delegation:
|
||||
if r.Header().Ttl < uint32(minTTL.Seconds()) {
|
||||
minTTL = time.Duration(r.Header().Ttl) * time.Second
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, r := range m.Ns {
|
||||
switch mt {
|
||||
case response.NameError, response.NoData:
|
||||
if r.Header().Rrtype == dns.TypeSOA {
|
||||
minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
|
||||
}
|
||||
case response.NoError, response.Delegation:
|
||||
if r.Header().Ttl < uint32(minTTL.Seconds()) {
|
||||
minTTL = time.Duration(r.Header().Ttl) * time.Second
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, r := range m.Extra {
|
||||
if r.Header().Rrtype == dns.TypeOPT {
|
||||
// OPT records use TTL field for extended rcode and flags
|
||||
continue
|
||||
}
|
||||
switch mt {
|
||||
case response.NameError, response.NoData:
|
||||
if r.Header().Rrtype == dns.TypeSOA {
|
||||
minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
|
||||
}
|
||||
case response.NoError, response.Delegation:
|
||||
if r.Header().Ttl < uint32(minTTL.Seconds()) {
|
||||
minTTL = time.Duration(r.Header().Ttl) * time.Second
|
||||
}
|
||||
}
|
||||
}
|
||||
return minTTL
|
||||
}
|
||||
|
||||
72
plugin/cache/minttl_test.go
vendored
72
plugin/cache/minttl_test.go
vendored
@@ -1,72 +0,0 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin/pkg/response"
|
||||
"github.com/coredns/coredns/plugin/test"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// See https://github.com/kubernetes/dns/issues/121, add some specific tests for those use cases.
|
||||
|
||||
func TestMinMsgTTL(t *testing.T) {
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion("z.alm.im.", dns.TypeA)
|
||||
m.Ns = []dns.RR{
|
||||
test.SOA("alm.im. 1800 IN SOA ivan.ns.cloudflare.com. dns.cloudflare.com. 2025042470 10000 2400 604800 3600"),
|
||||
}
|
||||
|
||||
utc := time.Now().UTC()
|
||||
|
||||
mt, _ := response.Typify(m, utc)
|
||||
if mt != response.NoData {
|
||||
t.Fatalf("Expected type to be response.NoData, got %s", mt)
|
||||
}
|
||||
dur := minMsgTTL(m, mt) // minTTL on msg is 3600 (neg. ttl on SOA)
|
||||
if dur != time.Duration(3600*time.Second) {
|
||||
t.Fatalf("Expected minttl duration to be %d, got %d", 3600, dur)
|
||||
}
|
||||
|
||||
m.Rcode = dns.RcodeNameError
|
||||
mt, _ = response.Typify(m, utc)
|
||||
if mt != response.NameError {
|
||||
t.Fatalf("Expected type to be response.NameError, got %s", mt)
|
||||
}
|
||||
dur = minMsgTTL(m, mt) // minTTL on msg is 3600 (neg. ttl on SOA)
|
||||
if dur != time.Duration(3600*time.Second) {
|
||||
t.Fatalf("Expected minttl duration to be %d, got %d", 3600, dur)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMinMsgTTL(b *testing.B) {
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion("example.org.", dns.TypeA)
|
||||
m.Ns = []dns.RR{
|
||||
test.A("a.example.org. 1800 IN A 127.0.0.53"),
|
||||
test.A("b.example.org. 1900 IN A 127.0.0.53"),
|
||||
test.A("c.example.org. 1600 IN A 127.0.0.53"),
|
||||
test.A("d.example.org. 1100 IN A 127.0.0.53"),
|
||||
test.A("e.example.org. 1000 IN A 127.0.0.53"),
|
||||
}
|
||||
m.Extra = []dns.RR{
|
||||
test.A("a.example.org. 1800 IN A 127.0.0.53"),
|
||||
test.A("b.example.org. 1600 IN A 127.0.0.53"),
|
||||
test.A("c.example.org. 1400 IN A 127.0.0.53"),
|
||||
test.A("d.example.org. 1200 IN A 127.0.0.53"),
|
||||
test.A("e.example.org. 1100 IN A 127.0.0.53"),
|
||||
}
|
||||
|
||||
utc := time.Now().UTC()
|
||||
mt, _ := response.Typify(m, utc)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
dur := minMsgTTL(m, mt)
|
||||
if dur != 1000*time.Second {
|
||||
b.Fatalf("Wrong minMsgTTL %d, expected %d", dur, 1000*time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user