Skip to content

Commit

Permalink
Support cases where the request is done with transfer-encoding chunked (
Browse files Browse the repository at this point in the history
grpc-ecosystem#589)

* Support cases where the request is done with transfer-encoding chunked

PR grpc-ecosystem#527 was put in place to fix an issue where an empty request body
would cause an empty message to be sent down to the GRPC service
(instead of failing). The fix at the time was to check that the
ContentLength was >0, but this doesn't take into consideration
transfer-encoding chunked POSTs. Since this patch all chunked POSTs
no longer unmarshal the message (as the content-length was 0).

My proposed fix is instead to always call Decode and simply ignore EOF
errors (as we still want to pass the un-filled struct down). I have
tested that things such as partial json blobs (something like '{') don'
t return EOF (they have their own json error).
  • Loading branch information
jacksontj authored and achew22 committed Apr 18, 2018
1 parent 6d4c5f7 commit bb00f27
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 84 deletions.
36 changes: 12 additions & 24 deletions examples/examplepb/a_bit_of_everything.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions examples/examplepb/echo_service.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 24 additions & 48 deletions examples/examplepb/flow_combination.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions examples/examplepb/wrappers.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions examples/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"context"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/empty"
Expand Down Expand Up @@ -667,6 +668,20 @@ func testAdditionalBindings(t *testing.T, port int) {
}
return resp
},
func() *http.Response {
r, w := io.Pipe()
go func() {
defer w.Close()
w.Write([]byte(`"hello"`))
}()
url := fmt.Sprintf("http://localhost:%d/v2/example/echo", port)
resp, err := http.Post(url, "application/json", r)
if err != nil {
t.Errorf("http.Post(%q, %q, %q) failed with %v; want success", url, "application/json", `"hello"`, err)
return nil
}
return resp
},
func() *http.Response {
url := fmt.Sprintf("http://localhost:%d/v2/example/echo?value=hello", port)
resp, err := http.Get(url)
Expand Down
6 changes: 2 additions & 4 deletions protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,8 @@ var (
var protoReq {{.Method.RequestType.GoType .Method.Service.File.GoPkg.Path}}
var metadata runtime.ServerMetadata
{{if .Body}}
if req.ContentLength > 0 {
if err := marshaler.NewDecoder(req.Body).Decode(&{{.Body.AssignableExpr "protoReq"}}); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := marshaler.NewDecoder(req.Body).Decode(&{{.Body.AssignableExpr "protoReq"}}); err != nil && err != io.EOF {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
{{end}}
{{if .PathParams}}
Expand Down

0 comments on commit bb00f27

Please sign in to comment.