Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/net/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ func (t *Transport) roundTrip(req *Request) (*Response, error) {
return nil, &badStringError{"unsupported protocol scheme", scheme}
}
if req.Method != "" && !validMethod(req.Method) {
req.closeBody()
return nil, fmt.Errorf("net/http: invalid method %q", req.Method)
}
if req.URL.Host == "" {
Expand Down
29 changes: 29 additions & 0 deletions src/net/http/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5719,3 +5719,32 @@ func TestInvalidHeaderResponse(t *testing.T) {
t.Errorf(`bad "Foo " header value: %q, want %q`, v, "bar")
}
}

type bodyCloser bool

func (bc *bodyCloser) Close() error {
*bc = true
return nil
}
func (bc *bodyCloser) Read(b []byte) (n int, err error) {
return 0, io.EOF
}

func TestInvalidMethodClosesBody(t *testing.T) {
cst := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
defer cst.Close()
var bc bodyCloser
u, _ := url.Parse(cst.URL)
req := &Request{
Method: " ",
URL: u,
Body: &bc,
}
_, err := DefaultClient.Do(req)
if err == nil {
t.Fatal("Expected an error")
}
if !bc {
t.Fatal("Expected body to have been closed")
}
}