Skip to content

Commit aed89d9

Browse files
committed
fix: add config $CURLRequestShareOptions for the CURLRequest
If $CURLRequestShareOptions is false, reset all config after sending a request. If true, keep the all config (the same as before).
1 parent 182c252 commit aed89d9

3 files changed

Lines changed: 48 additions & 10 deletions

File tree

app/Config/App.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,15 @@ class App extends BaseConfig
461461
* @var bool
462462
*/
463463
public $CSPEnabled = false;
464+
465+
/**
466+
* --------------------------------------------------------------------------
467+
* CURLRequest Share Options
468+
* --------------------------------------------------------------------------
469+
*
470+
* Whether share options between requests or not.
471+
*
472+
* @var bool
473+
*/
474+
public $CURLRequestShareOptions = true;
464475
}

system/HTTP/CURLRequest.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ class CURLRequest extends Request
4242
*
4343
* @var array
4444
*/
45-
protected $config = [
45+
protected $config;
46+
47+
/**
48+
* The default setting values
49+
*
50+
* @var array
51+
*/
52+
private $defaultConfig = [
4653
'timeout' => 0.0,
4754
'connect_timeout' => 150,
4855
'debug' => false,
@@ -79,6 +86,13 @@ class CURLRequest extends Request
7986
*/
8087
private $defaultOptions;
8188

89+
/**
90+
* Whether share options between requests or not.
91+
*
92+
* @var bool
93+
*/
94+
private $shareOptions;
95+
8296
/**
8397
* Takes an array of options to set the following possible class properties:
8498
*
@@ -102,6 +116,8 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
102116
$this->response = $response;
103117
$this->baseURI = $uri->useRawQueryString();
104118
$this->defaultOptions = $options;
119+
$this->config = $this->defaultConfig;
120+
$this->shareOptions = $config->CURLRequestShareOptions ?? true;
105121

106122
$this->parseOptions($options);
107123
}
@@ -122,15 +138,24 @@ public function request($method, string $url, array $options = []): ResponseInte
122138

123139
$this->send($method, $url);
124140

141+
if ($this->shareOptions === false) {
142+
$this->resetOptions();
143+
}
144+
145+
return $this->response;
146+
}
147+
148+
private function resetOptions()
149+
{
125150
// Reset headers
126151
$this->headers = [];
127152
$this->headerMap = [];
128-
// Reset unshared configs
129-
unset($this->config['multipart'], $this->config['form_params']);
153+
154+
// Reset configs
155+
$this->config = $this->defaultConfig;
156+
130157
// Set the default options for next request
131158
$this->parseOptions($this->defaultOptions);
132-
133-
return $this->response;
134159
}
135160

136161
/**

tests/system/HTTP/CURLRequestTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ protected function setUp(): void
3636
$this->request = $this->getRequest();
3737
}
3838

39-
protected function getRequest(array $options = [])
39+
protected function getRequest(array $options = [], $shareOptions = true)
4040
{
41-
$uri = isset($options['base_uri']) ? new URI($options['base_uri']) : new URI();
41+
$uri = isset($options['base_uri']) ? new URI($options['base_uri']) : new URI();
42+
$app = new App();
43+
$app->CURLRequestShareOptions = $shareOptions;
4244

43-
return new MockCURLRequest(($app = new App()), $uri, new Response($app), $options);
45+
return new MockCURLRequest(($app), $uri, new Response($app), $options);
4446
}
4547

4648
/**
@@ -197,12 +199,12 @@ public function testOptionsHeadersNotUsingPopulate()
197199
$this->assertSame('', $request->header('Accept-Encoding')->getValue());
198200
}
199201

200-
public function testHeaderContentLengthNotSharedBetweenRequests()
202+
public function testHeaderContentLengthNotSharedBetweenRequestsWhenSharedOptionsFalse()
201203
{
202204
$options = [
203205
'base_uri' => 'http://www.foo.com/api/v1/',
204206
];
205-
$request = $this->getRequest($options);
207+
$request = $this->getRequest($options, false);
206208

207209
$request->post('example', [
208210
'form_params' => [

0 commit comments

Comments
 (0)