Skip to content

Commit 14464bf

Browse files
committed
Add conditions to ensure Bind succeeds with Transfer-Encoding: chunked.
1 parent 3b01785 commit 14464bf

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

bind.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ func (b *DefaultBinder) BindQueryParams(c Context, i interface{}) error {
6767
// See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm
6868
func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {
6969
req := c.Request()
70-
if req.ContentLength <= 0 {
70+
var isChunked bool
71+
for _, enc := range req.TransferEncoding {
72+
if enc == "chunked" {
73+
isChunked = true
74+
break
75+
}
76+
}
77+
if req.ContentLength <= 0 && !isChunked {
7178
return
7279
}
7380

bind_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"mime/multipart"
1414
"net/http"
1515
"net/http/httptest"
16+
"net/http/httputil"
1617
"net/url"
1718
"reflect"
1819
"strconv"
@@ -941,6 +942,7 @@ func TestDefaultBinder_BindBody(t *testing.T) {
941942
givenMethod string
942943
givenContentType string
943944
whenNoPathParams bool
945+
whenChunkedBody bool
944946
whenBindTarget interface{}
945947
expect interface{}
946948
expectError string
@@ -1068,6 +1070,15 @@ func TestDefaultBinder_BindBody(t *testing.T) {
10681070
givenContent: http.NoBody,
10691071
expect: &Node{ID: 0, Node: ""},
10701072
},
1073+
{
1074+
name: "ok, JSON POST bind to struct with: path + query + chunked body",
1075+
givenURL: "/api/real_node/endpoint?node=xxx",
1076+
givenMethod: http.MethodPost,
1077+
givenContentType: MIMEApplicationJSON,
1078+
givenContent: httputil.NewChunkedReader(strings.NewReader("18\r\n" + `{"id": 1, "node": "zzz"}` + "\r\n0\r\n")),
1079+
whenChunkedBody: true,
1080+
expect: &Node{ID: 1, Node: "zzz"},
1081+
},
10711082
}
10721083

10731084
for _, tc := range testCases {
@@ -1083,6 +1094,10 @@ func TestDefaultBinder_BindBody(t *testing.T) {
10831094
case MIMEApplicationJSON:
10841095
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
10851096
}
1097+
if tc.whenChunkedBody {
1098+
req.ContentLength = -1
1099+
req.TransferEncoding = append(req.TransferEncoding, "chunked")
1100+
}
10861101
rec := httptest.NewRecorder()
10871102
c := e.NewContext(req, rec)
10881103

0 commit comments

Comments
 (0)