mirror of
https://github.com/golang/go.git
synced 2025-05-18 22:04:38 +00:00
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:
parent
0aede73917
commit
7579f96676
@ -333,9 +333,11 @@ func (r *Request) WithContext(ctx context.Context) *Request {
|
|||||||
// Deep copy the URL because it isn't
|
// Deep copy the URL because it isn't
|
||||||
// a map and the URL is mutable by users
|
// a map and the URL is mutable by users
|
||||||
// of WithContext.
|
// of WithContext.
|
||||||
|
if r.URL != nil {
|
||||||
r2URL := new(url.URL)
|
r2URL := new(url.URL)
|
||||||
*r2URL = *r.URL
|
*r2URL = *r.URL
|
||||||
r2.URL = r2URL
|
r2.URL = r2URL
|
||||||
|
}
|
||||||
|
|
||||||
return r2
|
return r2
|
||||||
}
|
}
|
||||||
|
@ -799,6 +799,13 @@ func TestWithContextDeepCopiesURL(t *testing.T) {
|
|||||||
if firstURL == secondURL {
|
if firstURL == secondURL {
|
||||||
t.Errorf("unexpected change to original request's URL")
|
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
|
// verify that NewRequest sets Request.GetBody and that it works
|
||||||
|
Loading…
x
Reference in New Issue
Block a user