mirror of
https://github.com/coredns/coredns.git
synced 2025-11-23 12:14:02 -05:00
Remove the word middleware (#1067)
* Rename middleware to plugin first pass; mostly used 'sed', few spots where I manually changed text. This still builds a coredns binary. * fmt error * Rename AddMiddleware to AddPlugin * Readd AddMiddleware to remain backwards compat
This commit is contained in:
58
plugin/pkg/dnsrecorder/recorder.go
Normal file
58
plugin/pkg/dnsrecorder/recorder.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Package dnsrecorder allows you to record a DNS response when it is send to the client.
|
||||
package dnsrecorder
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// Recorder is a type of ResponseWriter that captures
|
||||
// the rcode code written to it and also the size of the message
|
||||
// written in the response. A rcode code does not have
|
||||
// to be written, however, in which case 0 must be assumed.
|
||||
// It is best to have the constructor initialize this type
|
||||
// with that default status code.
|
||||
type Recorder struct {
|
||||
dns.ResponseWriter
|
||||
Rcode int
|
||||
Len int
|
||||
Msg *dns.Msg
|
||||
Start time.Time
|
||||
}
|
||||
|
||||
// New makes and returns a new Recorder,
|
||||
// which captures the DNS rcode from the ResponseWriter
|
||||
// and also the length of the response message written through it.
|
||||
func New(w dns.ResponseWriter) *Recorder {
|
||||
return &Recorder{
|
||||
ResponseWriter: w,
|
||||
Rcode: 0,
|
||||
Msg: nil,
|
||||
Start: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// WriteMsg records the status code and calls the
|
||||
// underlying ResponseWriter's WriteMsg method.
|
||||
func (r *Recorder) WriteMsg(res *dns.Msg) error {
|
||||
r.Rcode = res.Rcode
|
||||
// We may get called multiple times (axfr for instance).
|
||||
// Save the last message, but add the sizes.
|
||||
r.Len += res.Len()
|
||||
r.Msg = res
|
||||
return r.ResponseWriter.WriteMsg(res)
|
||||
}
|
||||
|
||||
// Write is a wrapper that records the length of the message that gets written.
|
||||
func (r *Recorder) Write(buf []byte) (int, error) {
|
||||
n, err := r.ResponseWriter.Write(buf)
|
||||
if err == nil {
|
||||
r.Len += n
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Hijack implements dns.Hijacker. It simply wraps the underlying
|
||||
// ResponseWriter's Hijack method if there is one, or returns an error.
|
||||
func (r *Recorder) Hijack() { r.ResponseWriter.Hijack(); return }
|
||||
28
plugin/pkg/dnsrecorder/recorder_test.go
Normal file
28
plugin/pkg/dnsrecorder/recorder_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package dnsrecorder
|
||||
|
||||
/*
|
||||
func TestNewResponseRecorder(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
recordRequest := NewResponseRecorder(w)
|
||||
if !(recordRequest.ResponseWriter == w) {
|
||||
t.Fatalf("Expected Response writer in the Recording to be same as the one sent\n")
|
||||
}
|
||||
if recordRequest.status != http.StatusOK {
|
||||
t.Fatalf("Expected recorded status to be http.StatusOK (%d) , but found %d\n ", http.StatusOK, recordRequest.status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
responseTestString := "test"
|
||||
recordRequest := NewResponseRecorder(w)
|
||||
buf := []byte(responseTestString)
|
||||
recordRequest.Write(buf)
|
||||
if recordRequest.size != len(buf) {
|
||||
t.Fatalf("Expected the bytes written counter to be %d, but instead found %d\n", len(buf), recordRequest.size)
|
||||
}
|
||||
if w.Body.String() != responseTestString {
|
||||
t.Fatalf("Expected Response Body to be %s , but found %s\n", responseTestString, w.Body.String())
|
||||
}
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user