aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/dnsutil/ttl.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2018-06-27 21:12:27 +0100
committerGravatar GitHub <noreply@github.com> 2018-06-27 21:12:27 +0100
commitdae506b5638c7309399cb273d7f76bc20ee518dd (patch)
tree3fe5eb2c2e2d4b7e047fe87ebeccc0e9e7aa5ea4 /plugin/pkg/dnsutil/ttl.go
parent99287d091c2db4028e54782fd4de43f63ca4b040 (diff)
downloadcoredns-dae506b5638c7309399cb273d7f76bc20ee518dd.tar.gz
coredns-dae506b5638c7309399cb273d7f76bc20ee518dd.tar.zst
coredns-dae506b5638c7309399cb273d7f76bc20ee518dd.zip
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
Diffstat (limited to 'plugin/pkg/dnsutil/ttl.go')
-rw-r--r--plugin/pkg/dnsutil/ttl.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/plugin/pkg/dnsutil/ttl.go b/plugin/pkg/dnsutil/ttl.go
new file mode 100644
index 000000000..e969fa8a6
--- /dev/null
+++ b/plugin/pkg/dnsutil/ttl.go
@@ -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
+)