From 934f6fed2a9d3ced6f551dd8847546436178e357 Mon Sep 17 00:00:00 2001 From: dingjun Date: Tue, 30 Jun 2020 19:25:02 +0800 Subject: [PATCH] use http.Error send error message --- server_https.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server_https.go b/server_https.go index a784bf7..3f22eaa 100644 --- a/server_https.go +++ b/server_https.go @@ -28,19 +28,21 @@ func (srv *server) handleHTTPReq(w http.ResponseWriter, r *http.Request) { data, err := ioutil.ReadAll(r.Body) if err != nil { log.Errorln("read request body", err) - w.WriteHeader(http.StatusBadRequest) + http.Error(w, "read request failed", http.StatusBadRequest) return } msg := new(dns.Msg) if err := msg.Unpack(data); err != nil { log.Errorln("parse dns message", err) + http.Error(w, "parse dns message error", http.StatusBadRequest) return } + m, err := getResponseFromUpstream(msg, srv.upstreams) if err != nil { log.Debugln("query", msg.Question[0].String(), "timeout") - w.WriteHeader(http.StatusServiceUnavailable) + http.Error(w, "query upstream server failed", http.StatusServiceUnavailable) return } @@ -54,7 +56,7 @@ func (srv *server) handleHTTPReq(w http.ResponseWriter, r *http.Request) { func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.RequestURI != srv.addr.Path { - w.WriteHeader(http.StatusNotFound) + http.Error(w, "Path not found", http.StatusNotFound) return } srv.handleHTTPReq(w, r)