Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 32 additions & 2 deletions src/org/apache/hadoop/WebHDFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,45 @@ public function __construct(
$user,
$namenodeRpcHost,
$namenodeRpcPort,
$debug
$curl_options = [],
$debug = false
) {
$this->host = $host;
$this->port = $port;
$this->user = $user;
$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;
}

/**
* Get the result of the last CURL command.
*
* @return mixed
*/
public function getLastRequestContentResult()
{
return $this->getCurl()->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()
{
return $this->getCurl()->getLastRequestInfoResult();
}

// File and Directory Operations
Expand Down
82 changes: 71 additions & 11 deletions src/org/apache/hadoop/tools/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,49 @@

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
*/
private $options;

public function __construct($debug = false)
/**
* @var array $curl_options Key value array of curl options.
* @link https://www.php.net/manual/en/function.curl-setopt.php
*/
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);
$this->debug = $debug;
}

/**
* Set an array of curl options. Keys are CURLOPT_* constants, values are the value to be set.
* @link 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;
}

/**
*
* @param $localPath string local file path to save
Expand Down Expand Up @@ -59,7 +88,7 @@ public function postLocation($url)
return $this->_findRedirectUrl($url, array(CURLOPT_POST => true));
}

private function _findRedirectUrl($url, $options)
private function _findRedirectUrl($url, $options = [])
{
$options[CURLOPT_URL] = $url;
$options[CURLOPT_HEADER] = true;
Expand All @@ -75,7 +104,7 @@ private function _findRedirectUrl($url, $options)
return null;
}

public function putFile($url, $filename)
public function putFile($url, $filename, $options = array())
{
$options[CURLOPT_URL] = $url;
$options[CURLOPT_PUT] = true;
Expand All @@ -88,7 +117,7 @@ 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[CURLOPT_URL] = $url;
// $options[CURLOPT_PUT] = true;
Expand All @@ -104,7 +133,7 @@ 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[CURLOPT_URL] = $url;
$options[CURLOPT_POST] = true;
Expand All @@ -115,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;
Expand All @@ -126,26 +154,30 @@ 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;

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;

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;
Expand All @@ -156,6 +188,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;
Expand All @@ -166,12 +205,29 @@ public function getLastRequestInfoResult($cleanLastRequest = false)
return $r;
}

/**
* 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
*/
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;
}

$curl_errno = $this->getLastRequestInfoResult()['curl_errno'];
if ($curl_errno !== CURLE_OK) {
return false;
}

if ($cleanLastRequestIfValid) {
$this->cleanLastRequest();
}
Expand All @@ -182,6 +238,8 @@ public function validateLastRequest($cleanLastRequestIfValid = false)
private function _exec($options, $returnInfo = false)
{
$ch = curl_init();
$options += $this->curl_options;

if ($this->debug === true) {
$options[CURLOPT_VERBOSE] = true;
}
Expand Down Expand Up @@ -231,6 +289,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;
}
Expand Down