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
This commit is contained in:
Frank Schroeder 2017-04-20 07:07:42 -07:00 committed by Frank Schröder
parent 8234f3acc6
commit ee5b06a231
14 changed files with 83 additions and 74 deletions

View File

@ -16,7 +16,7 @@ type aclCreateResponse struct {
// ACLDisabled handles if ACL datacenter is not configured // ACLDisabled handles if ACL datacenter is not configured
func ACLDisabled(resp http.ResponseWriter, req *http.Request) (interface{}, error) { func ACLDisabled(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
resp.WriteHeader(401) resp.WriteHeader(401)
resp.Write([]byte("ACL support disabled")) fmt.Fprint(resp, "ACL support disabled")
return nil, nil 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/") args.ACL.ID = strings.TrimPrefix(req.URL.Path, "/v1/acl/destroy/")
if args.ACL.ID == "" { if args.ACL.ID == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing ACL")) fmt.Fprint(resp, "Missing ACL")
return nil, nil return nil, nil
} }
@ -76,7 +76,7 @@ func (s *HTTPServer) aclSet(resp http.ResponseWriter, req *http.Request, update
if req.ContentLength > 0 { if req.ContentLength > 0 {
if err := decodeBody(req, &args.ACL, nil); err != nil { if err := decodeBody(req, &args.ACL, nil); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil 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. // create, as one will be generated if not provided.
if update && args.ACL.ID == "" { if update && args.ACL.ID == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("ACL ID must be set"))) fmt.Fprint(resp, "ACL ID must be set")
return nil, nil 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/") args.ACL = strings.TrimPrefix(req.URL.Path, "/v1/acl/clone/")
if args.ACL == "" { if args.ACL == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing ACL")) fmt.Fprint(resp, "Missing ACL")
return nil, nil 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 // Bail if the ACL is not found
if len(out.ACLs) == 0 { if len(out.ACLs) == 0 {
resp.WriteHeader(404) resp.WriteHeader(404)
resp.Write([]byte(fmt.Sprintf("Target ACL not found"))) fmt.Fprint(resp, "Target ACL not found")
return nil, nil 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/") args.ACL = strings.TrimPrefix(req.URL.Path, "/v1/acl/info/")
if args.ACL == "" { if args.ACL == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing ACL")) fmt.Fprint(resp, "Missing ACL")
return nil, nil return nil, nil
} }

View File

@ -220,20 +220,20 @@ func (s *HTTPServer) AgentRegisterCheck(resp http.ResponseWriter, req *http.Requ
} }
if err := decodeBody(req, &args, decodeCB); err != nil { if err := decodeBody(req, &args, decodeCB); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil return nil, nil
} }
// Verify the check has a name. // Verify the check has a name.
if args.Name == "" { if args.Name == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing check name")) fmt.Fprint(resp, "Missing check name")
return nil, nil return nil, nil
} }
if args.Status != "" && !structs.ValidStatus(args.Status) { if args.Status != "" && !structs.ValidStatus(args.Status) {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Bad check status")) fmt.Fprint(resp, "Bad check status")
return nil, nil return nil, nil
} }
@ -244,7 +244,7 @@ func (s *HTTPServer) AgentRegisterCheck(resp http.ResponseWriter, req *http.Requ
chkType := &args.CheckType chkType := &args.CheckType
if !chkType.Valid() { if !chkType.Valid() {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(invalidCheckMessage)) fmt.Fprint(resp, invalidCheckMessage)
return nil, nil return nil, nil
} }
@ -358,7 +358,7 @@ func (s *HTTPServer) AgentCheckUpdate(resp http.ResponseWriter, req *http.Reques
var update checkUpdate var update checkUpdate
if err := decodeBody(req, &update, nil); err != nil { if err := decodeBody(req, &update, nil); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil return nil, nil
} }
@ -368,7 +368,7 @@ func (s *HTTPServer) AgentCheckUpdate(resp http.ResponseWriter, req *http.Reques
case structs.HealthCritical: case structs.HealthCritical:
default: default:
resp.WriteHeader(400) 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 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 { if err := decodeBody(req, &args, decodeCB); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil return nil, nil
} }
// Verify the service has a name. // Verify the service has a name.
if args.Name == "" { if args.Name == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing service name")) fmt.Fprint(resp, "Missing service name")
return nil, nil return nil, nil
} }
@ -444,12 +444,12 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re
for _, check := range chkTypes { for _, check := range chkTypes {
if check.Status != "" && !structs.ValidStatus(check.Status) { if check.Status != "" && !structs.ValidStatus(check.Status) {
resp.WriteHeader(400) 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 return nil, nil
} }
if !check.Valid() { if !check.Valid() {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(invalidCheckMessage)) fmt.Fprint(resp, invalidCheckMessage)
return nil, nil 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/") serviceID := strings.TrimPrefix(req.URL.Path, "/v1/agent/service/maintenance/")
if serviceID == "" { if serviceID == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing service ID")) fmt.Fprint(resp, "Missing service ID")
return nil, nil return nil, nil
} }
@ -505,7 +505,7 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http
params := req.URL.Query() params := req.URL.Query()
if _, ok := params["enable"]; !ok { if _, ok := params["enable"]; !ok {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing value for enable")) fmt.Fprint(resp, "Missing value for enable")
return nil, nil return nil, nil
} }
@ -513,7 +513,7 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http
enable, err := strconv.ParseBool(raw) enable, err := strconv.ParseBool(raw)
if err != nil { if err != nil {
resp.WriteHeader(400) 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 return nil, nil
} }
@ -528,13 +528,13 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http
reason := params.Get("reason") reason := params.Get("reason")
if err = s.agent.EnableServiceMaintenance(serviceID, reason, token); err != nil { if err = s.agent.EnableServiceMaintenance(serviceID, reason, token); err != nil {
resp.WriteHeader(404) resp.WriteHeader(404)
resp.Write([]byte(err.Error())) fmt.Fprint(resp, err.Error())
return nil, nil return nil, nil
} }
} else { } else {
if err = s.agent.DisableServiceMaintenance(serviceID); err != nil { if err = s.agent.DisableServiceMaintenance(serviceID); err != nil {
resp.WriteHeader(404) resp.WriteHeader(404)
resp.Write([]byte(err.Error())) fmt.Fprint(resp, err.Error())
return nil, nil return nil, nil
} }
} }
@ -553,7 +553,7 @@ func (s *HTTPServer) AgentNodeMaintenance(resp http.ResponseWriter, req *http.Re
params := req.URL.Query() params := req.URL.Query()
if _, ok := params["enable"]; !ok { if _, ok := params["enable"]; !ok {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing value for enable")) fmt.Fprint(resp, "Missing value for enable")
return nil, nil return nil, nil
} }
@ -561,7 +561,7 @@ func (s *HTTPServer) AgentNodeMaintenance(resp http.ResponseWriter, req *http.Re
enable, err := strconv.ParseBool(raw) enable, err := strconv.ParseBool(raw)
if err != nil { if err != nil {
resp.WriteHeader(400) 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 return nil, nil
} }
@ -617,7 +617,7 @@ func (s *HTTPServer) AgentMonitor(resp http.ResponseWriter, req *http.Request) (
filter.MinLevel = logutils.LogLevel(logLevel) filter.MinLevel = logutils.LogLevel(logLevel)
if !logger.ValidateLevelFilter(filter.MinLevel, filter) { if !logger.ValidateLevelFilter(filter.MinLevel, filter) {
resp.WriteHeader(400) 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 return nil, nil
} }
flusher, ok := resp.(http.Flusher) flusher, ok := resp.(http.Flusher)
@ -645,7 +645,7 @@ func (s *HTTPServer) AgentMonitor(resp http.ResponseWriter, req *http.Request) (
} }
return nil, nil return nil, nil
case log := <-handler.logCh: case log := <-handler.logCh:
resp.Write([]byte(log + "\n")) fmt.Fprintln(resp, log)
flusher.Flush() flusher.Flush()
} }
} }

View File

@ -12,7 +12,7 @@ func (s *HTTPServer) CatalogRegister(resp http.ResponseWriter, req *http.Request
var args structs.RegisterRequest var args structs.RegisterRequest
if err := decodeBody(req, &args, nil); err != nil { if err := decodeBody(req, &args, nil); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil return nil, nil
} }
@ -34,7 +34,7 @@ func (s *HTTPServer) CatalogDeregister(resp http.ResponseWriter, req *http.Reque
var args structs.DeregisterRequest var args structs.DeregisterRequest
if err := decodeBody(req, &args, nil); err != nil { if err := decodeBody(req, &args, nil); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil 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/") args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/catalog/service/")
if args.ServiceName == "" { if args.ServiceName == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing service name")) fmt.Fprint(resp, "Missing service name")
return nil, nil 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/") args.Node = strings.TrimPrefix(req.URL.Path, "/v1/catalog/node/")
if args.Node == "" { if args.Node == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing node name")) fmt.Fprint(resp, "Missing node name")
return nil, nil return nil, nil
} }

View File

@ -1,16 +1,18 @@
package agent package agent
import ( import (
"github.com/hashicorp/consul/consul/structs" "fmt"
"net/http" "net/http"
"sort" "sort"
"github.com/hashicorp/consul/consul/structs"
) )
// coordinateDisabled handles all the endpoints when coordinates are not enabled, // coordinateDisabled handles all the endpoints when coordinates are not enabled,
// returning an error message. // returning an error message.
func coordinateDisabled(resp http.ResponseWriter, req *http.Request) (interface{}, error) { func coordinateDisabled(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
resp.WriteHeader(401) resp.WriteHeader(401)
resp.Write([]byte("Coordinate support disabled")) fmt.Fprint(resp, "Coordinate support disabled")
return nil, nil return nil, nil
} }

View File

@ -2,6 +2,7 @@ package agent
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"net/http" "net/http"
"strconv" "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/") event.Name = strings.TrimPrefix(req.URL.Path, "/v1/event/fire/")
if event.Name == "" { if event.Name == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing name")) fmt.Fprint(resp, "Missing name")
return nil, nil 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 err := s.agent.UserEvent(dc, token, event); err != nil {
if strings.Contains(err.Error(), permissionDenied) { if strings.Contains(err.Error(), permissionDenied) {
resp.WriteHeader(403) resp.WriteHeader(403)
resp.Write([]byte(permissionDenied)) fmt.Fprint(resp, permissionDenied)
return nil, nil return nil, nil
} }
resp.WriteHeader(500) resp.WriteHeader(500)

View File

@ -1,6 +1,7 @@
package agent package agent
import ( import (
"fmt"
"net/http" "net/http"
"strings" "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/") args.State = strings.TrimPrefix(req.URL.Path, "/v1/health/state/")
if args.State == "" { if args.State == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing check state")) fmt.Fprint(resp, "Missing check state")
return nil, nil 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/") args.Node = strings.TrimPrefix(req.URL.Path, "/v1/health/node/")
if args.Node == "" { if args.Node == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing node name")) fmt.Fprint(resp, "Missing node name")
return nil, nil 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/") args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/health/checks/")
if args.ServiceName == "" { if args.ServiceName == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing service name")) fmt.Fprint(resp, "Missing service name")
return nil, nil 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/") args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/health/service/")
if args.ServiceName == "" { if args.ServiceName == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing service name")) fmt.Fprint(resp, "Missing service name")
return nil, nil return nil, nil
} }

View File

@ -380,7 +380,7 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque
} }
resp.WriteHeader(code) resp.WriteHeader(code)
resp.Write([]byte(err.Error())) fmt.Fprint(resp, err.Error())
return 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 // Give them something helpful if there's no UI so they at least know
// what this server is. // what this server is.
if !s.IsUIEnabled() { if !s.IsUIEnabled() {
resp.Write([]byte("Consul Agent")) fmt.Fprint(resp, "Consul Agent")
return return
} }
@ -508,7 +508,7 @@ func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOpti
dur, err := time.ParseDuration(wait) dur, err := time.ParseDuration(wait)
if err != nil { if err != nil {
resp.WriteHeader(http.StatusBadRequest) // 400 resp.WriteHeader(http.StatusBadRequest) // 400
resp.Write([]byte("Invalid wait time")) fmt.Fprint(resp, "Invalid wait time")
return true return true
} }
b.MaxQueryTime = dur 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) index, err := strconv.ParseUint(idx, 10, 64)
if err != nil { if err != nil {
resp.WriteHeader(http.StatusBadRequest) // 400 resp.WriteHeader(http.StatusBadRequest) // 400
resp.Write([]byte("Invalid index")) fmt.Fprint(resp, "Invalid index")
return true return true
} }
b.MinQueryIndex = index b.MinQueryIndex = index
@ -537,7 +537,7 @@ func parseConsistency(resp http.ResponseWriter, req *http.Request, b *structs.Qu
} }
if b.AllowStale && b.RequireConsistent { if b.AllowStale && b.RequireConsistent {
resp.WriteHeader(http.StatusBadRequest) // 400 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 true
} }
return false return false

View File

@ -185,7 +185,7 @@ func (s *HTTPServer) KVSPut(resp http.ResponseWriter, req *http.Request, args *s
// Check the content-length // Check the content-length
if req.ContentLength > maxKVSize { if req.ContentLength > maxKVSize {
resp.WriteHeader(413) 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 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 { func missingKey(resp http.ResponseWriter, args *structs.KeyRequest) bool {
if args.Key == "" { if args.Key == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing key name")) fmt.Fprint(resp, "Missing key name")
return true return true
} }
return false return false
@ -275,7 +275,7 @@ func conflictingFlags(resp http.ResponseWriter, req *http.Request, flags ...stri
if _, ok := params[conflict]; ok { if _, ok := params[conflict]; ok {
if found { if found {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Conflicting flags: " + params.Encode())) fmt.Fprint(resp, "Conflicting flags: "+params.Encode())
return true return true
} }
found = true found = true

View File

@ -4,13 +4,13 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
multierror "github.com/hashicorp/go-multierror" multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/raft" "github.com/hashicorp/raft"
"strings"
) )
// OperatorRaftConfiguration is used to inspect the current Raft configuration. // 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 { if !hasID && !hasAddress {
resp.WriteHeader(http.StatusBadRequest) 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 return nil, nil
} }
if hasID && hasAddress { if hasID && hasAddress {
resp.WriteHeader(http.StatusBadRequest) 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 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 req.Method == "POST" || req.Method == "PUT" || req.Method == "DELETE" {
if err := decodeBody(req, &args, nil); err != nil { if err := decodeBody(req, &args, nil); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil return nil, nil
} }
} }
@ -102,14 +102,14 @@ func (s *HTTPServer) OperatorKeyringEndpoint(resp http.ResponseWriter, req *http
n, err := strconv.Atoi(relayFactor) n, err := strconv.Atoi(relayFactor)
if err != nil { if err != nil {
resp.WriteHeader(400) 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 return nil, nil
} }
args.RelayFactor, err = ParseRelayFactor(n) args.RelayFactor, err = ParseRelayFactor(n)
if err != nil { if err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Invalid relay factor: %v", err))) fmt.Fprintf(resp, "Invalid relay factor: %v", err)
return nil, nil return nil, nil
} }
} }
@ -223,7 +223,7 @@ func (s *HTTPServer) OperatorAutopilotConfiguration(resp http.ResponseWriter, re
var conf api.AutopilotConfiguration var conf api.AutopilotConfiguration
if err := decodeBody(req, &conf, FixupConfigDurations); err != nil { if err := decodeBody(req, &conf, FixupConfigDurations); err != nil {
resp.WriteHeader(400) 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 return nil, nil
} }
@ -242,7 +242,7 @@ func (s *HTTPServer) OperatorAutopilotConfiguration(resp http.ResponseWriter, re
casVal, err := strconv.ParseUint(params.Get("cas"), 10, 64) casVal, err := strconv.ParseUint(params.Get("cas"), 10, 64)
if err != nil { if err != nil {
resp.WriteHeader(400) 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 return nil, nil
} }
args.Config.ModifyIndex = casVal args.Config.ModifyIndex = casVal

View File

@ -31,7 +31,7 @@ func (s *HTTPServer) preparedQueryCreate(resp http.ResponseWriter, req *http.Req
if req.ContentLength > 0 { if req.ContentLength > 0 {
if err := decodeBody(req, &args.Query, nil); err != nil { if err := decodeBody(req, &args.Query, nil); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil return nil, nil
} }
} }
@ -116,7 +116,7 @@ func (s *HTTPServer) preparedQueryExecute(id string, resp http.ResponseWriter, r
// the specific error type. // the specific error type.
if err.Error() == consul.ErrQueryNotFound.Error() { if err.Error() == consul.ErrQueryNotFound.Error() {
resp.WriteHeader(404) resp.WriteHeader(404)
resp.Write([]byte(err.Error())) fmt.Fprint(resp, err.Error())
return nil, nil return nil, nil
} }
return nil, err return nil, err
@ -161,7 +161,7 @@ func (s *HTTPServer) preparedQueryExplain(id string, resp http.ResponseWriter, r
// the specific error type. // the specific error type.
if err.Error() == consul.ErrQueryNotFound.Error() { if err.Error() == consul.ErrQueryNotFound.Error() {
resp.WriteHeader(404) resp.WriteHeader(404)
resp.Write([]byte(err.Error())) fmt.Fprint(resp, err.Error())
return nil, nil return nil, nil
} }
return nil, err return nil, err
@ -185,7 +185,7 @@ func (s *HTTPServer) preparedQueryGet(id string, resp http.ResponseWriter, req *
// the specific error type. // the specific error type.
if err.Error() == consul.ErrQueryNotFound.Error() { if err.Error() == consul.ErrQueryNotFound.Error() {
resp.WriteHeader(404) resp.WriteHeader(404)
resp.Write([]byte(err.Error())) fmt.Fprint(resp, err.Error())
return nil, nil return nil, nil
} }
return nil, err return nil, err
@ -203,7 +203,7 @@ func (s *HTTPServer) preparedQueryUpdate(id string, resp http.ResponseWriter, re
if req.ContentLength > 0 { if req.ContentLength > 0 {
if err := decodeBody(req, &args.Query, nil); err != nil { if err := decodeBody(req, &args.Query, nil); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil return nil, nil
} }
} }

View File

@ -52,7 +52,7 @@ func (s *HTTPServer) SessionCreate(resp http.ResponseWriter, req *http.Request)
if req.ContentLength > 0 { if req.ContentLength > 0 {
if err := decodeBody(req, &args.Session, FixupLockDelay); err != nil { if err := decodeBody(req, &args.Session, FixupLockDelay); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil 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/") args.Session.ID = strings.TrimPrefix(req.URL.Path, "/v1/session/destroy/")
if args.Session.ID == "" { if args.Session.ID == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing session")) fmt.Fprint(resp, "Missing session")
return nil, nil 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/") args.Session = strings.TrimPrefix(req.URL.Path, "/v1/session/renew/")
if args.Session == "" { if args.Session == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing session")) fmt.Fprint(resp, "Missing session")
return nil, nil return nil, nil
} }
@ -161,7 +161,7 @@ func (s *HTTPServer) SessionRenew(resp http.ResponseWriter, req *http.Request) (
return nil, err return nil, err
} else if out.Sessions == nil { } else if out.Sessions == nil {
resp.WriteHeader(404) 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 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/") args.Session = strings.TrimPrefix(req.URL.Path, "/v1/session/info/")
if args.Session == "" { if args.Session == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing session")) fmt.Fprint(resp, "Missing session")
return nil, nil 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/") args.Node = strings.TrimPrefix(req.URL.Path, "/v1/session/node/")
if args.Node == "" { if args.Node == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing node name")) fmt.Fprint(resp, "Missing node name")
return nil, nil return nil, nil
} }

View File

@ -1,9 +1,10 @@
package agent package agent
import ( import (
"github.com/hashicorp/consul/testutil"
"os" "os"
"testing" "testing"
"github.com/hashicorp/consul/testutil"
) )
func TestStatusLeader(t *testing.T) { func TestStatusLeader(t *testing.T) {

View File

@ -93,7 +93,7 @@ func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (st
var ops api.TxnOps var ops api.TxnOps
if err := decodeBody(req, &ops, fixupKVOps); err != nil { if err := decodeBody(req, &ops, fixupKVOps); err != nil {
resp.WriteHeader(http.StatusBadRequest) 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 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. // transaction in order to curb abuse.
if size := len(ops); size > maxTxnOps { if size := len(ops); size > maxTxnOps {
resp.WriteHeader(http.StatusRequestEntityTooLarge) resp.WriteHeader(http.StatusRequestEntityTooLarge)
resp.Write([]byte(fmt.Sprintf("Transaction contains too many operations (%d > %d)", fmt.Fprintf(resp, "Transaction contains too many operations (%d > %d)",
size, maxTxnOps))) size, maxTxnOps)
return nil, 0, false return nil, 0, false
} }
@ -116,8 +117,9 @@ func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (st
if in.KV != nil { if in.KV != nil {
if size := len(in.KV.Value); size > maxKVSize { if size := len(in.KV.Value); size > maxKVSize {
resp.WriteHeader(http.StatusRequestEntityTooLarge) resp.WriteHeader(http.StatusRequestEntityTooLarge)
resp.Write([]byte(fmt.Sprintf("Value for key %q is too large (%d > %d bytes)", fmt.Fprintf(resp, "Value for key %q is too large (%d > %d bytes)",
in.KV.Key, size, maxKVSize))) in.KV.Key, size, maxKVSize)
return nil, 0, false return nil, 0, false
} else { } else {
netKVSize += size 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. // Enforce an overall size limit to help prevent abuse.
if netKVSize > maxKVSize { if netKVSize > maxKVSize {
resp.WriteHeader(http.StatusRequestEntityTooLarge) resp.WriteHeader(http.StatusRequestEntityTooLarge)
resp.Write([]byte(fmt.Sprintf("Cumulative size of key data is too large (%d > %d bytes)", fmt.Fprintf(resp, "Cumulative size of key data is too large (%d > %d bytes)",
netKVSize, maxKVSize))) netKVSize, maxKVSize)
return nil, 0, false return nil, 0, false
} }

View File

@ -1,6 +1,7 @@
package agent package agent
import ( import (
"fmt"
"net/http" "net/http"
"sort" "sort"
"strings" "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/") args.Node = strings.TrimPrefix(req.URL.Path, "/v1/internal/ui/node/")
if args.Node == "" { if args.Node == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte("Missing node name")) fmt.Fprint(resp, "Missing node name")
return nil, nil return nil, nil
} }