From d97e3906956bac22cb155f336b833de96eaa1b74 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Thu, 12 Dec 2024 12:19:01 +0100 Subject: [PATCH 1/8] SwiftOtter-SOP-348 Match CURL Client to RFC-9110 and RFC-5789 --- .../Magento/Framework/HTTP/Client/Curl.php | 106 +++++++++++++++++- 1 file changed, 101 insertions(+), 5 deletions(-) diff --git a/lib/internal/Magento/Framework/HTTP/Client/Curl.php b/lib/internal/Magento/Framework/HTTP/Client/Curl.php index 7ba4fb915e294..28fa37d6d3c15 100644 --- a/lib/internal/Magento/Framework/HTTP/Client/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Client/Curl.php @@ -1,9 +1,8 @@ * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * @api */ @@ -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 * From 07fa7da90ee5e7282203cf4ac287aff6a5414541 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Thu, 12 Dec 2024 19:50:11 +0100 Subject: [PATCH 2/8] 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 a8fe648e54385a80bdb3545b9ecf069b0e941c73 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Wed, 18 Dec 2024 12:16:41 +0100 Subject: [PATCH 3/8] SwiftOtter-SOP-348 Simplify method signature to match TestFramework's ("backwards compatibility") --- lib/internal/Magento/Framework/HTTP/Client/Curl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/HTTP/Client/Curl.php b/lib/internal/Magento/Framework/HTTP/Client/Curl.php index 718b61a63890d..28edbd7a53143 100644 --- a/lib/internal/Magento/Framework/HTTP/Client/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Client/Curl.php @@ -281,7 +281,7 @@ public function put(string $uri, string|array $params): void * * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.5 */ - public function delete(string $uri, array|string $params = []): void + public function delete($uri) { $this->makeRequest("DELETE", $uri, $params); } From d4de3ca4cc5676f1207316d5e91e5dd3913eb6ab 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 4/8] 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 28edbd7a53143..7e206e9ecd2df 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($uri) { - $this->makeRequest("DELETE", $uri, $params); + $this->makeRequest("DELETE", $uri); } /** From 3c542f486ef63d6caa0200b51bd125cffa4250c7 Mon Sep 17 00:00:00 2001 From: engcom-Charlie Date: Fri, 14 Mar 2025 16:14:32 +0530 Subject: [PATCH 5/8] Worked on review comments --- lib/internal/Magento/Framework/HTTP/Client/Curl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/HTTP/Client/Curl.php b/lib/internal/Magento/Framework/HTTP/Client/Curl.php index 7e206e9ecd2df..bafb9c8980c59 100644 --- a/lib/internal/Magento/Framework/HTTP/Client/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Client/Curl.php @@ -279,7 +279,7 @@ public function put(string $uri, string|array $params): void * * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.5 */ - public function delete($uri) + public function delete($uri): void { $this->makeRequest("DELETE", $uri); } From 6748954f6fa72d079bc32e3a59cfe1d5a44e4ee3 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Fri, 14 Mar 2025 12:10:15 +0100 Subject: [PATCH 6/8] Code Review / Adjust `delete` method declaration --- .../framework/Magento/TestFramework/Helper/Curl.php | 8 +++----- lib/internal/Magento/Framework/HTTP/Client/Curl.php | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php b/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php index 62b753cf5d344..89cec8c76dd73 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php @@ -15,15 +15,13 @@ class Curl extends CurlLibrary /** * Make DELETE request * - * String type was added to parameter $param in order to support sending JSON or XML requests. - * This feature was added base on Community Pull Request https://github.com/magento/magento2/pull/8373 - * * @param string $uri * @return void * - * @see \Magento\Framework\HTTP\Client#post($uri, $params) + * @deprecated Replace with the core `delete` implementation + * @see \Magento\Framework\HTTP\Client\Curl::delete */ - public function delete($uri) + public function delete($uri): void { $this->makeRequest("DELETE", $uri); } diff --git a/lib/internal/Magento/Framework/HTTP/Client/Curl.php b/lib/internal/Magento/Framework/HTTP/Client/Curl.php index 7e206e9ecd2df..bafb9c8980c59 100644 --- a/lib/internal/Magento/Framework/HTTP/Client/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Client/Curl.php @@ -279,7 +279,7 @@ public function put(string $uri, string|array $params): void * * @url https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.5 */ - public function delete($uri) + public function delete($uri): void { $this->makeRequest("DELETE", $uri); } From fd8319e7ee9a93e8f7abb3c915e152115ee553c8 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Fri, 14 Mar 2025 14:17:42 +0100 Subject: [PATCH 7/8] Code Review / Adjust `delete` method declaration --- .../framework/Magento/TestFramework/Helper/Curl.php | 5 +++-- lib/internal/Magento/Framework/HTTP/Client/Curl.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php b/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php index 89cec8c76dd73..7a64ad3853032 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php @@ -16,13 +16,14 @@ class Curl extends CurlLibrary * Make DELETE request * * @param string $uri + * @param array|string $params * @return void * * @deprecated Replace with the core `delete` implementation * @see \Magento\Framework\HTTP\Client\Curl::delete */ - public function delete($uri): void + public function delete($uri, array|string $params = []): void { - $this->makeRequest("DELETE", $uri); + $this->makeRequest("DELETE", $uri, $params); } } diff --git a/lib/internal/Magento/Framework/HTTP/Client/Curl.php b/lib/internal/Magento/Framework/HTTP/Client/Curl.php index bafb9c8980c59..a5132ca740d97 100644 --- a/lib/internal/Magento/Framework/HTTP/Client/Curl.php +++ b/lib/internal/Magento/Framework/HTTP/Client/Curl.php @@ -275,13 +275,14 @@ 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($uri): void + public function delete($uri, array|string $params = []): void { - $this->makeRequest("DELETE", $uri); + $this->makeRequest("DELETE", $uri, $params); } /** From ac3d0b790c624bf89a865623c806a7682d3d0bd2 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Mon, 24 Mar 2025 15:23:42 +0100 Subject: [PATCH 8/8] SwiftOtter-SOP-348 Add missing copyright change --- .../framework/Magento/TestFramework/Helper/Curl.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php b/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php index 7a64ad3853032..5057e1c6d284c 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Helper/Curl.php @@ -1,7 +1,7 @@