Skip to content
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

[2.4] Add support for missing Curl methods to the Curl client #39471

Open
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 129 additions & 11 deletions lib/internal/Magento/Framework/HTTP/Client/Curl.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2011 Adobe
* All Rights Reserved.
*/

declare(strict_types=1);

namespace Magento\Framework\HTTP\Client;

/**
* Class to work with HTTP protocol using curl library
*
* @author Magento Core Team <[email protected]>
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @api
*/
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
Expand Down Expand Up @@ -223,6 +232,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)
{
Expand All @@ -239,13 +250,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($uri)
{
$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
*
Expand Down Expand Up @@ -359,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)) {
Expand Down