diff --git a/system/Config/Services.php b/system/Config/Services.php index c0d5fcc392d2..3ec00110e694 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -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 */ @@ -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 ); diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index 9da231c17716..956c293b8761 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -39,13 +39,6 @@ class CURLRequest extends OutgoingRequest */ protected $responseOrig; - /** - * The URI associated with this request - * - * @var URI - */ - protected $baseURI; - /** * 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,7 +127,7 @@ 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 */ @@ -143,9 +135,16 @@ public function request($method, string $url, array $options = []): ResponseInte { $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 { // 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( diff --git a/system/Test/Mock/MockCURLRequest.php b/system/Test/Mock/MockCURLRequest.php index 97f80e7c1da2..1c0c8b0af03a 100644 --- a/system/Test/Mock/MockCURLRequest.php +++ b/system/Test/Mock/MockCURLRequest.php @@ -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() {