Skip to content

Commit

Permalink
Merge pull request #8760 from kenjis/fix-CURLRequest-getHeaderLine-Co…
Browse files Browse the repository at this point in the history
…ntent-Type

fix: [CURLRequest] `getHeaderLine('Content-Type')` causes InvalidArgumentException
  • Loading branch information
kenjis authored Apr 11, 2024
2 parents 59ec72d + 7a552d3 commit 460466c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 80 deletions.
15 changes: 0 additions & 15 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -12906,11 +12906,6 @@
'count' => 1,
'path' => __DIR__ . '/tests/system/HTTP/CLIRequestTest.php',
];
$ignoreErrors[] = [
'message' => '#^Access to an undefined property CodeIgniter\\\\HTTP\\\\CURLRequest\\:\\:\\$curl_options\\.$#',
'count' => 39,
'path' => __DIR__ . '/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php',
];
$ignoreErrors[] = [
'message' => '#^Assigning \'10\' directly on offset \'HTTP_CONTENT_LENGTH\' of \\$_SERVER is discouraged\\.$#',
'count' => 1,
Expand All @@ -12931,16 +12926,6 @@
'count' => 1,
'path' => __DIR__ . '/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php',
];
$ignoreErrors[] = [
'message' => '#^Call to an undefined method CodeIgniter\\\\HTTP\\\\CURLRequest\\:\\:setOutput\\(\\)\\.$#',
'count' => 3,
'path' => __DIR__ . '/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php',
];
$ignoreErrors[] = [
'message' => '#^Method CodeIgniter\\\\HTTP\\\\CURLRequestDoNotShareOptionsTest\\:\\:getRequest\\(\\) has no return type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php',
];
$ignoreErrors[] = [
'message' => '#^Method CodeIgniter\\\\HTTP\\\\CURLRequestDoNotShareOptionsTest\\:\\:getRequest\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#',
'count' => 1,
Expand Down
5 changes: 4 additions & 1 deletion system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response

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

$this->responseOrig = $response ?? new Response(config(App::class));
$this->responseOrig = $response ?? new Response(config(App::class));
// Remove the default Content-Type header.
$this->responseOrig->removeHeader('Content-Type');

$this->baseURI = $uri->useRawQueryString();
$this->defaultOptions = $options;

Expand Down
83 changes: 50 additions & 33 deletions tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
final class CURLRequestDoNotShareOptionsTest extends CIUnitTestCase
{
private CURLRequest $request;
private MockCURLRequest $request;

protected function setUp(): void
{
Expand All @@ -39,7 +39,7 @@ protected function setUp(): void
$this->request = $this->getRequest();
}

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

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

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

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

Expand Down Expand Up @@ -784,14 +784,14 @@ public function testSendContinuedWithManyHeaders(): void

$responseHeaderKeys = [
'Cache-Control',
'Content-Type',
'Server',
'Connection',
'Keep-Alive',
'Set-Cookie',
'Date',
'Expires',
'Pragma',
'Content-Type',
'Transfer-Encoding',
];
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
Expand Down Expand Up @@ -823,10 +823,10 @@ public function testResponseHeadersWithMultipleRequests(): void

$responseHeaderKeys = [
'Cache-Control',
'Content-Type',
'Server',
'Expires',
'Pragma',
'Content-Type',
'Transfer-Encoding',
];
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
Expand All @@ -846,8 +846,8 @@ public function testResponseHeadersWithMultipleRequests(): void

$responseHeaderKeys = [
'Cache-Control',
'Content-Type',
'Expires',
'Content-Type',
'Transfer-Encoding',
];
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
Expand Down Expand Up @@ -1141,4 +1141,21 @@ public function testUserAgentOption(): void
$this->assertArrayHasKey(CURLOPT_USERAGENT, $options);
$this->assertSame($agent, $options[CURLOPT_USERAGENT]);
}

public function testGetHeaderLineContentType(): void
{
$output = 'HTTP/2 200
date: Thu, 11 Apr 2024 07:26:00 GMT
content-type: text/html; charset=UTF-8
cache-control: no-store, max-age=0, no-cache
server: cloudflare
content-encoding: br
alt-svc: h3=":443"; ma=86400' . "\x0d\x0a\x0d\x0aResponse Body";

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

$response = $this->request->request('get', 'http://example.com');

$this->assertSame('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
}
}
62 changes: 31 additions & 31 deletions tests/system/HTTP/CURLRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,20 +387,20 @@ public function testAuthBasicOptionExplicit(): void
public function testAuthDigestOption(): void
{
$output = "HTTP/1.1 401 Unauthorized
Server: ddos-guard
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
Server: ddos-guard
Connection: keep-alive
Keep-Alive: timeout=60
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
Date: Tue, 07 Jul 2020 15:13:14 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
Content-Type: application/xml; charset=utf-8
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";
Server: ddos-guard
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
WWW-Authenticate: Digest\x0d\x0a\x0d\x0aHTTP/1.1 200 OK
Server: ddos-guard
Connection: keep-alive
Keep-Alive: timeout=60
Set-Cookie: __ddg1=z177j4mLtqzC07v0zviU; Domain=.site.ru; HttpOnly; Path=/; Expires=Wed, 07-Jul-2021 15:13:14 GMT
Date: Tue, 07 Jul 2020 15:13:14 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: PHPSESSID=80pd3hlg38mvjnelpvokp9lad0; path=/
Content-Type: application/xml; charset=utf-8
Transfer-Encoding: chunked\x0d\x0a\x0d\x0a<title>Update success! config</title>";

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

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

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

Expand Down Expand Up @@ -782,14 +782,14 @@ public function testSendContinuedWithManyHeaders(): void

$responseHeaderKeys = [
'Cache-Control',
'Content-Type',
'Server',
'Connection',
'Keep-Alive',
'Set-Cookie',
'Date',
'Expires',
'Pragma',
'Content-Type',
'Transfer-Encoding',
];
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
Expand Down Expand Up @@ -836,10 +836,10 @@ public function testResponseHeadersWithMultipleRequests(): void

$responseHeaderKeys = [
'Cache-Control',
'Content-Type',
'Server',
'Expires',
'Pragma',
'Content-Type',
'Transfer-Encoding',
];
$this->assertSame($responseHeaderKeys, array_keys($response->headers()));
Expand All @@ -859,8 +859,8 @@ public function testResponseHeadersWithMultipleRequests(): void

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

0 comments on commit 460466c

Please sign in to comment.