Skip to content

Commit 2a903cf

Browse files
committed
clear response headers between requests in CURLRequest when $shareOptions is disabled
1 parent ec585bd commit 2a903cf

4 files changed

Lines changed: 124 additions & 2 deletions

File tree

system/HTTP/CURLRequest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@ protected function applyBody(array $curlOptions = []): array
469469
*/
470470
protected function setResponseHeaders(array $headers = [])
471471
{
472+
if ($this->shareOptions === false) {
473+
foreach ($this->response->headers() as $header) {
474+
$this->response->removeHeader($header->getName());
475+
}
476+
}
477+
472478
foreach ($headers as $header) {
473479
if (($pos = strpos($header, ':')) !== false) {
474480
$title = substr($header, 0, $pos);

tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,15 +766,71 @@ public function testSendContinuedWithManyHeaders()
766766
$this->assertSame('<title>Update success! config</title>', $response->getBody());
767767

768768
$responseHeaderKeys = [
769-
'Cache-Control',
770-
'Content-Type',
771769
'Server',
772770
'Connection',
773771
'Keep-Alive',
774772
'Set-Cookie',
775773
'Date',
776774
'Expires',
775+
'Cache-Control',
777776
'Pragma',
777+
'Content-Type',
778+
'Transfer-Encoding',
779+
];
780+
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
781+
782+
$this->assertSame(200, $response->getStatusCode());
783+
}
784+
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+
'Server',
809+
'Expires',
810+
'Cache-Control',
811+
'Pragma',
812+
'Content-Type',
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+
Server: ddos-guard
821+
Expires: Thu, 19 Nov 1982 08:52:00 GMT
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+
'Server',
832+
'Expires',
833+
'Content-Type',
778834
'Transfer-Encoding',
779835
];
780836
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));

tests/system/HTTP/CURLRequestTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,64 @@ 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>Hello</title>";
784+
$request->setOutput($output);
785+
786+
$response = $request->get('answer');
787+
788+
$this->assertSame('<title>Hello</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+
Server: ddos-guard
804+
Expires: Thu, 19 Nov 1982 08:52:00 GMT
805+
Content-Type: application/xml; charset=utf-8
806+
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Hello</title>";
807+
$request->setOutput($output);
808+
809+
$response = $request->get('answer');
810+
811+
$this->assertSame('<title>Hello</title>', $response->getBody());
812+
813+
$responseHeaderKeys = [
814+
'Cache-Control',
815+
'Content-Type',
816+
'Server',
817+
'Expires',
818+
'Pragma',
819+
'Transfer-Encoding',
820+
];
821+
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
822+
823+
$this->assertSame(200, $response->getStatusCode());
824+
}
825+
768826
public function testSplitResponse()
769827
{
770828
$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:** Clear response headers between requests when ``Config\CURLRequest::$shareOptions`` is disabled.
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)