net/http: don't crash in Request.WithContext if Request.URL is nil

Fixes #20601

Change-Id: I296d50dc5210a735a2a65d64bfef05d14c93057b
Reviewed-on: https://go-review.googlesource.com/45073
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Rhys Hiltner <rhys@justin.tv>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Brad Fitzpatrick 2017-06-07 18:13:38 +00:00
parent 0aede73917
commit 7579f96676
2 changed files with 12 additions and 3 deletions

View File

@ -333,9 +333,11 @@ func (r *Request) WithContext(ctx context.Context) *Request {
// Deep copy the URL because it isn't
// a map and the URL is mutable by users
// of WithContext.
r2URL := new(url.URL)
*r2URL = *r.URL
r2.URL = r2URL
if r.URL != nil {
r2URL := new(url.URL)
*r2URL = *r.URL
r2.URL = r2URL
}
return r2
}

View File

@ -799,6 +799,13 @@ func TestWithContextDeepCopiesURL(t *testing.T) {
if firstURL == secondURL {
t.Errorf("unexpected change to original request's URL")
}
// And also check we don't crash on nil (Issue 20601)
req.URL = nil
reqCopy = req.WithContext(context.Background())
if reqCopy.URL != nil {
t.Error("expected nil URL in cloned request")
}
}
// verify that NewRequest sets Request.GetBody and that it works