Skip to content

Commit e0e9531

Browse files
authored
Merge pull request #7398 from michalsn/fix/curl-response-headers
fix: CURLRequest - clear response headers between requests
2 parents 5b3ea6c + 26b504f commit e0e9531

5 files changed

Lines changed: 126 additions & 1 deletion

File tree

system/HTTP/CURLRequest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class CURLRequest extends OutgoingRequest
2828
*/
2929
protected $response;
3030

31+
/**
32+
* The original response object associated with this request
33+
*
34+
* @var ResponseInterface|null
35+
*/
36+
protected $responseOrig;
37+
3138
/**
3239
* The URI associated with this request
3340
*
@@ -105,7 +112,7 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
105112

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

108-
$this->response = $response;
115+
$this->responseOrig = $response ?? new Response(config('App'));
109116
$this->baseURI = $uri->useRawQueryString();
110117
$this->defaultOptions = $options;
111118

@@ -125,6 +132,8 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
125132
*/
126133
public function request($method, string $url, array $options = []): ResponseInterface
127134
{
135+
$this->response = clone $this->responseOrig;
136+
128137
$this->parseOptions($options);
129138

130139
$url = $this->prepareURL($url);

system/Test/Mock/MockCURLRequest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function setOutput($output)
3434

3535
protected function sendRequest(array $curlOptions = []): string
3636
{
37+
$this->response = clone $this->responseOrig;
38+
3739
// Save so we can access later.
3840
$this->curl_options = $curlOptions;
3941

tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,62 @@ public function testSendContinuedWithManyHeaders()
782782
$this->assertSame(200, $response->getStatusCode());
783783
}
784784

785+
/**
786+
* See: https://github.com/codeigniter4/CodeIgniter4/issues/7394
787+
*/
788+
public function testResponseHeadersWithMultipleRequests()
789+
{
790+
$request = $this->getRequest([
791+
'base_uri' => 'http://www.foo.com/api/v1/',
792+
]);
793+
794+
$output = "HTTP/2.0 200 OK
795+
Server: ddos-guard
796+
Expires: Thu, 19 Nov 1981 08:52:00 GMT
797+
Cache-Control: no-store, no-cache, must-revalidate
798+
Pragma: no-cache
799+
Content-Type: application/xml; charset=utf-8
800+
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello1</title>";
801+
$request->setOutput($output);
802+
803+
$response = $request->get('answer1');
804+
805+
$this->assertSame('<title>Hello1</title>', $response->getBody());
806+
807+
$responseHeaderKeys = [
808+
'Cache-Control',
809+
'Content-Type',
810+
'Server',
811+
'Expires',
812+
'Pragma',
813+
'Transfer-Encoding',
814+
];
815+
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
816+
817+
$this->assertSame(200, $response->getStatusCode());
818+
819+
$output = "HTTP/2.0 200 OK
820+
Expires: Thu, 19 Nov 1982 08:52:00 GMT
821+
Cache-Control: no-store, no-cache, must-revalidate
822+
Content-Type: application/xml; charset=utf-8
823+
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello2</title>";
824+
$request->setOutput($output);
825+
826+
$response = $request->get('answer2');
827+
828+
$this->assertSame('<title>Hello2</title>', $response->getBody());
829+
830+
$responseHeaderKeys = [
831+
'Cache-Control',
832+
'Content-Type',
833+
'Expires',
834+
'Transfer-Encoding',
835+
];
836+
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
837+
838+
$this->assertSame(200, $response->getStatusCode());
839+
}
840+
785841
public function testSplitResponse()
786842
{
787843
$request = $this->getRequest([

tests/system/HTTP/CURLRequestTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,62 @@ public function testSendContinuedWithManyHeaders()
765765
$this->assertSame(200, $response->getStatusCode());
766766
}
767767

768+
/**
769+
* See: https://github.com/codeigniter4/CodeIgniter4/issues/7394
770+
*/
771+
public function testResponseHeadersWithMultipleRequests()
772+
{
773+
$request = $this->getRequest([
774+
'base_uri' => 'http://www.foo.com/api/v1/',
775+
]);
776+
777+
$output = "HTTP/2.0 200 OK
778+
Server: ddos-guard
779+
Expires: Thu, 19 Nov 1981 08:52:00 GMT
780+
Cache-Control: no-store, no-cache, must-revalidate
781+
Pragma: no-cache
782+
Content-Type: application/xml; charset=utf-8
783+
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello1</title>";
784+
$request->setOutput($output);
785+
786+
$response = $request->get('answer1');
787+
788+
$this->assertSame('<title>Hello1</title>', $response->getBody());
789+
790+
$responseHeaderKeys = [
791+
'Cache-Control',
792+
'Content-Type',
793+
'Server',
794+
'Expires',
795+
'Pragma',
796+
'Transfer-Encoding',
797+
];
798+
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
799+
800+
$this->assertSame(200, $response->getStatusCode());
801+
802+
$output = "HTTP/2.0 200 OK
803+
Expires: Thu, 19 Nov 1982 08:52:00 GMT
804+
Cache-Control: no-store, no-cache, must-revalidate
805+
Content-Type: application/xml; charset=utf-8
806+
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello2</title>";
807+
$request->setOutput($output);
808+
809+
$response = $request->get('answer2');
810+
811+
$this->assertSame('<title>Hello2</title>', $response->getBody());
812+
813+
$responseHeaderKeys = [
814+
'Cache-Control',
815+
'Content-Type',
816+
'Expires',
817+
'Transfer-Encoding',
818+
];
819+
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
820+
821+
$this->assertSame(200, $response->getStatusCode());
822+
}
823+
768824
public function testSplitResponse()
769825
{
770826
$request = $this->getRequest([

user_guide_src/source/changelogs/v4.3.4.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Deprecations
2727
Bugs Fixed
2828
**********
2929

30+
- **CURLRequest:** Fixed a bug where the response class was shared between requests.
31+
3032
See the repo's
3133
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
3234
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)