Commit 39cfb760 authored by Luke Young's avatar Luke Young Committed by Emmanuel Odeke

net/http: make Transport.roundTrip close body on invalid method

Updates #35015

Change-Id: Ibfe8f72ed3887ca88ce9c1d8a29dacda72f3fe17
GitHub-Last-Rev: 4bfc56e71660ad9624ac5eb594b3afd0d221c99d
GitHub-Pull-Request: golang/go#35014
Reviewed-on: https://go-review.googlesource.com/c/go/+/202237Reviewed-by: default avatarEmmanuel Odeke <emm.odeke@gmail.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 72275c0d
......@@ -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 == "" {
......
......@@ -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")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment