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: CURLRequest - clear response headers between requests #7398

Merged
merged 5 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class CURLRequest extends OutgoingRequest
*/
protected $response;

/**
* The original response object associated with this request
*
* @var ResponseInterface|null
*/
protected $responseOrig;

/**
* The URI associated with this request
*
Expand Down Expand Up @@ -105,7 +112,7 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response

parent::__construct('GET', $uri);

$this->response = $response;
$this->responseOrig = $response ?? new Response(config('App'));
$this->baseURI = $uri->useRawQueryString();
$this->defaultOptions = $options;

Expand All @@ -125,6 +132,8 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
*/
public function request($method, string $url, array $options = []): ResponseInterface
{
$this->response = clone $this->responseOrig;

$this->parseOptions($options);

$url = $this->prepareURL($url);
Expand Down
2 changes: 2 additions & 0 deletions system/Test/Mock/MockCURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function setOutput($output)

protected function sendRequest(array $curlOptions = []): string
{
$this->response = clone $this->responseOrig;

// Save so we can access later.
$this->curl_options = $curlOptions;

Expand Down
56 changes: 56 additions & 0 deletions tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,62 @@ public function testSendContinuedWithManyHeaders()
$this->assertSame(200, $response->getStatusCode());
}

/**
* See: https://github.com/codeigniter4/CodeIgniter4/issues/7394
*/
public function testResponseHeadersWithMultipleRequests()
{
$request = $this->getRequest([
'base_uri' => 'http://www.foo.com/api/v1/',
]);

$output = "HTTP/2.0 200 OK
Server: ddos-guard
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Type: application/xml; charset=utf-8
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello1</title>";
$request->setOutput($output);

$response = $request->get('answer1');

$this->assertSame('<title>Hello1</title>', $response->getBody());

$responseHeaderKeys = [
'Cache-Control',
'Content-Type',
'Server',
'Expires',
'Pragma',
'Transfer-Encoding',
];
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));

$this->assertSame(200, $response->getStatusCode());

$output = "HTTP/2.0 200 OK
Expires: Thu, 19 Nov 1982 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Content-Type: application/xml; charset=utf-8
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello2</title>";
$request->setOutput($output);

$response = $request->get('answer2');

$this->assertSame('<title>Hello2</title>', $response->getBody());

$responseHeaderKeys = [
'Cache-Control',
'Content-Type',
'Expires',
'Transfer-Encoding',
];
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));

$this->assertSame(200, $response->getStatusCode());
}

public function testSplitResponse()
{
$request = $this->getRequest([
Expand Down
56 changes: 56 additions & 0 deletions tests/system/HTTP/CURLRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,62 @@ public function testSendContinuedWithManyHeaders()
$this->assertSame(200, $response->getStatusCode());
}

/**
* See: https://github.com/codeigniter4/CodeIgniter4/issues/7394
*/
public function testResponseHeadersWithMultipleRequests()
{
$request = $this->getRequest([
'base_uri' => 'http://www.foo.com/api/v1/',
]);

$output = "HTTP/2.0 200 OK
Server: ddos-guard
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Type: application/xml; charset=utf-8
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello1</title>";
$request->setOutput($output);

$response = $request->get('answer1');

$this->assertSame('<title>Hello1</title>', $response->getBody());

$responseHeaderKeys = [
'Cache-Control',
'Content-Type',
'Server',
'Expires',
'Pragma',
'Transfer-Encoding',
];
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));

$this->assertSame(200, $response->getStatusCode());

$output = "HTTP/2.0 200 OK
Expires: Thu, 19 Nov 1982 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Content-Type: application/xml; charset=utf-8
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello2</title>";
$request->setOutput($output);

$response = $request->get('answer2');

$this->assertSame('<title>Hello2</title>', $response->getBody());

$responseHeaderKeys = [
'Cache-Control',
'Content-Type',
'Expires',
'Transfer-Encoding',
];
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));

$this->assertSame(200, $response->getStatusCode());
}

public function testSplitResponse()
{
$request = $this->getRequest([
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.3.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Deprecations
Bugs Fixed
**********

- **CURLRequest:** Fixed a bug where the response class was shared between requests.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.