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.5] Add support for missing Curl methods to the Curl client #39469

Open
wants to merge 3 commits into
base: 2.5-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2022 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\TestModuleCatalogSearch\Model;

use Magento\TestFramework\Helper\Curl;
use Magento\Framework\HTTP\Client\Curl;

/**
* Retrieve elasticsearch version by curl request
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2018 Adobe
* All Rights Reserved.
*/

declare(strict_types=1);
Expand All @@ -19,7 +19,7 @@ class Amqp
const DEFAULT_MANAGEMENT_PORT = '15672';

/**
* @var Curl
* @var \Magento\Framework\HTTP\Client\Curl
*/
private $curl;

Expand All @@ -44,7 +44,7 @@ public function __construct(
) {
$this->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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2018 Adobe
* All Rights Reserved.
*/

declare(strict_types=1);

namespace Magento\TestFramework\Helper;

use Magento\Framework\HTTP\Client\Curl as CurlLibrary;

/**
* @deprecated All the curl methods are included in \Magento\Framework\HTTP\Client\Curl class.
* @see \Magento\Framework\HTTP\Client\Curl
*/
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)
*/
public function delete($uri)
{
$this->makeRequest("DELETE", $uri);
}
}
155 changes: 136 additions & 19 deletions lib/internal/Magento/Framework/HTTP/Client/Curl.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
<?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 <core@magentocommerce.com>
* @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',
'OPTIONS'
];

/**
* Max supported protocol by curl CURL_SSLVERSION_TLSv1_2
* @var int
Expand All @@ -26,7 +38,6 @@ class Curl implements \Magento\Framework\HTTP\ClientInterface
protected $_host = 'localhost';

/**
* Port
* @var int
*/
protected $_port = 80;
Expand Down Expand Up @@ -56,19 +67,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;
Expand Down Expand Up @@ -223,6 +231,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 +249,108 @@ 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
* @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);
}

/**
* 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 @@ -278,7 +383,7 @@ public function getCookies()
}
$out = [];
foreach ($this->_responseHeaders['Set-Cookie'] as $row) {
$values = explode("; ", $row);
$values = explode("; ", $row ?? '');
$c = count($values);
if (!$c) {
continue;
Expand All @@ -305,7 +410,7 @@ public function getCookiesFull()
}
$out = [];
foreach ($this->_responseHeaders['Set-Cookie'] as $row) {
$values = explode("; ", $row);
$values = explode("; ", $row ?? '');
$c = count($values);
if (!$c) {
continue;
Expand All @@ -322,7 +427,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;
Expand Down Expand Up @@ -359,13 +464,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 Expand Up @@ -438,6 +554,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) {
Expand Down Expand Up @@ -469,7 +586,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)
Expand Down Expand Up @@ -503,7 +620,7 @@ public function setOptions($arr)
* Set curl option
*
* @param string $name
* @param string $value
* @param mixed $value
* @return void
*/
public function setOption($name, $value)
Expand Down