From d97e3906956bac22cb155f336b833de96eaa1b74 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Thu, 12 Dec 2024 12:19:01 +0100 Subject: [PATCH 1/3] 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/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 a8fe648e54385a80bdb3545b9ecf069b0e941c73 Mon Sep 17 00:00:00 2001 From: Lukasz Bajsarowicz Date: Wed, 18 Dec 2024 12:16:41 +0100 Subject: [PATCH 3/3] 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); }