Correct the post/redirect/get logic when using MultiCurl::addPost()

This commit is contained in:
Zach Borboa
2021-11-20 22:17:32 -05:00
parent 0c8eab3083
commit 3d9acedb66
4 changed files with 25 additions and 17 deletions
+9 -4
View File
@@ -718,12 +718,17 @@ class Curl
$this->setUrl($url);
// Set the request method to "POST" when following a 303 redirect with
// an additional POST request is desired. This is equivalent to setting
// the -X, --request command line option where curl won't change the
// request method according to the HTTP 30x response code.
if ($follow_303_with_post) {
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'POST');
} else {
if (isset($this->options[CURLOPT_CUSTOMREQUEST])) {
$this->setOpt(CURLOPT_CUSTOMREQUEST, null);
}
} elseif (isset($this->options[CURLOPT_CUSTOMREQUEST])) {
// Unset the CURLOPT_CUSTOMREQUEST option so that curl does not use
// a POST request after a post/redirect/get redirection. Without
// this, curl will use the method string specified for all requests.
$this->setOpt(CURLOPT_CUSTOMREQUEST, null);
}
$this->setOpt(CURLOPT_POST, true);
+6 -6
View File
@@ -261,7 +261,7 @@ class MultiCurl
* @param $url
* @param $data
* @param $follow_303_with_post
* If true, will cause 303 redirections to be followed using GET requests (default: false).
* If true, will cause 303 redirections to be followed using a POST request (default: false).
* Note: Redirections are only followed if the CURLOPT_FOLLOWLOCATION option is set to true.
*
* @return object
@@ -284,11 +284,11 @@ class MultiCurl
$curl->setUrl($url);
/*
* For post-redirect-get requests, the CURLOPT_CUSTOMREQUEST option must not
* be set, otherwise cURL will perform POST requests for redirections.
*/
if (!$follow_303_with_post) {
// Set the request method to "POST" when following a 303 redirect with
// an additional POST request is desired. This is equivalent to setting
// the -X, --request command line option where curl won't change the
// request method according to the HTTP 30x response code.
if ($follow_303_with_post) {
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'POST');
}
+2 -1
View File
@@ -53,7 +53,8 @@ class Test
private function chainedRequest($request_method, $data)
{
if ($request_method === 'POST') {
$this->server('request_method', $request_method, $data, true);
$follow_303_with_post = true;
$this->server('request_method', $request_method, $data, $follow_303_with_post);
} else {
$this->server('request_method', $request_method, $data);
}
+8 -6
View File
@@ -2993,20 +2993,20 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
public function testMultiPostRedirectGet()
{
// Deny post-redirect-get
// Deny the post-redirect-get and make a POST following the redirection.
$multi_curl = new MultiCurl(Test::TEST_URL);
$multi_curl->setOpt(CURLOPT_FOLLOWLOCATION, true);
$multi_curl->setFollowLocation(true);
$multi_curl->setHeader('X-DEBUG-TEST', 'post_redirect_get');
$multi_curl->addPost([], false)->complete(function ($instance) {
$multi_curl->addPost([], true)->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertEquals('Redirected: POST', $instance->response);
});
$multi_curl->start();
// Allow post-redirect-get
// Allow the post-redirect-get and make a GET following the redirection.
$multi_curl = new MultiCurl(Test::TEST_URL);
$multi_curl->setOpt(CURLOPT_FOLLOWLOCATION, true);
$multi_curl->setFollowLocation(true);
$multi_curl->setHeader('X-DEBUG-TEST', 'post_redirect_get');
$multi_curl->addPost([], true)->complete(function ($instance) {
$multi_curl->addPost([], false)->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertEquals('Redirected: GET', $instance->response);
});
$multi_curl->start();
@@ -4697,6 +4697,8 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$this->assertNull($multi_curl->getOpt(CURLOPT_FOLLOWLOCATION));
$multi_curl->setFollowLocation(true);
$this->assertTrue($multi_curl->getOpt(CURLOPT_FOLLOWLOCATION));
$multi_curl->setFollowLocation(false);
$this->assertFalse($multi_curl->getOpt(CURLOPT_FOLLOWLOCATION));
}
public function testSetForbidReuse()