-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
lbajsarowicz
wants to merge
5
commits into
magento:2.4-develop
Choose a base branch
from
lbajsarowicz:2.4-develop-missing-curl-methods
base: 2.4-develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d97e390
SwiftOtter-SOP-348 Match CURL Client to RFC-9110 and RFC-5789
lbajsarowicz 07fa7da
SwiftOtter-SOP-348 Pass HTTP payload `curlopt`
lbajsarowicz a8fe648
SwiftOtter-SOP-348 Simplify method signature to match TestFramework's…
lbajsarowicz d4de3ca
SOP-348 #36336 Fix invalid `DELETE` method reference (no payload)
lbajsarowicz 07062c7
Merge branch '2.4-develop' into 2.4-develop-missing-curl-methods
engcom-Hotel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +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 <[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', | ||
'OPTIONS' | ||
]; | ||
|
||
/** | ||
* Max supported protocol by curl CURL_SSLVERSION_TLSv1_2 | ||
* @var int | ||
|
@@ -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) | ||
{ | ||
|
@@ -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($uri) | ||
{ | ||
$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 | ||
* | ||
|
@@ -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)) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
void
as the function return typeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per the documentation, delete method might have Payload. So I think we can add
$params
as an optional variable. I think here @bgorski is just pointing out that we are not passing$params
but using it in the makeRequest method.