From 2448ba1c974c02a277b1c60810a54bff20dc483c Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 12:30:04 -0400 Subject: [PATCH 01/13] Allow for CURL options to be set, and expose errors --- composer.json | 2 +- src/org/apache/hadoop/WebHDFS.php | 13 +++++++++++-- src/org/apache/hadoop/tools/Curl.php | 17 ++++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index cfd42a2..31dacdd 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "PHP WebHDFS, forked from https://github.com/simpleenergy/php-WebHDFS", "minimum-stability": "stable", "license": "MIT", - "version": "1.0.8", + "version": "1.0.9", "authors": [ { "name": "tranch-xiao", diff --git a/src/org/apache/hadoop/WebHDFS.php b/src/org/apache/hadoop/WebHDFS.php index fe664ef..15b2730 100644 --- a/src/org/apache/hadoop/WebHDFS.php +++ b/src/org/apache/hadoop/WebHDFS.php @@ -24,7 +24,8 @@ public function __construct( $user, $namenodeRpcHost, $namenodeRpcPort, - $debug + $curl_options = [], + $debug = false ) { $this->host = $host; $this->port = $port; @@ -32,7 +33,15 @@ public function __construct( $this->namenode_rpc_host = $namenodeRpcHost; $this->namenode_rpc_port = $namenodeRpcPort; $this->debug = $debug; - $this->curl = new Curl($this->debug); + $this->curl = new Curl($curl_options, $this->debug); + } + + /** + * @return Curl + */ + public function getCurl() + { + return $this->curl; } // File and Directory Operations diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index c62451c..61207b7 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -13,11 +13,22 @@ class Curl */ private $options; - public function __construct($debug = false) + /** + * @var array $curl_options Key value array of curl options. @see https://www.php.net/manual/en/function.curl-setopt.php + */ + private $curl_options; + + public function __construct($curl_options = [], $debug = false) { + $this->curl_options = $curl_options; $this->debug = $debug; } + public function setCurlOptions(array $curl_options) + { + $this->curl_options = $curl_options; + } + /** * * @param $localPath string local file path to save @@ -182,6 +193,8 @@ public function validateLastRequest($cleanLastRequestIfValid = false) private function _exec($options, $returnInfo = false) { $ch = curl_init(); + $options = array_merge($this->curl_options, $options); + if ($this->debug === true) { $options[CURLOPT_VERBOSE] = true; } @@ -231,6 +244,8 @@ private function _exec($options, $returnInfo = false) $result = curl_exec($ch); $this->lastRequestContentResult = $result; $this->lastRequestInfoResult = curl_getinfo($ch); + $this->lastRequestInfoResult['curl_errno'] = curl_errno($ch); + $this->lastRequestInfoResult['curl_error'] = curl_error($ch); if ($returnInfo) { $result = $this->lastRequestInfoResult; } From ae784ac731849c1e755da20bb55073e730af3f51 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 12:35:16 -0400 Subject: [PATCH 02/13] Add comments --- src/org/apache/hadoop/tools/Curl.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index 61207b7..7134935 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -20,10 +20,17 @@ class Curl public function __construct($curl_options = [], $debug = false) { - $this->curl_options = $curl_options; + $this->setCurlOptions($curl_options); $this->debug = $debug; } + /** + * Set an array of curl options. Keys are CURLOPT_* constants, values are the value to be set. + * @see https://www.php.net/manual/en/function.curl-setopt-array.php + * + * @param array $curl_options Array of curl options to set + * @return void + */ public function setCurlOptions(array $curl_options) { $this->curl_options = $curl_options; From f761f4a565b681d5027c642a437399af5dd4d989 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 12:35:48 -0400 Subject: [PATCH 03/13] @link --- src/org/apache/hadoop/tools/Curl.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index 7134935..31ecf11 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -14,7 +14,8 @@ class Curl private $options; /** - * @var array $curl_options Key value array of curl options. @see https://www.php.net/manual/en/function.curl-setopt.php + * @var array $curl_options Key value array of curl options. + * @link https://www.php.net/manual/en/function.curl-setopt.php */ private $curl_options; @@ -26,7 +27,7 @@ public function __construct($curl_options = [], $debug = false) /** * Set an array of curl options. Keys are CURLOPT_* constants, values are the value to be set. - * @see https://www.php.net/manual/en/function.curl-setopt-array.php + * @link https://www.php.net/manual/en/function.curl-setopt-array.php * * @param array $curl_options Array of curl options to set * @return void From db172681c56ce76123f25ae9d1f4dad6d5d8350a Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 12:36:47 -0400 Subject: [PATCH 04/13] Add comment --- src/org/apache/hadoop/tools/Curl.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index 31ecf11..02675a4 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -19,6 +19,10 @@ class Curl */ private $curl_options; + /** + * @param array $curl_options Key value array of curl options. @link https://www.php.net/manual/en/function.curl-setopt.php + * @param bool $debug Optional debug parameter, sets CURLOPT_VERBOSE if true. + */ public function __construct($curl_options = [], $debug = false) { $this->setCurlOptions($curl_options); From cbcb4072db46eb05673775d356d0049fd0198395 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 12:50:39 -0400 Subject: [PATCH 05/13] Fail if no status code --- src/org/apache/hadoop/tools/Curl.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index 02675a4..ab9c1e0 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -192,9 +192,15 @@ public function getLastRequestInfoResult($cleanLastRequest = false) public function validateLastRequest($cleanLastRequestIfValid = false) { $http_code = $this->getLastRequestInfoResult()['http_code']; + + if (!$http_code) { + return false; + } + if ($http_code >= 400 && $http_code <= 500) { return false; } + if ($cleanLastRequestIfValid) { $this->cleanLastRequest(); } From a160754064874587be4f4508a19669cd1e66e966 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 12:52:43 -0400 Subject: [PATCH 06/13] $options = array(); --- src/org/apache/hadoop/tools/Curl.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index ab9c1e0..2a86356 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -84,6 +84,7 @@ public function postLocation($url) private function _findRedirectUrl($url, $options) { + $options = array(); $options[CURLOPT_URL] = $url; $options[CURLOPT_HEADER] = true; $options[CURLOPT_RETURNTRANSFER] = true; @@ -100,6 +101,7 @@ private function _findRedirectUrl($url, $options) public function putFile($url, $filename) { + $options = array(); $options[CURLOPT_URL] = $url; $options[CURLOPT_PUT] = true; $handle = fopen($filename, "r"); @@ -113,6 +115,7 @@ public function putFile($url, $filename) public function putData($url, $data, $contentType = 'application/json') { + $options = array(); $options[CURLOPT_URL] = $url; // $options[CURLOPT_PUT] = true; $options[CURLOPT_CUSTOMREQUEST] = 'PUT'; @@ -129,6 +132,7 @@ public function putData($url, $data, $contentType = 'application/json') public function postString($url, $string) { + $options = array(); $options[CURLOPT_URL] = $url; $options[CURLOPT_POST] = true; $options[CURLOPT_POSTFIELDS] = $string; @@ -200,7 +204,7 @@ public function validateLastRequest($cleanLastRequestIfValid = false) if ($http_code >= 400 && $http_code <= 500) { return false; } - + if ($cleanLastRequestIfValid) { $this->cleanLastRequest(); } From a4aa4c4a93d85c31415a6ec003057f02529606ec Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 13:18:59 -0400 Subject: [PATCH 07/13] += --- src/org/apache/hadoop/tools/Curl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index 2a86356..defaa7b 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -215,7 +215,7 @@ public function validateLastRequest($cleanLastRequestIfValid = false) private function _exec($options, $returnInfo = false) { $ch = curl_init(); - $options = array_merge($this->curl_options, $options); + $options += $this->curl_options; if ($this->debug === true) { $options[CURLOPT_VERBOSE] = true; From f70007b7eee0ec9b82058427d624f43291b8139b Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 13:28:57 -0400 Subject: [PATCH 08/13] Add docs --- src/org/apache/hadoop/WebHDFS.php | 17 +++++++++++++++++ src/org/apache/hadoop/tools/Curl.php | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/org/apache/hadoop/WebHDFS.php b/src/org/apache/hadoop/WebHDFS.php index 15b2730..d9cf244 100644 --- a/src/org/apache/hadoop/WebHDFS.php +++ b/src/org/apache/hadoop/WebHDFS.php @@ -44,6 +44,23 @@ public function getCurl() return $this->curl; } + /** + * Get the result of the last CURL command. + * @return mixed + */ + public function getLastRequestContentResult() + { + return $this->getCurl()->getLastRequestContentResult(); + } + + /** + * @return array + */ + public function getLastRequestInfoResult() + { + return $this->getCurl()->getLastRequestInfoResult(); + } + // File and Directory Operations public function create( diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index defaa7b..1930ae0 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -4,9 +4,15 @@ class Curl { + /** @var bool Optional debug flag, enables CURLOPT_VERBOSE */ private $debug; + + /** @var mixed Content result from the last CURL call */ private $lastRequestContentResult; + + /** @var array Info about last CURL result from curl_getinfo */ private $lastRequestInfoResult; + /** * @var array * curl options @@ -173,6 +179,12 @@ public function delete($url) return $this->_exec($options); } + /** + * Get the content form the last CURL call. + * + * @param bool $cleanLastRequest Clear the last CURL content result after getting. + * @return mixed + */ public function getLastRequestContentResult($cleanLastRequest = false) { $r = $this->lastRequestContentResult; @@ -183,6 +195,13 @@ public function getLastRequestContentResult($cleanLastRequest = false) return $r; } + /** + * Get the curl info from the last CURL call. + * @link https://www.php.net/manual/en/function.curl-getinfo.php + * + * @param bool $cleanLastRequest Clear last CURL info result after getting. + * @return array + */ public function getLastRequestInfoResult($cleanLastRequest = false) { $r = $this->lastRequestInfoResult; From 8acdd01bf25b3e2e06274be5cb8d302d64176495 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 13:32:23 -0400 Subject: [PATCH 09/13] Add comment --- src/org/apache/hadoop/WebHDFS.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/org/apache/hadoop/WebHDFS.php b/src/org/apache/hadoop/WebHDFS.php index d9cf244..ad8d1d6 100644 --- a/src/org/apache/hadoop/WebHDFS.php +++ b/src/org/apache/hadoop/WebHDFS.php @@ -46,6 +46,7 @@ public function getCurl() /** * Get the result of the last CURL command. + * * @return mixed */ public function getLastRequestContentResult() @@ -54,6 +55,9 @@ public function getLastRequestContentResult() } /** + * Get the curl info from the last CURL call. + * @link https://www.php.net/manual/en/function.curl-getinfo.php + * * @return array */ public function getLastRequestInfoResult() From 55299ac1c5121dc451f96ecc4f4bcb485de2eefe Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 13:33:06 -0400 Subject: [PATCH 10/13] Remove options --- src/org/apache/hadoop/tools/Curl.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index 1930ae0..d76c534 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -88,9 +88,8 @@ public function postLocation($url) return $this->_findRedirectUrl($url, array(CURLOPT_POST => true)); } - private function _findRedirectUrl($url, $options) + private function _findRedirectUrl($url, $options = []) { - $options = array(); $options[CURLOPT_URL] = $url; $options[CURLOPT_HEADER] = true; $options[CURLOPT_RETURNTRANSFER] = true; From ebec7691773950a6dc5a741f981a2230f80d8ad1 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 13:35:09 -0400 Subject: [PATCH 11/13] Comment --- src/org/apache/hadoop/tools/Curl.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index d76c534..7d12d28 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -104,9 +104,8 @@ private function _findRedirectUrl($url, $options = []) return null; } - public function putFile($url, $filename) + public function putFile($url, $filename, $options = array()) { - $options = array(); $options[CURLOPT_URL] = $url; $options[CURLOPT_PUT] = true; $handle = fopen($filename, "r"); @@ -118,9 +117,8 @@ public function putFile($url, $filename) return ('201' == $info['http_code']); } - public function putData($url, $data, $contentType = 'application/json') + public function putData($url, $data, $options = array()) { - $options = array(); $options[CURLOPT_URL] = $url; // $options[CURLOPT_PUT] = true; $options[CURLOPT_CUSTOMREQUEST] = 'PUT'; @@ -135,9 +133,8 @@ public function putData($url, $data, $contentType = 'application/json') return ('201' == $info['http_code']); } - public function postString($url, $string) + public function postString($url, $string, $options = array()) { - $options = array(); $options[CURLOPT_URL] = $url; $options[CURLOPT_POST] = true; $options[CURLOPT_POSTFIELDS] = $string; @@ -147,9 +144,8 @@ public function postString($url, $string) return ('200' == $info['http_code']); } - public function put($url) + public function put($url, $options = array()) { - $options = array(); $options[CURLOPT_URL] = $url; $options[CURLOPT_PUT] = true; $options[CURLOPT_RETURNTRANSFER] = true; @@ -158,9 +154,8 @@ public function put($url) return $this->_exec($options); } - public function post($url) + public function post($url, $options = array()) { - $options = array(); $options[CURLOPT_URL] = $url; $options[CURLOPT_POST] = true; $options[CURLOPT_RETURNTRANSFER] = true; @@ -168,9 +163,8 @@ public function post($url) return $this->_exec($options); } - public function delete($url) + public function delete($url, $options = array()) { - $options = array(); $options[CURLOPT_URL] = $url; $options[CURLOPT_CUSTOMREQUEST] = "DELETE"; $options[CURLOPT_RETURNTRANSFER] = true; @@ -211,6 +205,12 @@ public function getLastRequestInfoResult($cleanLastRequest = false) return $r; } + /** + * Validate if the last CURL response is within the 2xx-3xx HTTP status code range. + * + * @param bool $cleanLastRequestIfValid Clear last CURL info result after getting. + * @return bool + */ public function validateLastRequest($cleanLastRequestIfValid = false) { $http_code = $this->getLastRequestInfoResult()['http_code']; From 7400621ebb9939e15417ee409e6d90eb4c2ddb03 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 14:19:21 -0400 Subject: [PATCH 12/13] Check curl error --- src/org/apache/hadoop/tools/Curl.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index 7d12d28..b6ae258 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -223,6 +223,11 @@ public function validateLastRequest($cleanLastRequestIfValid = false) return false; } + $curl_errno = $this->getLastRequestInfoResult()['curl_errno']; + if ($curl_errno !== CURLE_OK) { + return false; + } + if ($cleanLastRequestIfValid) { $this->cleanLastRequest(); } From aa06ca0d9e914fb3ca28aec94dc5974217516ece Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Wed, 26 Jun 2024 14:19:36 -0400 Subject: [PATCH 13/13] Update comment --- src/org/apache/hadoop/tools/Curl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/apache/hadoop/tools/Curl.php b/src/org/apache/hadoop/tools/Curl.php index b6ae258..0a25793 100644 --- a/src/org/apache/hadoop/tools/Curl.php +++ b/src/org/apache/hadoop/tools/Curl.php @@ -206,7 +206,7 @@ public function getLastRequestInfoResult($cleanLastRequest = false) } /** - * Validate if the last CURL response is within the 2xx-3xx HTTP status code range. + * Validate if the last CURL response is within the 2xx-3xx HTTP status code range and has no curl errors. * * @param bool $cleanLastRequestIfValid Clear last CURL info result after getting. * @return bool