diff --git a/cmd/protoc-gen-go-http/template.go b/cmd/protoc-gen-go-http/template.go index 618319563..c4f5cc44b 100644 --- a/cmd/protoc-gen-go-http/template.go +++ b/cmd/protoc-gen-go-http/template.go @@ -18,11 +18,6 @@ func Register{{.ServiceType}}HTTPServer(s http1.ServiceRegistrar, srv {{.Service {{range .Methods}} func _HTTP_{{$.ServiceType}}_{{.Name}}_{{.Num}}(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error) (interface{}, error) { var in {{.Request}} -{{if ne (len .Vars) 0}} - if err := http1.BindVars(req, &in); err != nil { - return nil, err - } -{{end}} {{if eq .Body ""}} if err := http1.BindForm(req, &in); err != nil { return nil, err @@ -35,6 +30,11 @@ func _HTTP_{{$.ServiceType}}_{{.Name}}_{{.Num}}(srv interface{}, ctx context.Con if err := dec(in{{.Body}}); err != nil { return nil, err } +{{end}} +{{if ne (len .Vars) 0}} + if err := http1.BindVars(req, &in); err != nil { + return nil, err + } {{end}} out, err := srv.({{$.ServiceType}}Server).{{.Name}}(ctx, &in) if err != nil { diff --git a/transport/http/default.go b/transport/http/default.go index 61f7e1576..d1a59e95f 100644 --- a/transport/http/default.go +++ b/transport/http/default.go @@ -25,9 +25,9 @@ func contentSubtype(contentType string) string { // guaranteed since != baseContentType and has baseContentType prefix switch contentType[len(baseContentType)] { case '/', ';': - // this will return true for "application/grpc+" or "application/grpc;" - // which the previous validContentType function tested to be valid, so we - // just say that no content-subtype is specified in this case + if i := strings.Index(contentType, ";"); i != -1 { + return contentType[len(baseContentType)+1 : i] + } return contentType[len(baseContentType)+1:] default: return "" diff --git a/transport/http/default_test.go b/transport/http/default_test.go new file mode 100644 index 000000000..044f5be2e --- /dev/null +++ b/transport/http/default_test.go @@ -0,0 +1,23 @@ +package http + +import "testing" + +func TestSubtype(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {"application/json", "json"}, + {"application/json;", "json"}, + {"application/json; charset=utf-8", "json"}, + {"application/", ""}, + {"application", ""}, + {"foo", ""}, + {"", ""}, + } + for _, test := range tests { + if contentSubtype(test.input) != test.expected { + t.Errorf("expected %s got %s", test.expected, test.input) + } + } +}