-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,13 +39,6 @@ class CURLRequest extends OutgoingRequest | |
*/ | ||
protected $responseOrig; | ||
|
||
/** | ||
* The URI associated with this request | ||
* | ||
* @var URI | ||
*/ | ||
protected $baseURI; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bc break. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. backward compatibility break, remove protected property is not allowed on fix pr. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Maybe I should submit to next version. |
||
|
||
/** | ||
* The setting values | ||
* | ||
|
@@ -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. | ||
* | ||
|
@@ -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 */ | ||
|
@@ -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)); | ||
|
||
|
@@ -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); | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parentheses should not needed: