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 { if cfg.UpstreamTimeout == 0 {
cfg.UpstreamTimeout = 5 cfg.UpstreamTimeout = 5
} }
initDNSClient(cfg) initDNSClient(cfg)
log.Debugf("%+v", cfg) log.Debugf("%+v", cfg)
makeServers(cfg) makeServers(cfg)
select {} select {}
} }

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

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

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

Loading…
Cancel
Save