Files
coredns/middleware/rewrite/rewrite.go

62 lines
1.7 KiB
Go
Raw Normal View History

// Package rewrite is middleware for rewriting requests internally to something different.
2016-03-18 20:57:35 +00:00
package rewrite
import (
"github.com/coredns/coredns/middleware"
2016-03-18 20:57:35 +00:00
"github.com/miekg/dns"
"golang.org/x/net/context"
2016-03-18 20:57:35 +00:00
)
// Result is the result of a rewrite
type Result int
const (
// RewriteIgnored is returned when rewrite is not done on request.
RewriteIgnored Result = iota
// RewriteDone is returned when rewrite is done on request.
RewriteDone
// RewriteStatus is returned when rewrite is not needed and status code should be set
// for the request.
RewriteStatus
)
// Rewrite is middleware to rewrite requests internally before being handled.
type Rewrite struct {
Next middleware.Handler
Rules []Rule
noRevert bool
2016-03-18 20:57:35 +00:00
}
Golint2 (#280) * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * More lint fixes This leaves: ~~~ middleware/kubernetes/nametemplate/nametemplate.go:64:6: exported type NameTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:71:1: exported method NameTemplate.SetTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:108:1: exported method NameTemplate.GetZoneFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:116:1: exported method NameTemplate.GetNamespaceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:120:1: exported method NameTemplate.GetServiceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:124:1: exported method NameTemplate.GetTypeFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:135:1: exported method NameTemplate.GetSymbolFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:167:1: exported method NameTemplate.IsValid should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:182:6: exported type NameValues should have comment or be unexported middleware/kubernetes/util/util.go:1:1: package comment should be of the form "Package util ..." middleware/kubernetes/util/util.go:27:2: exported const WildcardStar should have comment (or a comment on this block) or be unexported middleware/proxy/lookup.go:66:1: exported method Proxy.Forward should have comment or be unexported middleware/proxy/proxy.go:24:6: exported type Client should have comment or be unexported middleware/proxy/proxy.go:107:1: exported function Clients should have comment or be unexported middleware/proxy/reverseproxy.go:10:6: exported type ReverseProxy should have comment or be unexported middleware/proxy/reverseproxy.go:16:1: exported method ReverseProxy.ServeDNS should have comment or be unexported middleware/proxy/upstream.go:42:6: exported type Options should have comment or be unexported ~~~ I plan on reworking the proxy anyway, so I'll leave that be.
2016-09-23 09:14:12 +01:00
// ServeDNS implements the middleware.Handler interface.
func (rw Rewrite) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
2016-04-21 22:02:26 +01:00
wr := NewResponseReverter(w, r)
2016-03-18 20:57:35 +00:00
for _, rule := range rw.Rules {
switch result := rule.Rewrite(r); result {
case RewriteDone:
if rw.noRevert {
return middleware.NextOrFailure(rw.Name(), rw.Next, ctx, w, r)
}
return middleware.NextOrFailure(rw.Name(), rw.Next, ctx, wr, r)
2016-03-18 20:57:35 +00:00
case RewriteIgnored:
break
case RewriteStatus:
// only valid for complex rules.
// if cRule, ok := rule.(*ComplexRule); ok && cRule.Status != 0 {
// return cRule.Status, nil
// }
}
}
return middleware.NextOrFailure(rw.Name(), rw.Next, ctx, w, r)
2016-03-18 20:57:35 +00:00
}
2016-10-27 11:48:37 +00:00
// Name implements the Handler interface.
func (rw Rewrite) Name() string { return "rewrite" }
2016-03-18 20:57:35 +00:00
// Rule describes an internal location rewrite rule.
type Rule interface {
// Rewrite rewrites the internal location of the current request.
Rewrite(*dns.Msg) Result
// New returns a new rule.
New(...string) Rule
2016-03-18 20:57:35 +00:00
}