-
Notifications
You must be signed in to change notification settings - Fork 18k
net/http: RoundTrip unexpectedly changes Request #39533
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
Comments
Even prior to this change, Tracking the history of this behavior, I believe it originates with https://golang.org/cl/33971, which causes the HTTP2 transport to replace the request when retrying. My first thought was that it seems odd to replace the entire
Replacing the My inclination is to say that "never" is the right answer, since it causes the least disruption to existing users. If that's the case, we could:
|
Requests are read-only data, immutable. I believe it is allowed today to call RoundTrip from multiple goroutines using the same request (provided Body = NoBody or nil). Mutating the Request would break that, introducing races. The main time that Requests change and the reason Response records the final Request is when http.Client.Do follows redirects. That's the time when you most often need to look at the Response - to find out what the final fetched URL even was (if you care). Since they can change in Do, I'm not convinced it's important they can't change in RoundTrip. Maybe we can say that the Response returned by RoundTrip always returns the original Request? I'm not sure. It's either that or decide the tests are broken. You can't mutate in place. /cc @bradfitz |
Change https://golang.org/cl/237560 mentions this issue: |
We'd only need to mutate the Request if the Body is neither NoBody nor nil, which are the cases where you aren't allowed to call RoundTrip from multiple goroutines. If immutable requests are an invariant, though, then it is what it is. |
@gopherbot Please backport to Go 1.14. This is a fixup to a previous fix (CL 242117) for an approved backport issue #39279. We can't submit that CL without this one. This needs to be backported to Go 1.14 only, since the fix is already included in Go 1.15. |
Backport issue(s) opened: #40973 (for 1.14). Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases. |
Change https://golang.org/cl/249880 mentions this issue: |
…equests Ensure that the exact Request passed to Transport.RoundTrip is returned in the Response. Do not replace the Request with a copy when resetting the request body. Updates #39533. Fixes #40973. Change-Id: Ie6fb080c24b0f6625b0761b7aa542af3d2411817 Reviewed-on: https://go-review.googlesource.com/c/go/+/237560 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/249880 Run-TryBot: Dmitri Shuralyov <[email protected]> Reviewed-by: Damien Neil <[email protected]>
https://go-review.googlesource.com/c/go/+/234894 causes
(*http.Transport).RoundTrip
to return a different*http.Request
in the*http.Response
than was passed to it, under some circumstances.The documentation for
(http.Response).Request
says, "Request is the request that was sent to obtain this Response." While this doesn't precisely specify that it's the exact*Request
passed toRoundTrip
, that's probably close enough to consider this an incorrect API change.Discovered because this breaks code which keeps a map of
*http.Request
s.The text was updated successfully, but these errors were encountered: