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

fix: Optimize the usage of option 'baseURI' for CURLRequest #9273

Closed
wants to merge 1 commit 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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parentheses should not needed:

Suggested change
$uri = new URI($options['baseURI'] ?? ($options['base_uri'] ?? null));
$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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc break.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excuse me, this is my first time to submit pull request. What's meaning 'bc break'? And what should I do next?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backward compatibility break, remove protected property is not allowed on fix pr.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Maybe I should submit to next version.


/**
* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc break as well.

{
// 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
Loading