From 3da9ad432862e47e038de0084d38ff67378b85f6 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Tue, 26 Sep 2017 18:16:44 +0300 Subject: [PATCH] Using io.LimitReader as the way to control size of request body with respect to content length --- api/agent/protocol/json.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/api/agent/protocol/json.go b/api/agent/protocol/json.go index 5531883e6..a3a457f5f 100644 --- a/api/agent/protocol/json.go +++ b/api/agent/protocol/json.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "strconv" ) // JSONInput is what's sent into the function @@ -32,13 +33,18 @@ func (p *JSONProtocol) IsStreamable() bool { } func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error { - // TODO content-length or chunked encoding var body bytes.Buffer if req.Body != nil { var dest io.Writer = &body - // TODO copy w/ ctx and check err - io.Copy(dest, req.Body) + // TODO copy w/ ctx + nBytes, _ := strconv.ParseInt( + req.Header.Get("Content-Length"), 10, 64) + _, err := io.Copy(dest, io.LimitReader(req.Body, nBytes)) + if err != nil { + // TODO: maybe mask this error if favour of something different + return err + } } // convert to JSON func format