From dc8d2f2582fc2a6f6a6f9d973ffc713609b118b3 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Thu, 12 Dec 2024 12:12:26 +0100 Subject: [PATCH 1/3] SwiftOtter-SOP-348 Match CURL Client to RFC-9110 and RFC-5789 --- .../Model/ElasticsearchVersionChecker.php | 6 +- .../Magento/TestFramework/Helper/Amqp.php | 8 +- .../Magento/TestFramework/Helper/Curl.php | 24 +--- .../Magento/Framework/HTTP/Client/Curl.php | 123 ++++++++++++++++-- 4 files changed, 123 insertions(+), 38 deletions(-) diff --git a/dev/tests/integration/_files/Magento/TestModuleCatalogSearch/Model/ElasticsearchVersionChecker.php b/dev/tests/integration/_files/Magento/TestModuleCatalogSearch/Model/ElasticsearchVersionChecker.php index 5e006a0aa1197..5432ed7abeb29 100644 --- a/dev/tests/integration/_files/Magento/TestModuleCatalogSearch/Model/ElasticsearchVersionChecker.php +++ b/dev/tests/integration/_files/Magento/TestModuleCatalogSearch/Model/ElasticsearchVersionChecker.php @@ -1,13 +1,13 @@ deploymentConfig = $deploymentConfig ?? \Magento\TestFramework\Helper\Bootstrap::getObjectManager() ->get(\Magento\Framework\App\DeploymentConfig::class); - $this->curl = new Curl(); + $this->curl = new \Magento\Framework\HTTP\Client\Curl(); $this->curl->setCredentials( $this->deploymentConfig->get(self::CONFIG_PATH_USER), $this->deploymentConfig->get(self::CONFIG_PATH_PASSWORD) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php b/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php index 62b753cf5d344..46766477e359d 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php @@ -1,30 +1,18 @@ makeRequest("DELETE", $uri); - } } diff --git a/lib/internal/Magento/Framework/HTTP/Client/Curl.php b/lib/internal/Magento/Framework/HTTP/Client/Curl.php index 5379d481289f5..28fa37d6d3c15 100644 --- a/lib/internal/Magento/Framework/HTTP/Client/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Client/Curl.php @@ -1,15 +1,17 @@ * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) + * @api */ class Curl implements \Magento\Framework\HTTP\ClientInterface { @@ -26,7 +28,6 @@ class Curl implements \Magento\Framework\HTTP\ClientInterface protected $_host = 'localhost'; /** - * Port * @var int */ protected $_port = 80; @@ -56,19 +57,16 @@ class Curl implements \Magento\Framework\HTTP\ClientInterface protected $_cookies = []; /** - * Response headers * @var array */ protected $_responseHeaders = []; /** - * Response body * @var string */ protected $_responseBody = ''; /** - * Response status * @var int */ protected $_responseStatus = 0; @@ -223,6 +221,8 @@ public function removeCookies() * * @param string $uri uri relative to host, ex. "/index.php" * @return void + * + * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.1 */ public function get($uri) { @@ -239,13 +239,109 @@ public function get($uri) * @param array|string $params * @return void * - * @see \Magento\Framework\HTTP\Client#post($uri, $params) + * @see \Magento\Framework\HTTP\Client::post($uri, $params) + * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.3 */ public function post($uri, $params) { $this->makeRequest("POST", $uri, $params); } + /** + * Make PUT request + * + * @param string $uri + * @param array|string $params + * @return void + * + * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.4 + */ + public function put(string $uri, string|array $params): void + { + $this->makeRequest("PUT", $uri, $params); + } + + /** + * Make DELETE request + * + * @param string $uri + * @param array|string $params + * @return void + * + * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.5 + */ + public function delete(string $uri, array|string $params = []): void + { + $this->makeRequest("DELETE", $uri, $params); + } + + /** + * Make PATCH request + * + * @param string $uri + * @param array|string $params + * @return void + * + * @url https://www.rfc-editor.org/info/rfc5789 + */ + public function patch(string $uri, array|string $params): void + { + $this->makeRequest("PATCH", $uri, $params); + } + + /** + * Make OPTIONS request + * + * @param string $uri + * @param array|string $params + * @return void + * + * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.7 + */ + public function options(string $uri, array|string $params = []): void + { + $this->makeRequest("OPTIONS", $uri, $params); + } + + /** + * Make HEAD request + * + * @param string $uri + * @return void + * + * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.2 + */ + public function head(string $uri): void + { + $this->makeRequest("HEAD", $uri); + } + + /** + * Make TRACE request + * + * @param string $uri + * @return void + * + * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.8 + */ + public function trace(string $uri): void + { + $this->makeRequest("TRACE", $uri); + } + + /** + * Make CONNECT request + * + * @param string $uri + * @return void + * + * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.6 + */ + public function connect(string $uri): void + { + $this->makeRequest("CONNECT", $uri); + } + /** * Get response headers * @@ -278,7 +374,7 @@ public function getCookies() } $out = []; foreach ($this->_responseHeaders['Set-Cookie'] as $row) { - $values = explode("; ", $row); + $values = explode("; ", $row ?? ''); $c = count($values); if (!$c) { continue; @@ -305,7 +401,7 @@ public function getCookiesFull() } $out = []; foreach ($this->_responseHeaders['Set-Cookie'] as $row) { - $values = explode("; ", $row); + $values = explode("; ", $row ?? ''); $c = count($values); if (!$c) { continue; @@ -322,7 +418,7 @@ public function getCookiesFull() } for ($i = 0; $i < $c; $i++) { list($subkey, $val) = explode("=", $values[$i]); - $out[trim($key)][trim($subkey)] = trim($val); + $out[trim($key)][trim($subkey)] = $val !== null ? trim($val) : ''; } } return $out; @@ -438,6 +534,7 @@ public function doError($string) */ protected function parseHeaders($ch, $data) { + $data = $data !== null ? $data : ''; if ($this->_headerCount == 0) { $line = explode(" ", trim($data), 3); if (count($line) < 2) { @@ -469,7 +566,7 @@ protected function parseHeaders($ch, $data) * Set curl option directly * * @param string $name - * @param string $value + * @param mixed $value * @return void */ protected function curlOption($name, $value) @@ -503,7 +600,7 @@ public function setOptions($arr) * Set curl option * * @param string $name - * @param string $value + * @param mixed $value * @return void */ public function setOption($name, $value) From 4af7f36db1a7ffc31e9df177ed8ed27c42c10e69 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Thu, 12 Dec 2024 19:50:11 +0100 Subject: [PATCH 2/3] SwiftOtter-SOP-348 Pass HTTP payload `curlopt` --- .../Magento/Framework/HTTP/Client/Curl.php | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/HTTP/Client/Curl.php b/lib/internal/Magento/Framework/HTTP/Client/Curl.php index 28fa37d6d3c15..718b61a63890d 100644 --- a/lib/internal/Magento/Framework/HTTP/Client/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Client/Curl.php @@ -15,6 +15,17 @@ */ class Curl implements \Magento\Framework\HTTP\ClientInterface { + /** + * @url https://www.rfc-editor.org/rfc/rfc9110.html + */ + private const HTTP_METHODS_WITH_PAYLOAD = [ + 'POST', + 'PUT', + 'PATCH', + 'DELETE', + 'OPTIONS' + ]; + /** * Max supported protocol by curl CURL_SSLVERSION_TLSv1_2 * @var int @@ -455,13 +466,24 @@ protected function makeRequest($method, $uri, $params = []) $this->_ch = curl_init(); $this->curlOption(CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FTP | CURLPROTO_FTPS); $this->curlOption(CURLOPT_URL, $uri); - if ($method == 'POST') { - $this->curlOption(CURLOPT_POST, 1); + + if (in_array($method, self::HTTP_METHODS_WITH_PAYLOAD)) { $this->curlOption(CURLOPT_POSTFIELDS, is_array($params) ? http_build_query($params) : $params); - } elseif ($method == "GET") { - $this->curlOption(CURLOPT_HTTPGET, 1); - } else { - $this->curlOption(CURLOPT_CUSTOMREQUEST, $method); + } + + switch ($method) { + case 'POST': + $this->curlOption(CURLOPT_POST, 1); + break; + case 'PUT': + $this->curlOption(CURLOPT_PUT, 1); + break; + case "GET": + $this->curlOption(CURLOPT_HTTPGET, 1); + break; + default: + $this->curlOption(CURLOPT_CUSTOMREQUEST, $method); + break; } if (count($this->_headers)) { From 08bec19ee36e71e74961e3c60d21a5fe729a67db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Bajsarowicz?= Date: Fri, 17 Jan 2025 08:26:24 +1100 Subject: [PATCH 3/3] SOP-348 #36336 Fix invalid `DELETE` method reference (no payload) --- lib/internal/Magento/Framework/HTTP/Client/Curl.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/HTTP/Client/Curl.php b/lib/internal/Magento/Framework/HTTP/Client/Curl.php index 718b61a63890d..4a342ceab7169 100644 --- a/lib/internal/Magento/Framework/HTTP/Client/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Client/Curl.php @@ -22,7 +22,6 @@ class Curl implements \Magento\Framework\HTTP\ClientInterface 'POST', 'PUT', 'PATCH', - 'DELETE', 'OPTIONS' ]; @@ -276,14 +275,13 @@ public function put(string $uri, string|array $params): void * Make DELETE request * * @param string $uri - * @param array|string $params * @return void * * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.5 */ public function delete(string $uri, array|string $params = []): void { - $this->makeRequest("DELETE", $uri, $params); + $this->makeRequest("DELETE", $uri); } /**