Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transport: Make error-handling for bad HTTP method consistent between HTTP/2 server transport and handler server transport #6989

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions internal/transport/handler_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ import (
// inside an http.Handler, or writes an HTTP error to w and returns an error.
// It requires that the http Server supports HTTP/2.
func NewServerHandlerTransport(w http.ResponseWriter, r *http.Request, stats []stats.Handler) (ServerTransport, error) {
if r.ProtoMajor != 2 {
msg := "gRPC requires HTTP/2"
http.Error(w, msg, http.StatusBadRequest)
return nil, errors.New(msg)
}
if r.Method != "POST" {
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
msg := fmt.Sprintf("invalid gRPC request method %q", r.Method)
http.Error(w, msg, http.StatusBadRequest)
http.Error(w, msg, http.StatusMethodNotAllowed)
return nil, errors.New(msg)
}
contentType := r.Header.Get("Content-Type")
Expand All @@ -69,6 +65,11 @@ func NewServerHandlerTransport(w http.ResponseWriter, r *http.Request, stats []s
http.Error(w, msg, http.StatusUnsupportedMediaType)
return nil, errors.New(msg)
}
if r.ProtoMajor != 2 {
msg := "gRPC requires HTTP/2"
http.Error(w, msg, http.StatusHTTPVersionNotSupported)
return nil, errors.New(msg)
}
if _, ok := w.(http.Flusher); !ok {
msg := "gRPC requires a ResponseWriter supporting http.Flusher"
http.Error(w, msg, http.StatusInternalServerError)
Expand Down
22 changes: 12 additions & 10 deletions internal/transport/handler_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ func (s) TestHandlerTransport_NewServerHandlerTransport(t *testing.T) {
check func(*serverHandlerTransport, *testCase) error
}
tests := []testCase{
{
name: "http/1.1",
req: &http.Request{
ProtoMajor: 1,
ProtoMinor: 1,
},
wantErr: "gRPC requires HTTP/2",
wantErrCode: http.StatusBadRequest,
},
{
name: "bad method",
req: &http.Request{
Expand All @@ -67,7 +58,7 @@ func (s) TestHandlerTransport_NewServerHandlerTransport(t *testing.T) {
Header: http.Header{},
},
wantErr: `invalid gRPC request method "GET"`,
wantErrCode: http.StatusBadRequest,
wantErrCode: http.StatusMethodNotAllowed,
},
{
name: "bad content type",
Expand All @@ -81,6 +72,17 @@ func (s) TestHandlerTransport_NewServerHandlerTransport(t *testing.T) {
wantErr: `invalid gRPC request content-type "application/foo"`,
wantErrCode: http.StatusUnsupportedMediaType,
},
{
name: "http/1.1",
req: &http.Request{
ProtoMajor: 1,
ProtoMinor: 1,
Method: "POST",
Header: http.Header{"Content-Type": []string{"application/grpc"}},
},
wantErr: "gRPC requires HTTP/2",
wantErrCode: http.StatusHTTPVersionNotSupported,
},
{
name: "not flusher",
req: &http.Request{
Expand Down
Loading