Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Optimize the usage of option 'baseURI' for CURLRequest #9274

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public static function csp(?CSPConfig $config = null, bool $getShared = true)
/**
* The CURL Request class acts as a simple HTTP client for interacting
* with other servers, typically through APIs.
* The option 'base_uri' is deprecated and will be remove soon.
*
* @return CURLRequest
*/
Expand All @@ -208,10 +209,12 @@ public static function curlrequest(array $options = [], ?ResponseInterface $resp

$config ??= config(App::class);
$response ??= new Response($config);
$uri = new URI($options['baseURI'] ?? $options['base_uri'] ?? null);
unset($options['baseURI']);

return new CURLRequest(
$config,
new URI($options['base_uri'] ?? null),
$uri,
$response,
$options
);
Expand Down
32 changes: 13 additions & 19 deletions system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ class CURLRequest extends OutgoingRequest
*/
protected $responseOrig;

/**
* The URI associated with this request
*
* @var URI
*/
protected $baseURI;

/**
* The setting values
*
Expand Down Expand Up @@ -104,7 +97,6 @@ class CURLRequest extends OutgoingRequest
/**
* Takes an array of options to set the following possible class properties:
*
* - baseURI
* - timeout
* - any other request options to use as defaults.
*
Expand All @@ -116,13 +108,13 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
throw HTTPException::forMissingCurl(); // @codeCoverageIgnore
}

$uri->useRawQueryString();
parent::__construct(Method::GET, $uri);

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

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

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

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

if (array_key_exists('baseURI', $options)) {
$uri = new URI($options['baseURI']);
$uri->useRawQueryString();
unset($options['baseURI']);
} else {
$uri = $this->uri;
}
$this->parseOptions($options);

$url = $this->prepareURL($url);
$url = $this->prepareURL($url, $uri);

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

Expand Down Expand Up @@ -293,11 +292,6 @@ public function setJSON($data)
*/
protected function parseOptions(array $options)
{
if (array_key_exists('baseURI', $options)) {
$this->baseURI = $this->baseURI->setURI($options['baseURI']);
unset($options['baseURI']);
}

if (array_key_exists('headers', $options) && is_array($options['headers'])) {
foreach ($options['headers'] as $name => $value) {
$this->setHeader($name, $value);
Expand Down Expand Up @@ -325,16 +319,16 @@ protected function parseOptions(array $options)

/**
* If the $url is a relative URL, will attempt to create
* a full URL by prepending $this->baseURI to it.
* a full URL by prepending $uri to it.
*/
protected function prepareURL(string $url): string
protected function prepareURL(string $url, URI $uri): string
{
// If it's a full URI, then we have nothing to do here...
if (str_contains($url, '://')) {
return $url;
}

$uri = $this->baseURI->resolveRelativeURI($url);
$uri = $uri->resolveRelativeURI($url);

// Create the string instead of casting to prevent baseURL muddling
return URI::createURIString(
Expand Down
6 changes: 0 additions & 6 deletions system/Test/Mock/MockCURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ protected function sendRequest(array $curlOptions = []): string
return $this->output;
}

// for testing purposes only
public function getBaseURI()
{
return $this->baseURI;
}

// for testing purposes only
public function getDelay()
{
Expand Down