From 848938ad48ca7f6416e9f55a0263cdcfaf6d510c Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Tue, 28 Jan 2020 16:22:40 -0500 Subject: [PATCH] Output proper HTTP status codes for Txn requests that are too large (#7157) --- agent/txn_endpoint.go | 2 ++ agent/txn_endpoint_test.go | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/agent/txn_endpoint.go b/agent/txn_endpoint.go index b90c7af581..2419b34661 100644 --- a/agent/txn_endpoint.go +++ b/agent/txn_endpoint.go @@ -69,9 +69,11 @@ func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (st sizeStr := req.Header.Get("Content-Length") if sizeStr != "" { if size, err := strconv.Atoi(sizeStr); err != nil { + resp.WriteHeader(http.StatusBadRequest) fmt.Fprintf(resp, "Failed to parse Content-Length: %v", err) return nil, 0, false } else if size > int(s.agent.config.KVMaxValueSize) { + resp.WriteHeader(http.StatusRequestEntityTooLarge) fmt.Fprintf(resp, "Request body too large, max size: %v bytes", s.agent.config.KVMaxValueSize) return nil, 0, false } diff --git a/agent/txn_endpoint_test.go b/agent/txn_endpoint_test.go index 2e7655b479..c3fec4f55d 100644 --- a/agent/txn_endpoint_test.go +++ b/agent/txn_endpoint_test.go @@ -39,7 +39,7 @@ func TestTxnEndpoint_Bad_JSON(t *testing.T) { func TestTxnEndpoint_Bad_Size_Item(t *testing.T) { t.Parallel() - testIt := func(agent *TestAgent, wantPass bool) { + testIt := func(t *testing.T, agent *TestAgent, wantPass bool) { value := strings.Repeat("X", 3*raft.SuggestedMaxDataSize) value = base64.StdEncoding.EncodeToString([]byte(value)) buf := bytes.NewBuffer([]byte(fmt.Sprintf(` @@ -54,6 +54,7 @@ func TestTxnEndpoint_Bad_Size_Item(t *testing.T) { ] `, value))) req, _ := http.NewRequest("PUT", "/v1/txn", buf) + req.Header.Add("Content-Length", fmt.Sprintf("%d", buf.Len())) resp := httptest.NewRecorder() if _, err := agent.srv.Txn(resp, req); err != nil { t.Fatalf("err: %v", err) @@ -68,13 +69,13 @@ func TestTxnEndpoint_Bad_Size_Item(t *testing.T) { t.Run("toobig", func(t *testing.T) { a := NewTestAgent(t, t.Name(), "") - testIt(a, false) + testIt(t, a, false) a.Shutdown() }) t.Run("allowed", func(t *testing.T) { a := NewTestAgent(t, t.Name(), "limits = { kv_max_value_size = 123456789 }") - testIt(a, true) + testIt(t, a, true) a.Shutdown() }) }