Skip to content
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

Merged
merged 1 commit into from
Jan 14, 2021
Merged

Fix redirects following POST/PUT/PATCH requests #2948

merged 1 commit into from
Jan 14, 2021

Conversation

agisboye
Copy link
Contributor

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 the knownBody 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 on URLSessionTask 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.

@tomerd tomerd added 5.3 and removed 5.3 labels Dec 18, 2020
@spevans
Copy link
Contributor

spevans commented Dec 20, 2020

@swift-ci test

@spevans
Copy link
Contributor

spevans commented Dec 22, 2020

I ran the test case against macOS Foundation (using the DarwinCompatibilityTests project) and it timed-out. I don't know if this is a bug in macOS or an incorrect test case.

@agisboye
Copy link
Contributor Author

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?

@spevans
Copy link
Contributor

spevans commented Jan 4, 2021

The reason some of the tests hang on macOS is that the delegates (when used) should be calling a completion handler with .allow to keep the transaction progressing. There is a bug in URLSession on Linux that doesn't require this completion handler call so the buggy tests still work on Linux. Other tests are failing for different reasons.

My current plan for URLSession is to re-enable the current tests on CI (except on Windows where there are still some issues) and then fix up each test to work on macOS, while fixing URLSession to correctly pass the fixed tests and fail with the broken tests. Once all of the tests are passing on macOS then the other bugs can be fixed up.

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.

@agisboye
Copy link
Contributor Author

agisboye commented Jan 12, 2021

It seems DataTask never calls the completion handler here.
I'd rather not change the behavior of DataTask since it's used by so many of the tests. Instead I'm using HTTPRedirectionDataTask.

@spevans
Copy link
Contributor

spevans commented Jan 12, 2021

@swift-ci test

1 similar comment
@spevans
Copy link
Contributor

spevans commented Jan 13, 2021

@swift-ci test

Copy link
Contributor

@spevans spevans left a 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
@spevans
Copy link
Contributor

spevans commented Jan 14, 2021

@swift-ci test and merge

@swift-ci swift-ci merged commit 8f08574 into swiftlang:main Jan 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants