Skip to content

Commit 12dd752

Browse files
committed
Optimize the usage of option 'baseURI' for CURLRequest
1 parent fcf37d6 commit 12dd752

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

system/Config/Services.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public static function csp(?CSPConfig $config = null, bool $getShared = true)
197197
/**
198198
* The CURL Request class acts as a simple HTTP client for interacting
199199
* with other servers, typically through APIs.
200+
* The option 'base_uri' is deprecated and will be remove soon.
200201
*
201202
* @return CURLRequest
202203
*/
@@ -208,10 +209,12 @@ public static function curlrequest(array $options = [], ?ResponseInterface $resp
208209

209210
$config ??= config(App::class);
210211
$response ??= new Response($config);
212+
$uri = new URI($options['baseURI'] ?? ($options['base_uri'] ?? null));
213+
unset($options['baseURI']);
211214

212215
return new CURLRequest(
213216
$config,
214-
new URI($options['base_uri'] ?? null),
217+
$uri,
215218
$response,
216219
$options
217220
);

system/HTTP/CURLRequest.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ class CURLRequest extends OutgoingRequest
3939
*/
4040
protected $responseOrig;
4141

42-
/**
43-
* The URI associated with this request
44-
*
45-
* @var URI
46-
*/
47-
protected $baseURI;
48-
4942
/**
5043
* The setting values
5144
*
@@ -104,7 +97,6 @@ class CURLRequest extends OutgoingRequest
10497
/**
10598
* Takes an array of options to set the following possible class properties:
10699
*
107-
* - baseURI
108100
* - timeout
109101
* - any other request options to use as defaults.
110102
*
@@ -116,13 +108,13 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
116108
throw HTTPException::forMissingCurl(); // @codeCoverageIgnore
117109
}
118110

111+
$uri->useRawQueryString();
119112
parent::__construct(Method::GET, $uri);
120113

121114
$this->responseOrig = $response ?? new Response($config);
122115
// Remove the default Content-Type header.
123116
$this->responseOrig->removeHeader('Content-Type');
124117

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

128120
/** @var ConfigCURLRequest|null $configCURLRequest */
@@ -135,17 +127,24 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
135127

136128
/**
137129
* Sends an HTTP request to the specified $url. If this is a relative
138-
* URL, it will be merged with $this->baseURI to form a complete URL.
130+
* URL, it will be merged with $options['baseURI'] or $this->uri to form a complete URL.
139131
*
140132
* @param string $method HTTP method
141133
*/
142134
public function request($method, string $url, array $options = []): ResponseInterface
143135
{
144136
$this->response = clone $this->responseOrig;
145137

138+
if (array_key_exists('baseURI', $options)) {
139+
$uri = new URI($options['baseURI']);
140+
$uri->useRawQueryString();
141+
unset($options['baseURI']);
142+
}else{
143+
$uri = $this->uri;
144+
}
146145
$this->parseOptions($options);
147146

148-
$url = $this->prepareURL($url);
147+
$url = $this->prepareURL($url, $uri);
149148

150149
$method = esc(strip_tags($method));
151150

@@ -293,11 +292,6 @@ public function setJSON($data)
293292
*/
294293
protected function parseOptions(array $options)
295294
{
296-
if (array_key_exists('baseURI', $options)) {
297-
$this->baseURI = $this->baseURI->setURI($options['baseURI']);
298-
unset($options['baseURI']);
299-
}
300-
301295
if (array_key_exists('headers', $options) && is_array($options['headers'])) {
302296
foreach ($options['headers'] as $name => $value) {
303297
$this->setHeader($name, $value);
@@ -325,16 +319,16 @@ protected function parseOptions(array $options)
325319

326320
/**
327321
* If the $url is a relative URL, will attempt to create
328-
* a full URL by prepending $this->baseURI to it.
322+
* a full URL by prepending $uri to it.
329323
*/
330-
protected function prepareURL(string $url): string
324+
protected function prepareURL(string $url, URI $uri): string
331325
{
332326
// If it's a full URI, then we have nothing to do here...
333327
if (str_contains($url, '://')) {
334328
return $url;
335329
}
336330

337-
$uri = $this->baseURI->resolveRelativeURI($url);
331+
$uri = $uri->resolveRelativeURI($url);
338332

339333
// Create the string instead of casting to prevent baseURL muddling
340334
return URI::createURIString(

0 commit comments

Comments
 (0)