From ee5b06a231157f270af5f3aa636d1b2f722e91ba Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Thu, 20 Apr 2017 07:07:42 -0700 Subject: [PATCH] Use fmt.Fprint/Fprintf/Fprintln Used the following rewrite rules: gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go --- command/agent/acl_endpoint.go | 14 ++++----- command/agent/agent_endpoint.go | 38 ++++++++++++------------ command/agent/catalog_endpoint.go | 8 ++--- command/agent/coordinate_endpoint.go | 6 ++-- command/agent/event_endpoint.go | 5 ++-- command/agent/health_endpoint.go | 9 +++--- command/agent/http.go | 10 +++---- command/agent/kvs_endpoint.go | 6 ++-- command/agent/operator_endpoint.go | 16 +++++----- command/agent/prepared_query_endpoint.go | 10 +++---- command/agent/session_endpoint.go | 12 ++++---- command/agent/status_endpoint_test.go | 3 +- command/agent/txn_endpoint.go | 17 ++++++----- command/agent/ui_endpoint.go | 3 +- 14 files changed, 83 insertions(+), 74 deletions(-) diff --git a/command/agent/acl_endpoint.go b/command/agent/acl_endpoint.go index e97189e2c7..5f3dd65e13 100644 --- a/command/agent/acl_endpoint.go +++ b/command/agent/acl_endpoint.go @@ -16,7 +16,7 @@ type aclCreateResponse struct { // ACLDisabled handles if ACL datacenter is not configured func ACLDisabled(resp http.ResponseWriter, req *http.Request) (interface{}, error) { resp.WriteHeader(401) - resp.Write([]byte("ACL support disabled")) + fmt.Fprint(resp, "ACL support disabled") return nil, nil } @@ -37,7 +37,7 @@ func (s *HTTPServer) ACLDestroy(resp http.ResponseWriter, req *http.Request) (in args.ACL.ID = strings.TrimPrefix(req.URL.Path, "/v1/acl/destroy/") if args.ACL.ID == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing ACL")) + fmt.Fprint(resp, "Missing ACL") return nil, nil } @@ -76,7 +76,7 @@ func (s *HTTPServer) aclSet(resp http.ResponseWriter, req *http.Request, update if req.ContentLength > 0 { if err := decodeBody(req, &args.ACL, nil); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } } @@ -85,7 +85,7 @@ func (s *HTTPServer) aclSet(resp http.ResponseWriter, req *http.Request, update // create, as one will be generated if not provided. if update && args.ACL.ID == "" { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("ACL ID must be set"))) + fmt.Fprint(resp, "ACL ID must be set") return nil, nil } @@ -118,7 +118,7 @@ func (s *HTTPServer) ACLClone(resp http.ResponseWriter, req *http.Request) (inte args.ACL = strings.TrimPrefix(req.URL.Path, "/v1/acl/clone/") if args.ACL == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing ACL")) + fmt.Fprint(resp, "Missing ACL") return nil, nil } @@ -131,7 +131,7 @@ func (s *HTTPServer) ACLClone(resp http.ResponseWriter, req *http.Request) (inte // Bail if the ACL is not found if len(out.ACLs) == 0 { resp.WriteHeader(404) - resp.Write([]byte(fmt.Sprintf("Target ACL not found"))) + fmt.Fprint(resp, "Target ACL not found") return nil, nil } @@ -167,7 +167,7 @@ func (s *HTTPServer) ACLGet(resp http.ResponseWriter, req *http.Request) (interf args.ACL = strings.TrimPrefix(req.URL.Path, "/v1/acl/info/") if args.ACL == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing ACL")) + fmt.Fprint(resp, "Missing ACL") return nil, nil } diff --git a/command/agent/agent_endpoint.go b/command/agent/agent_endpoint.go index 47a20369e2..038183df22 100644 --- a/command/agent/agent_endpoint.go +++ b/command/agent/agent_endpoint.go @@ -220,20 +220,20 @@ func (s *HTTPServer) AgentRegisterCheck(resp http.ResponseWriter, req *http.Requ } if err := decodeBody(req, &args, decodeCB); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } // Verify the check has a name. if args.Name == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing check name")) + fmt.Fprint(resp, "Missing check name") return nil, nil } if args.Status != "" && !structs.ValidStatus(args.Status) { resp.WriteHeader(400) - resp.Write([]byte("Bad check status")) + fmt.Fprint(resp, "Bad check status") return nil, nil } @@ -244,7 +244,7 @@ func (s *HTTPServer) AgentRegisterCheck(resp http.ResponseWriter, req *http.Requ chkType := &args.CheckType if !chkType.Valid() { resp.WriteHeader(400) - resp.Write([]byte(invalidCheckMessage)) + fmt.Fprint(resp, invalidCheckMessage) return nil, nil } @@ -358,7 +358,7 @@ func (s *HTTPServer) AgentCheckUpdate(resp http.ResponseWriter, req *http.Reques var update checkUpdate if err := decodeBody(req, &update, nil); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } @@ -368,7 +368,7 @@ func (s *HTTPServer) AgentCheckUpdate(resp http.ResponseWriter, req *http.Reques case structs.HealthCritical: default: resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Invalid check status: '%s'", update.Status))) + fmt.Fprintf(resp, "Invalid check status: '%s'", update.Status) return nil, nil } @@ -425,14 +425,14 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re } if err := decodeBody(req, &args, decodeCB); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } // Verify the service has a name. if args.Name == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing service name")) + fmt.Fprint(resp, "Missing service name") return nil, nil } @@ -444,12 +444,12 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re for _, check := range chkTypes { if check.Status != "" && !structs.ValidStatus(check.Status) { resp.WriteHeader(400) - resp.Write([]byte("Status for checks must 'passing', 'warning', 'critical'")) + fmt.Fprint(resp, "Status for checks must 'passing', 'warning', 'critical'") return nil, nil } if !check.Valid() { resp.WriteHeader(400) - resp.Write([]byte(invalidCheckMessage)) + fmt.Fprint(resp, invalidCheckMessage) return nil, nil } } @@ -497,7 +497,7 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http serviceID := strings.TrimPrefix(req.URL.Path, "/v1/agent/service/maintenance/") if serviceID == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing service ID")) + fmt.Fprint(resp, "Missing service ID") return nil, nil } @@ -505,7 +505,7 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http params := req.URL.Query() if _, ok := params["enable"]; !ok { resp.WriteHeader(400) - resp.Write([]byte("Missing value for enable")) + fmt.Fprint(resp, "Missing value for enable") return nil, nil } @@ -513,7 +513,7 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http enable, err := strconv.ParseBool(raw) if err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Invalid value for enable: %q", raw))) + fmt.Fprintf(resp, "Invalid value for enable: %q", raw) return nil, nil } @@ -528,13 +528,13 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http reason := params.Get("reason") if err = s.agent.EnableServiceMaintenance(serviceID, reason, token); err != nil { resp.WriteHeader(404) - resp.Write([]byte(err.Error())) + fmt.Fprint(resp, err.Error()) return nil, nil } } else { if err = s.agent.DisableServiceMaintenance(serviceID); err != nil { resp.WriteHeader(404) - resp.Write([]byte(err.Error())) + fmt.Fprint(resp, err.Error()) return nil, nil } } @@ -553,7 +553,7 @@ func (s *HTTPServer) AgentNodeMaintenance(resp http.ResponseWriter, req *http.Re params := req.URL.Query() if _, ok := params["enable"]; !ok { resp.WriteHeader(400) - resp.Write([]byte("Missing value for enable")) + fmt.Fprint(resp, "Missing value for enable") return nil, nil } @@ -561,7 +561,7 @@ func (s *HTTPServer) AgentNodeMaintenance(resp http.ResponseWriter, req *http.Re enable, err := strconv.ParseBool(raw) if err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Invalid value for enable: %q", raw))) + fmt.Fprintf(resp, "Invalid value for enable: %q", raw) return nil, nil } @@ -617,7 +617,7 @@ func (s *HTTPServer) AgentMonitor(resp http.ResponseWriter, req *http.Request) ( filter.MinLevel = logutils.LogLevel(logLevel) if !logger.ValidateLevelFilter(filter.MinLevel, filter) { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Unknown log level: %s", filter.MinLevel))) + fmt.Fprintf(resp, "Unknown log level: %s", filter.MinLevel) return nil, nil } flusher, ok := resp.(http.Flusher) @@ -645,7 +645,7 @@ func (s *HTTPServer) AgentMonitor(resp http.ResponseWriter, req *http.Request) ( } return nil, nil case log := <-handler.logCh: - resp.Write([]byte(log + "\n")) + fmt.Fprintln(resp, log) flusher.Flush() } } diff --git a/command/agent/catalog_endpoint.go b/command/agent/catalog_endpoint.go index 4c7f59db97..5c82590ef3 100644 --- a/command/agent/catalog_endpoint.go +++ b/command/agent/catalog_endpoint.go @@ -12,7 +12,7 @@ func (s *HTTPServer) CatalogRegister(resp http.ResponseWriter, req *http.Request var args structs.RegisterRequest if err := decodeBody(req, &args, nil); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } @@ -34,7 +34,7 @@ func (s *HTTPServer) CatalogDeregister(resp http.ResponseWriter, req *http.Reque var args structs.DeregisterRequest if err := decodeBody(req, &args, nil); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } @@ -119,7 +119,7 @@ func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Req args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/catalog/service/") if args.ServiceName == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing service name")) + fmt.Fprint(resp, "Missing service name") return nil, nil } @@ -149,7 +149,7 @@ func (s *HTTPServer) CatalogNodeServices(resp http.ResponseWriter, req *http.Req args.Node = strings.TrimPrefix(req.URL.Path, "/v1/catalog/node/") if args.Node == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing node name")) + fmt.Fprint(resp, "Missing node name") return nil, nil } diff --git a/command/agent/coordinate_endpoint.go b/command/agent/coordinate_endpoint.go index 92453225d7..334bff9f44 100644 --- a/command/agent/coordinate_endpoint.go +++ b/command/agent/coordinate_endpoint.go @@ -1,16 +1,18 @@ package agent import ( - "github.com/hashicorp/consul/consul/structs" + "fmt" "net/http" "sort" + + "github.com/hashicorp/consul/consul/structs" ) // coordinateDisabled handles all the endpoints when coordinates are not enabled, // returning an error message. func coordinateDisabled(resp http.ResponseWriter, req *http.Request) (interface{}, error) { resp.WriteHeader(401) - resp.Write([]byte("Coordinate support disabled")) + fmt.Fprint(resp, "Coordinate support disabled") return nil, nil } diff --git a/command/agent/event_endpoint.go b/command/agent/event_endpoint.go index 6735d66b64..e6d38244e7 100644 --- a/command/agent/event_endpoint.go +++ b/command/agent/event_endpoint.go @@ -2,6 +2,7 @@ package agent import ( "bytes" + "fmt" "io" "net/http" "strconv" @@ -32,7 +33,7 @@ func (s *HTTPServer) EventFire(resp http.ResponseWriter, req *http.Request) (int event.Name = strings.TrimPrefix(req.URL.Path, "/v1/event/fire/") if event.Name == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing name")) + fmt.Fprint(resp, "Missing name") return nil, nil } @@ -64,7 +65,7 @@ func (s *HTTPServer) EventFire(resp http.ResponseWriter, req *http.Request) (int if err := s.agent.UserEvent(dc, token, event); err != nil { if strings.Contains(err.Error(), permissionDenied) { resp.WriteHeader(403) - resp.Write([]byte(permissionDenied)) + fmt.Fprint(resp, permissionDenied) return nil, nil } resp.WriteHeader(500) diff --git a/command/agent/health_endpoint.go b/command/agent/health_endpoint.go index f201c3bc19..78ebe51e5e 100644 --- a/command/agent/health_endpoint.go +++ b/command/agent/health_endpoint.go @@ -1,6 +1,7 @@ package agent import ( + "fmt" "net/http" "strings" @@ -20,7 +21,7 @@ func (s *HTTPServer) HealthChecksInState(resp http.ResponseWriter, req *http.Req args.State = strings.TrimPrefix(req.URL.Path, "/v1/health/state/") if args.State == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing check state")) + fmt.Fprint(resp, "Missing check state") return nil, nil } @@ -49,7 +50,7 @@ func (s *HTTPServer) HealthNodeChecks(resp http.ResponseWriter, req *http.Reques args.Node = strings.TrimPrefix(req.URL.Path, "/v1/health/node/") if args.Node == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing node name")) + fmt.Fprint(resp, "Missing node name") return nil, nil } @@ -80,7 +81,7 @@ func (s *HTTPServer) HealthServiceChecks(resp http.ResponseWriter, req *http.Req args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/health/checks/") if args.ServiceName == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing service name")) + fmt.Fprint(resp, "Missing service name") return nil, nil } @@ -118,7 +119,7 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/health/service/") if args.ServiceName == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing service name")) + fmt.Fprint(resp, "Missing service name") return nil, nil } diff --git a/command/agent/http.go b/command/agent/http.go index 317df3a100..2b78d32629 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -380,7 +380,7 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque } resp.WriteHeader(code) - resp.Write([]byte(err.Error())) + fmt.Fprint(resp, err.Error()) return } @@ -433,7 +433,7 @@ func (s *HTTPServer) Index(resp http.ResponseWriter, req *http.Request) { // Give them something helpful if there's no UI so they at least know // what this server is. if !s.IsUIEnabled() { - resp.Write([]byte("Consul Agent")) + fmt.Fprint(resp, "Consul Agent") return } @@ -508,7 +508,7 @@ func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOpti dur, err := time.ParseDuration(wait) if err != nil { resp.WriteHeader(http.StatusBadRequest) // 400 - resp.Write([]byte("Invalid wait time")) + fmt.Fprint(resp, "Invalid wait time") return true } b.MaxQueryTime = dur @@ -517,7 +517,7 @@ func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOpti index, err := strconv.ParseUint(idx, 10, 64) if err != nil { resp.WriteHeader(http.StatusBadRequest) // 400 - resp.Write([]byte("Invalid index")) + fmt.Fprint(resp, "Invalid index") return true } b.MinQueryIndex = index @@ -537,7 +537,7 @@ func parseConsistency(resp http.ResponseWriter, req *http.Request, b *structs.Qu } if b.AllowStale && b.RequireConsistent { resp.WriteHeader(http.StatusBadRequest) // 400 - resp.Write([]byte("Cannot specify ?stale with ?consistent, conflicting semantics.")) + fmt.Fprint(resp, "Cannot specify ?stale with ?consistent, conflicting semantics.") return true } return false diff --git a/command/agent/kvs_endpoint.go b/command/agent/kvs_endpoint.go index 7692e4926d..cfe3c7f80a 100644 --- a/command/agent/kvs_endpoint.go +++ b/command/agent/kvs_endpoint.go @@ -185,7 +185,7 @@ func (s *HTTPServer) KVSPut(resp http.ResponseWriter, req *http.Request, args *s // Check the content-length if req.ContentLength > maxKVSize { resp.WriteHeader(413) - resp.Write([]byte(fmt.Sprintf("Value exceeds %d byte limit", maxKVSize))) + fmt.Fprintf(resp, "Value exceeds %d byte limit", maxKVSize) return nil, nil } @@ -260,7 +260,7 @@ func (s *HTTPServer) KVSDelete(resp http.ResponseWriter, req *http.Request, args func missingKey(resp http.ResponseWriter, args *structs.KeyRequest) bool { if args.Key == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing key name")) + fmt.Fprint(resp, "Missing key name") return true } return false @@ -275,7 +275,7 @@ func conflictingFlags(resp http.ResponseWriter, req *http.Request, flags ...stri if _, ok := params[conflict]; ok { if found { resp.WriteHeader(400) - resp.Write([]byte("Conflicting flags: " + params.Encode())) + fmt.Fprint(resp, "Conflicting flags: "+params.Encode()) return true } found = true diff --git a/command/agent/operator_endpoint.go b/command/agent/operator_endpoint.go index 2534c1261b..3d0c899d8a 100644 --- a/command/agent/operator_endpoint.go +++ b/command/agent/operator_endpoint.go @@ -4,13 +4,13 @@ import ( "fmt" "net/http" "strconv" + "strings" "time" "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/consul/structs" multierror "github.com/hashicorp/go-multierror" "github.com/hashicorp/raft" - "strings" ) // OperatorRaftConfiguration is used to inspect the current Raft configuration. @@ -58,12 +58,12 @@ func (s *HTTPServer) OperatorRaftPeer(resp http.ResponseWriter, req *http.Reques if !hasID && !hasAddress { resp.WriteHeader(http.StatusBadRequest) - resp.Write([]byte("Must specify either ?id with the server's ID or ?address with IP:port of peer to remove")) + fmt.Fprint(resp, "Must specify either ?id with the server's ID or ?address with IP:port of peer to remove") return nil, nil } if hasID && hasAddress { resp.WriteHeader(http.StatusBadRequest) - resp.Write([]byte("Must specify only one of ?id or ?address")) + fmt.Fprint(resp, "Must specify only one of ?id or ?address") return nil, nil } @@ -91,7 +91,7 @@ func (s *HTTPServer) OperatorKeyringEndpoint(resp http.ResponseWriter, req *http if req.Method == "POST" || req.Method == "PUT" || req.Method == "DELETE" { if err := decodeBody(req, &args, nil); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } } @@ -102,14 +102,14 @@ func (s *HTTPServer) OperatorKeyringEndpoint(resp http.ResponseWriter, req *http n, err := strconv.Atoi(relayFactor) if err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Error parsing relay factor: %v", err))) + fmt.Fprintf(resp, "Error parsing relay factor: %v", err) return nil, nil } args.RelayFactor, err = ParseRelayFactor(n) if err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Invalid relay factor: %v", err))) + fmt.Fprintf(resp, "Invalid relay factor: %v", err) return nil, nil } } @@ -223,7 +223,7 @@ func (s *HTTPServer) OperatorAutopilotConfiguration(resp http.ResponseWriter, re var conf api.AutopilotConfiguration if err := decodeBody(req, &conf, FixupConfigDurations); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Error parsing autopilot config: %v", err))) + fmt.Fprintf(resp, "Error parsing autopilot config: %v", err) return nil, nil } @@ -242,7 +242,7 @@ func (s *HTTPServer) OperatorAutopilotConfiguration(resp http.ResponseWriter, re casVal, err := strconv.ParseUint(params.Get("cas"), 10, 64) if err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Error parsing cas value: %v", err))) + fmt.Fprintf(resp, "Error parsing cas value: %v", err) return nil, nil } args.Config.ModifyIndex = casVal diff --git a/command/agent/prepared_query_endpoint.go b/command/agent/prepared_query_endpoint.go index f182607f1f..b226c3ea54 100644 --- a/command/agent/prepared_query_endpoint.go +++ b/command/agent/prepared_query_endpoint.go @@ -31,7 +31,7 @@ func (s *HTTPServer) preparedQueryCreate(resp http.ResponseWriter, req *http.Req if req.ContentLength > 0 { if err := decodeBody(req, &args.Query, nil); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } } @@ -116,7 +116,7 @@ func (s *HTTPServer) preparedQueryExecute(id string, resp http.ResponseWriter, r // the specific error type. if err.Error() == consul.ErrQueryNotFound.Error() { resp.WriteHeader(404) - resp.Write([]byte(err.Error())) + fmt.Fprint(resp, err.Error()) return nil, nil } return nil, err @@ -161,7 +161,7 @@ func (s *HTTPServer) preparedQueryExplain(id string, resp http.ResponseWriter, r // the specific error type. if err.Error() == consul.ErrQueryNotFound.Error() { resp.WriteHeader(404) - resp.Write([]byte(err.Error())) + fmt.Fprint(resp, err.Error()) return nil, nil } return nil, err @@ -185,7 +185,7 @@ func (s *HTTPServer) preparedQueryGet(id string, resp http.ResponseWriter, req * // the specific error type. if err.Error() == consul.ErrQueryNotFound.Error() { resp.WriteHeader(404) - resp.Write([]byte(err.Error())) + fmt.Fprint(resp, err.Error()) return nil, nil } return nil, err @@ -203,7 +203,7 @@ func (s *HTTPServer) preparedQueryUpdate(id string, resp http.ResponseWriter, re if req.ContentLength > 0 { if err := decodeBody(req, &args.Query, nil); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } } diff --git a/command/agent/session_endpoint.go b/command/agent/session_endpoint.go index 92ed9c6b14..8d07d3b9cd 100644 --- a/command/agent/session_endpoint.go +++ b/command/agent/session_endpoint.go @@ -52,7 +52,7 @@ func (s *HTTPServer) SessionCreate(resp http.ResponseWriter, req *http.Request) if req.ContentLength > 0 { if err := decodeBody(req, &args.Session, FixupLockDelay); err != nil { resp.WriteHeader(400) - resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + fmt.Fprintf(resp, "Request decode failed: %v", err) return nil, nil } } @@ -124,7 +124,7 @@ func (s *HTTPServer) SessionDestroy(resp http.ResponseWriter, req *http.Request) args.Session.ID = strings.TrimPrefix(req.URL.Path, "/v1/session/destroy/") if args.Session.ID == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing session")) + fmt.Fprint(resp, "Missing session") return nil, nil } @@ -152,7 +152,7 @@ func (s *HTTPServer) SessionRenew(resp http.ResponseWriter, req *http.Request) ( args.Session = strings.TrimPrefix(req.URL.Path, "/v1/session/renew/") if args.Session == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing session")) + fmt.Fprint(resp, "Missing session") return nil, nil } @@ -161,7 +161,7 @@ func (s *HTTPServer) SessionRenew(resp http.ResponseWriter, req *http.Request) ( return nil, err } else if out.Sessions == nil { resp.WriteHeader(404) - resp.Write([]byte(fmt.Sprintf("Session id '%s' not found", args.Session))) + fmt.Fprintf(resp, "Session id '%s' not found", args.Session) return nil, nil } @@ -179,7 +179,7 @@ func (s *HTTPServer) SessionGet(resp http.ResponseWriter, req *http.Request) (in args.Session = strings.TrimPrefix(req.URL.Path, "/v1/session/info/") if args.Session == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing session")) + fmt.Fprint(resp, "Missing session") return nil, nil } @@ -227,7 +227,7 @@ func (s *HTTPServer) SessionsForNode(resp http.ResponseWriter, req *http.Request args.Node = strings.TrimPrefix(req.URL.Path, "/v1/session/node/") if args.Node == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing node name")) + fmt.Fprint(resp, "Missing node name") return nil, nil } diff --git a/command/agent/status_endpoint_test.go b/command/agent/status_endpoint_test.go index 0e22eaa30f..66fdb7c17b 100644 --- a/command/agent/status_endpoint_test.go +++ b/command/agent/status_endpoint_test.go @@ -1,9 +1,10 @@ package agent import ( - "github.com/hashicorp/consul/testutil" "os" "testing" + + "github.com/hashicorp/consul/testutil" ) func TestStatusLeader(t *testing.T) { diff --git a/command/agent/txn_endpoint.go b/command/agent/txn_endpoint.go index b589678e90..2db9e5630d 100644 --- a/command/agent/txn_endpoint.go +++ b/command/agent/txn_endpoint.go @@ -93,7 +93,7 @@ func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (st var ops api.TxnOps if err := decodeBody(req, &ops, fixupKVOps); err != nil { resp.WriteHeader(http.StatusBadRequest) - resp.Write([]byte(fmt.Sprintf("Failed to parse body: %v", err))) + fmt.Fprintf(resp, "Failed to parse body: %v", err) return nil, 0, false } @@ -101,8 +101,9 @@ func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (st // transaction in order to curb abuse. if size := len(ops); size > maxTxnOps { resp.WriteHeader(http.StatusRequestEntityTooLarge) - resp.Write([]byte(fmt.Sprintf("Transaction contains too many operations (%d > %d)", - size, maxTxnOps))) + fmt.Fprintf(resp, "Transaction contains too many operations (%d > %d)", + size, maxTxnOps) + return nil, 0, false } @@ -116,8 +117,9 @@ func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (st if in.KV != nil { if size := len(in.KV.Value); size > maxKVSize { resp.WriteHeader(http.StatusRequestEntityTooLarge) - resp.Write([]byte(fmt.Sprintf("Value for key %q is too large (%d > %d bytes)", - in.KV.Key, size, maxKVSize))) + fmt.Fprintf(resp, "Value for key %q is too large (%d > %d bytes)", + in.KV.Key, size, maxKVSize) + return nil, 0, false } else { netKVSize += size @@ -149,8 +151,9 @@ func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (st // Enforce an overall size limit to help prevent abuse. if netKVSize > maxKVSize { resp.WriteHeader(http.StatusRequestEntityTooLarge) - resp.Write([]byte(fmt.Sprintf("Cumulative size of key data is too large (%d > %d bytes)", - netKVSize, maxKVSize))) + fmt.Fprintf(resp, "Cumulative size of key data is too large (%d > %d bytes)", + netKVSize, maxKVSize) + return nil, 0, false } diff --git a/command/agent/ui_endpoint.go b/command/agent/ui_endpoint.go index c240879876..3532e688e3 100644 --- a/command/agent/ui_endpoint.go +++ b/command/agent/ui_endpoint.go @@ -1,6 +1,7 @@ package agent import ( + "fmt" "net/http" "sort" "strings" @@ -67,7 +68,7 @@ func (s *HTTPServer) UINodeInfo(resp http.ResponseWriter, req *http.Request) (in args.Node = strings.TrimPrefix(req.URL.Path, "/v1/internal/ui/node/") if args.Node == "" { resp.WriteHeader(400) - resp.Write([]byte("Missing node name")) + fmt.Fprint(resp, "Missing node name") return nil, nil }