From d890ddbfd9125f36bc7ff70cc7a584dd038e8432 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Fri, 8 Nov 2019 16:04:15 +0100 Subject: [PATCH] api: check response content length before decoding. The API decodeBody function will now check the content length before attempting to decode. If the length is zero, and the out interface is nil then it is safe to assume the API call is not returning any data to the user. This allows us to better handle passing nil to API calls in a single place. --- api/api.go | 13 +++++++++++-- api/nodes.go | 6 ++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/api/api.go b/api/api.go index 3951ed13d63a..95fcd7344255 100644 --- a/api/api.go +++ b/api/api.go @@ -5,6 +5,7 @@ import ( "compress/gzip" "crypto/tls" "encoding/json" + "errors" "fmt" "io" "net" @@ -930,8 +931,16 @@ func parseWriteMeta(resp *http.Response, q *WriteMeta) error { // decodeBody is used to JSON decode a body func decodeBody(resp *http.Response, out interface{}) error { - dec := json.NewDecoder(resp.Body) - return dec.Decode(out) + switch resp.ContentLength { + case 0: + if out == nil { + return nil + } + return errors.New("Got 0 byte response with non-nil decode object") + default: + dec := json.NewDecoder(resp.Body) + return dec.Decode(out) + } } // encodeBody is used to encode a request body diff --git a/api/nodes.go b/api/nodes.go index 4a264eb4d330..7072dadc3f78 100644 --- a/api/nodes.go +++ b/api/nodes.go @@ -413,17 +413,15 @@ func (n *Nodes) Stats(nodeID string, q *QueryOptions) (*HostStats, error) { } func (n *Nodes) GC(nodeID string, q *QueryOptions) error { - var resp struct{} path := fmt.Sprintf("/v1/client/gc?node_id=%s", nodeID) - _, err := n.client.query(path, &resp, q) + _, err := n.client.query(path, nil, q) return err } // TODO Add tests func (n *Nodes) GcAlloc(allocID string, q *QueryOptions) error { - var resp struct{} path := fmt.Sprintf("/v1/client/allocation/%s/gc", allocID) - _, err := n.client.query(path, &resp, q) + _, err := n.client.query(path, nil, q) return err }