Skip to content

Commit 460466c

Browse files
authored
Merge pull request #8760 from kenjis/fix-CURLRequest-getHeaderLine-Content-Type
fix: [CURLRequest] `getHeaderLine('Content-Type')` causes InvalidArgumentException
2 parents 59ec72d + 7a552d3 commit 460466c

File tree

4 files changed

+85
-80
lines changed

4 files changed

+85
-80
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12906,11 +12906,6 @@
1290612906
'count' => 1,
1290712907
'path' => __DIR__ . '/tests/system/HTTP/CLIRequestTest.php',
1290812908
];
12909-
$ignoreErrors[] = [
12910-
'message' => '#^Access to an undefined property CodeIgniter\\\\HTTP\\\\CURLRequest\\:\\:\\$curl_options\\.$#',
12911-
'count' => 39,
12912-
'path' => __DIR__ . '/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php',
12913-
];
1291412909
$ignoreErrors[] = [
1291512910
'message' => '#^Assigning \'10\' directly on offset \'HTTP_CONTENT_LENGTH\' of \\$_SERVER is discouraged\\.$#',
1291612911
'count' => 1,
@@ -12931,16 +12926,6 @@
1293112926
'count' => 1,
1293212927
'path' => __DIR__ . '/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php',
1293312928
];
12934-
$ignoreErrors[] = [
12935-
'message' => '#^Call to an undefined method CodeIgniter\\\\HTTP\\\\CURLRequest\\:\\:setOutput\\(\\)\\.$#',
12936-
'count' => 3,
12937-
'path' => __DIR__ . '/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php',
12938-
];
12939-
$ignoreErrors[] = [
12940-
'message' => '#^Method CodeIgniter\\\\HTTP\\\\CURLRequestDoNotShareOptionsTest\\:\\:getRequest\\(\\) has no return type specified\\.$#',
12941-
'count' => 1,
12942-
'path' => __DIR__ . '/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php',
12943-
];
1294412929
$ignoreErrors[] = [
1294512930
'message' => '#^Method CodeIgniter\\\\HTTP\\\\CURLRequestDoNotShareOptionsTest\\:\\:getRequest\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#',
1294612931
'count' => 1,

system/HTTP/CURLRequest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
116116

117117
parent::__construct(Method::GET, $uri);
118118

119-
$this->responseOrig = $response ?? new Response(config(App::class));
119+
$this->responseOrig = $response ?? new Response(config(App::class));
120+
// Remove the default Content-Type header.
121+
$this->responseOrig->removeHeader('Content-Type');
122+
120123
$this->baseURI = $uri->useRawQueryString();
121124
$this->defaultOptions = $options;
122125

tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
final class CURLRequestDoNotShareOptionsTest extends CIUnitTestCase
3131
{
32-
private CURLRequest $request;
32+
private MockCURLRequest $request;
3333

3434
protected function setUp(): void
3535
{
@@ -39,7 +39,7 @@ protected function setUp(): void
3939
$this->request = $this->getRequest();
4040
}
4141

42-
protected function getRequest(array $options = [])
42+
protected function getRequest(array $options = []): MockCURLRequest
4343
{
4444
$uri = isset($options['base_uri']) ? new URI($options['base_uri']) : new URI();
4545
$app = new App();
@@ -404,20 +404,20 @@ public function testAuthBasicOptionExplicit(): void
404404
public function testAuthDigestOption(): void
405405
{
406406
$output = "HTTP/1.1 401 Unauthorized
407-
Server: ddos-guard
408-
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
409-
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
410-
Server: ddos-guard
411-
Connection: keep-alive
412-
Keep-Alive: timeout=60
413-
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
414-
Date: Tue, 07 Jul 2020 15:13:14 GMT
415-
Expires: Thu, 19 Nov 1981 08:52:00 GMT
416-
Cache-Control: no-store, no-cache, must-revalidate
417-
Pragma: no-cache
418-
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
419-
Content-Type: application/xml; charset=utf-8
420-
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";
407+
Server: ddos-guard
408+
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
409+
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
410+
Server: ddos-guard
411+
Connection: keep-alive
412+
Keep-Alive: timeout=60
413+
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
414+
Date: Tue, 07 Jul 2020 15:13:14 GMT
415+
Expires: Thu, 19 Nov 1981 08:52:00 GMT
416+
Cache-Control: no-store, no-cache, must-revalidate
417+
Pragma: no-cache
418+
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
419+
Content-Type: application/xml; charset=utf-8
420+
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";
421421

422422
$this->request->setOutput($output);
423423

@@ -457,20 +457,20 @@ public function testSetAuthBasic(): void
457457
public function testSetAuthDigest(): void
458458
{
459459
$output = "HTTP/1.1 401 Unauthorized
460-
Server: ddos-guard
461-
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
462-
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
463-
Server: ddos-guard
464-
Connection: keep-alive
465-
Keep-Alive: timeout=60
466-
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
467-
Date: Tue, 07 Jul 2020 15:13:14 GMT
468-
Expires: Thu, 19 Nov 1981 08:52:00 GMT
469-
Cache-Control: no-store, no-cache, must-revalidate
470-
Pragma: no-cache
471-
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
472-
Content-Type: application/xml; charset=utf-8
473-
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";
460+
Server: ddos-guard
461+
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
462+
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
463+
Server: ddos-guard
464+
Connection: keep-alive
465+
Keep-Alive: timeout=60
466+
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
467+
Date: Tue, 07 Jul 2020 15:13:14 GMT
468+
Expires: Thu, 19 Nov 1981 08:52:00 GMT
469+
Cache-Control: no-store, no-cache, must-revalidate
470+
Pragma: no-cache
471+
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
472+
Content-Type: application/xml; charset=utf-8
473+
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";
474474

475475
$this->request->setOutput($output);
476476

@@ -784,14 +784,14 @@ public function testSendContinuedWithManyHeaders(): void
784784

785785
$responseHeaderKeys = [
786786
'Cache-Control',
787-
'Content-Type',
788787
'Server',
789788
'Connection',
790789
'Keep-Alive',
791790
'Set-Cookie',
792791
'Date',
793792
'Expires',
794793
'Pragma',
794+
'Content-Type',
795795
'Transfer-Encoding',
796796
];
797797
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
@@ -823,10 +823,10 @@ public function testResponseHeadersWithMultipleRequests(): void
823823

824824
$responseHeaderKeys = [
825825
'Cache-Control',
826-
'Content-Type',
827826
'Server',
828827
'Expires',
829828
'Pragma',
829+
'Content-Type',
830830
'Transfer-Encoding',
831831
];
832832
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
@@ -846,8 +846,8 @@ public function testResponseHeadersWithMultipleRequests(): void
846846

847847
$responseHeaderKeys = [
848848
'Cache-Control',
849-
'Content-Type',
850849
'Expires',
850+
'Content-Type',
851851
'Transfer-Encoding',
852852
];
853853
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
@@ -1141,4 +1141,21 @@ public function testUserAgentOption(): void
11411141
$this->assertArrayHasKey(CURLOPT_USERAGENT, $options);
11421142
$this->assertSame($agent, $options[CURLOPT_USERAGENT]);
11431143
}
1144+
1145+
public function testGetHeaderLineContentType(): void
1146+
{
1147+
$output = 'HTTP/2 200
1148+
date: Thu, 11 Apr 2024 07:26:00 GMT
1149+
content-type: text/html; charset=UTF-8
1150+
cache-control: no-store, max-age=0, no-cache
1151+
server: cloudflare
1152+
content-encoding: br
1153+
alt-svc: h3=":443"; ma=86400' . "\x0d\x0a\x0d\x0aResponse Body";
1154+
1155+
$this->request->setOutput($output);
1156+
1157+
$response = $this->request->request('get', 'http://example.com');
1158+
1159+
$this->assertSame('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
1160+
}
11441161
}

tests/system/HTTP/CURLRequestTest.php

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -387,20 +387,20 @@ public function testAuthBasicOptionExplicit(): void
387387
public function testAuthDigestOption(): void
388388
{
389389
$output = "HTTP/1.1 401 Unauthorized
390-
Server: ddos-guard
391-
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
392-
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
393-
Server: ddos-guard
394-
Connection: keep-alive
395-
Keep-Alive: timeout=60
396-
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
397-
Date: Tue, 07 Jul 2020 15:13:14 GMT
398-
Expires: Thu, 19 Nov 1981 08:52:00 GMT
399-
Cache-Control: no-store, no-cache, must-revalidate
400-
Pragma: no-cache
401-
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
402-
Content-Type: application/xml; charset=utf-8
403-
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";
390+
Server: ddos-guard
391+
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
392+
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
393+
Server: ddos-guard
394+
Connection: keep-alive
395+
Keep-Alive: timeout=60
396+
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
397+
Date: Tue, 07 Jul 2020 15:13:14 GMT
398+
Expires: Thu, 19 Nov 1981 08:52:00 GMT
399+
Cache-Control: no-store, no-cache, must-revalidate
400+
Pragma: no-cache
401+
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
402+
Content-Type: application/xml; charset=utf-8
403+
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";
404404

405405
$this->request->setOutput($output);
406406

@@ -440,20 +440,20 @@ public function testSetAuthBasic(): void
440440
public function testSetAuthDigest(): void
441441
{
442442
$output = "HTTP/1.1 401 Unauthorized
443-
Server: ddos-guard
444-
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
445-
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
446-
Server: ddos-guard
447-
Connection: keep-alive
448-
Keep-Alive: timeout=60
449-
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
450-
Date: Tue, 07 Jul 2020 15:13:14 GMT
451-
Expires: Thu, 19 Nov 1981 08:52:00 GMT
452-
Cache-Control: no-store, no-cache, must-revalidate
453-
Pragma: no-cache
454-
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
455-
Content-Type: application/xml; charset=utf-8
456-
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";
443+
Server: ddos-guard
444+
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
445+
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
446+
Server: ddos-guard
447+
Connection: keep-alive
448+
Keep-Alive: timeout=60
449+
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
450+
Date: Tue, 07 Jul 2020 15:13:14 GMT
451+
Expires: Thu, 19 Nov 1981 08:52:00 GMT
452+
Cache-Control: no-store, no-cache, must-revalidate
453+
Pragma: no-cache
454+
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
455+
Content-Type: application/xml; charset=utf-8
456+
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";
457457

458458
$this->request->setOutput($output);
459459

@@ -782,14 +782,14 @@ public function testSendContinuedWithManyHeaders(): void
782782

783783
$responseHeaderKeys = [
784784
'Cache-Control',
785-
'Content-Type',
786785
'Server',
787786
'Connection',
788787
'Keep-Alive',
789788
'Set-Cookie',
790789
'Date',
791790
'Expires',
792791
'Pragma',
792+
'Content-Type',
793793
'Transfer-Encoding',
794794
];
795795
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
@@ -836,10 +836,10 @@ public function testResponseHeadersWithMultipleRequests(): void
836836

837837
$responseHeaderKeys = [
838838
'Cache-Control',
839-
'Content-Type',
840839
'Server',
841840
'Expires',
842841
'Pragma',
842+
'Content-Type',
843843
'Transfer-Encoding',
844844
];
845845
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
@@ -859,8 +859,8 @@ public function testResponseHeadersWithMultipleRequests(): void
859859

860860
$responseHeaderKeys = [
861861
'Cache-Control',
862-
'Content-Type',
863862
'Expires',
863+
'Content-Type',
864864
'Transfer-Encoding',
865865
];
866866
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));

0 commit comments

Comments
 (0)