Skip to content

Commit dbbdd2d

Browse files
authored
Merge pull request #5249 from kenjis/remove-curl-headers-sharing
Fix: remove CURLRequest headers sharing from $_SERVER
2 parents 4034dc4 + 88963ba commit dbbdd2d

File tree

8 files changed

+1183
-47
lines changed

8 files changed

+1183
-47
lines changed

app/Config/CURLRequest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
7+
class CURLRequest extends BaseConfig
8+
{
9+
/**
10+
* --------------------------------------------------------------------------
11+
* CURLRequest Share Options
12+
* --------------------------------------------------------------------------
13+
*
14+
* Whether share options between requests or not.
15+
*
16+
* If true, all the options won't be reset between requests.
17+
* It may cause an error request with unnecessary headers.
18+
*
19+
* @var bool
20+
*/
21+
public $shareOptions = true;
22+
}

env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,9 @@
124124
#--------------------------------------------------------------------
125125

126126
# logger.threshold = 4
127+
128+
#--------------------------------------------------------------------
129+
# CURLRequest
130+
#--------------------------------------------------------------------
131+
132+
# curlrequest.shareOptions = true

system/HTTP/CURLRequest.php

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use CodeIgniter\HTTP\Exceptions\HTTPException;
1515
use Config\App;
16+
use Config\CURLRequest as ConfigCURLRequest;
1617
use InvalidArgumentException;
1718

1819
/**
@@ -42,7 +43,14 @@ class CURLRequest extends Request
4243
*
4344
* @var array
4445
*/
45-
protected $config = [
46+
protected $config;
47+
48+
/**
49+
* The default setting values
50+
*
51+
* @var array
52+
*/
53+
protected $defaultConfig = [
4654
'timeout' => 0.0,
4755
'connect_timeout' => 150,
4856
'debug' => false,
@@ -72,6 +80,23 @@ class CURLRequest extends Request
7280
*/
7381
protected $delay = 0.0;
7482

83+
/**
84+
* The default options from the constructor. Applied to all requests.
85+
*
86+
* @var array
87+
*/
88+
private $defaultOptions;
89+
90+
/**
91+
* Whether share options between requests or not.
92+
*
93+
* If true, all the options won't be reset between requests.
94+
* It may cause an error request with unnecessary headers.
95+
*
96+
* @var bool
97+
*/
98+
private $shareOptions;
99+
75100
/**
76101
* Takes an array of options to set the following possible class properties:
77102
*
@@ -92,9 +117,15 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
92117

93118
parent::__construct($config);
94119

95-
$this->response = $response;
96-
$this->baseURI = $uri->useRawQueryString();
120+
$this->response = $response;
121+
$this->baseURI = $uri->useRawQueryString();
122+
$this->defaultOptions = $options;
123+
124+
/** @var ConfigCURLRequest|null $configCURLRequest */
125+
$configCURLRequest = config('CURLRequest');
126+
$this->shareOptions = $configCURLRequest->shareOptions ?? true;
97127

128+
$this->config = $this->defaultConfig;
98129
$this->parseOptions($options);
99130
}
100131

@@ -114,9 +145,29 @@ public function request($method, string $url, array $options = []): ResponseInte
114145

115146
$this->send($method, $url);
116147

148+
if ($this->shareOptions === false) {
149+
$this->resetOptions();
150+
}
151+
117152
return $this->response;
118153
}
119154

155+
/**
156+
* Reset all options to default.
157+
*/
158+
protected function resetOptions()
159+
{
160+
// Reset headers
161+
$this->headers = [];
162+
$this->headerMap = [];
163+
164+
// Reset configs
165+
$this->config = $this->defaultConfig;
166+
167+
// Set the default options for next request
168+
$this->parseOptions($this->defaultOptions);
169+
}
170+
120171
/**
121172
* Convenience method for sending a GET request.
122173
*/
@@ -350,27 +401,17 @@ public function send(string $method, string $url)
350401
}
351402

352403
/**
353-
* Takes all headers current part of this request and adds them
354-
* to the cURL request.
404+
* Adds $this->headers to the cURL request.
355405
*/
356406
protected function applyRequestHeaders(array $curlOptions = []): array
357407
{
358408
if (empty($this->headers)) {
359-
$this->populateHeaders();
360-
// Otherwise, it will corrupt the request
361-
$this->removeHeader('Host');
362-
$this->removeHeader('Accept-Encoding');
363-
}
364-
365-
$headers = $this->headers();
366-
367-
if (empty($headers)) {
368409
return $curlOptions;
369410
}
370411

371412
$set = [];
372413

373-
foreach (array_keys($headers) as $name) {
414+
foreach (array_keys($this->headers) as $name) {
374415
$set[] = $name . ': ' . $this->getHeaderLine($name);
375416
}
376417

0 commit comments

Comments
 (0)