define dns message type as constant

master
fangdingjun 6 years ago
parent 640d3e2018
commit 96df0202b5

@ -52,8 +52,12 @@ func main() {
if cfg.UpstreamTimeout == 0 {
cfg.UpstreamTimeout = 5
}
initDNSClient(cfg)
log.Debugf("%+v", cfg)
makeServers(cfg)
select {}
}

@ -35,6 +35,8 @@ func (lh *logHandler) Status() int {
var _ http.ResponseWriter = &logHandler{}
// LogHandler is a log middleware
// record request log
func LogHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
@ -50,6 +52,5 @@ func LogHandler(handler http.Handler) http.Handler {
handler.ServeHTTP(lh, r)
log.Infof("\"%s %s %s\" - %d %d \"%s\"",
r.Method, r.RequestURI, r.Proto, lh.Status(), lh.size, r.UserAgent())
})
}

@ -9,10 +9,12 @@ import (
"github.com/miekg/dns"
)
const dnsMsgType = "application/dns-message"
func (srv *server) handleHTTPReq(w http.ResponseWriter, r *http.Request) {
ctype := r.Header.Get("content-type")
if !strings.HasPrefix(ctype, "application/dns-message") {
log.Errorf("request type %s, require application/dns-message", ctype)
if !strings.HasPrefix(ctype, dnsMsgType) {
log.Errorf("request type %s, require %s", ctype, dnsMsgType)
http.Error(w, "dns message is required", http.StatusBadRequest)
return
}
@ -45,7 +47,7 @@ func (srv *server) handleHTTPReq(w http.ResponseWriter, r *http.Request) {
for _, a := range m.Answer {
log.Debugln("result", a.String())
}
w.Header().Set("content-type", "application/dns-message")
w.Header().Set("content-type", dnsMsgType)
d, _ := m.Pack()
w.Write(d)
}

@ -10,7 +10,6 @@ import (
"time"
"github.com/fangdingjun/go-log"
//nghttp2 "github.com/fangdingjun/nghttp2-go"
"github.com/miekg/dns"
"golang.org/x/net/http2"
)

Loading…
Cancel
Save