mirror of
https://github.com/coredns/coredns.git
synced 2025-11-02 02:03:13 -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:
72
plugin/pkg/dnsutil/ttl.go
Normal file
72
plugin/pkg/dnsutil/ttl.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package dnsutil
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin/pkg/response"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// MinimalTTL scans the message returns the lowest TTL found taking into the response.Type of the message.
|
||||
func MinimalTTL(m *dns.Msg, mt response.Type) time.Duration {
|
||||
if mt != response.NoError && mt != response.NameError && mt != response.NoData {
|
||||
return MinimalDefaultTTL
|
||||
}
|
||||
|
||||
// No data to examine, return a short ttl as a fail safe.
|
||||
if len(m.Answer)+len(m.Ns)+len(m.Extra) == 0 {
|
||||
return MinimalDefaultTTL
|
||||
}
|
||||
|
||||
minTTL := MaximumDefaulTTL
|
||||
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
|
||||
}
|
||||
|
||||
const (
|
||||
// MinimalDefaultTTL is the absolute lowest TTL we use in CoreDNS.
|
||||
MinimalDefaultTTL = 5 * time.Second
|
||||
// MaximumDefaulTTL is the maximum TTL was use on RRsets in CoreDNS.
|
||||
MaximumDefaulTTL = 1 * time.Hour
|
||||
)
|
||||
72
plugin/pkg/dnsutil/ttl_test.go
Normal file
72
plugin/pkg/dnsutil/ttl_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package dnsutil
|
||||
|
||||
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 TestMinimalTTL(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 := MinimalTTL(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 = MinimalTTL(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 BenchmarkMinimalTTL(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 := MinimalTTL(m, mt)
|
||||
if dur != 1000*time.Second {
|
||||
b.Fatalf("Wrong MinimalTTL %d, expected %d", dur, 1000*time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user