-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix redirects following POST/PUT/PATCH requests #2948
Conversation
@swift-ci test |
I ran the test case against macOS Foundation (using the |
It seems several of the tests for URLSession fail with a timeout (when testing against macOS Foundation). This happens on commits prior to mine as well so I don't think it's related to the changes that I've made. Could it be an issue with the test server implementation? |
The reason some of the tests hang on macOS is that the delegates (when used) should be calling a completion handler with My current plan for So I would suggest re-examining your test to see why it is failing on macOS as it may indicate another bug in the Linux code. |
It seems |
@swift-ci test |
1 similar comment
@swift-ci test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine now, could you just squash the commits down to one please, and then this can be merged in. Thanks
Fix test on Darwin Fix redirect status code handling
@swift-ci test and merge |
I’ve stumbled upon a problem in
URLSession
when a redirect occurs following a request with a request body (POST/PUT/PATCH).Say we create a
URLRequest
with a httpBody and an httpMethod of "POST".When the POST request is made, a URLSessionTask is initialized with the httpBody from the URLRequest.
Later, when the server responds with a redirect to this request, a new request is made but the original task is reused. This involves calling
startNewTransfer
, which in turn looks up any existing known body in theknownBody
property.The result is that the new request, which is supposed to follow the redirect, also includes the original request data. This trips the following check.
This PR attempts to work around the issue by unsetting the
knownBody
onURLSessionTask
before a redirect request is performed.I also changed the behavior of how the redirect request is constructed. Previously, the original request (including httpBody) was copied and the httpMethod changed to GET. This is an invalid request.
Even though this turned out not to be the source of the problem (because the httpBody on the redirect request is never used), it doesn’t make sense for Foundation to be producing invalid requests internally.