Skip to content

Commit

Permalink
Add better debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
srjlewis committed May 30, 2020
1 parent 2257d8a commit 845b85d
Showing 1 changed file with 52 additions and 42 deletions.
94 changes: 52 additions & 42 deletions src/JsonRPC/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class HttpClient
* HttpClient constructor
*
* @access public
* @param string $url
* @param string $url
*/
public function __construct($url = '')
{
Expand All @@ -115,7 +115,7 @@ public function __construct($url = '')
* Set URL
*
* @access public
* @param string $url
* @param string $url
* @return $this
*/
public function withUrl($url)
Expand All @@ -128,7 +128,7 @@ public function withUrl($url)
* Set username
*
* @access public
* @param string $username
* @param string $username
* @return $this
*/
public function withUsername($username)
Expand All @@ -141,7 +141,7 @@ public function withUsername($username)
* Set password
*
* @access public
* @param string $password
* @param string $password
* @return $this
*/
public function withPassword($password)
Expand All @@ -154,7 +154,7 @@ public function withPassword($password)
* Set timeout
*
* @access public
* @param integer $timeout
* @param integer $timeout
* @return $this
*/
public function withTimeout($timeout)
Expand All @@ -167,7 +167,7 @@ public function withTimeout($timeout)
* Set headers
*
* @access public
* @param array $headers
* @param array $headers
* @return $this
*/
public function withHeaders(array $headers)
Expand All @@ -180,8 +180,8 @@ public function withHeaders(array $headers)
* Set cookies
*
* @access public
* @param array $cookies
* @param boolean $replace
* @param array $cookies
* @param boolean $replace
*/
public function withCookies(array $cookies, $replace = false)
{
Expand Down Expand Up @@ -232,7 +232,7 @@ public function withSslLocalCert($path)
* Assign a callback before the request
*
* @access public
* @param Closure $closure
* @param Closure $closure
* @return $this
*/
public function withBeforeRequestCallback(Closure $closure)
Expand All @@ -256,30 +256,32 @@ public function getCookies()
* Do the HTTP request
*
* @access public
* @throws ConnectionFailureException
* @param string $payload
* @param string[] $headers Headers for this request
* @param string $payload
* @param string[] $headers Headers for this request
* @return array
* @throws ConnectionFailureException
*/
public function execute($payload, array $headers = array())
{
if (is_callable($this->beforeRequest)) {
call_user_func_array($this->beforeRequest, array($this, $payload, $headers));
}

$rawResponse = '';

if ($this->isCurlLoaded()) {
$ch = curl_init();
$ch = curl_init();
$requestHeaders = $this->buildHeaders($headers);
$headers = array();
$headers = array();
curl_setopt_array($ch, array(
CURLOPT_URL => trim($this->url),
CURLOPT_URL => trim($this->url),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => $this->timeout,
CURLOPT_MAXREDIRS => 2,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => $this->verifySslCertificate,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => $requestHeaders,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => $requestHeaders,
CURLOPT_HEADERFUNCTION => function ($curl, $header) use (&$headers) {
$headers[] = $header;
return strlen($header);
Expand All @@ -288,31 +290,39 @@ public function execute($payload, array $headers = array())
if ($this->sslLocalCert !== null) {
curl_setopt($ch, CURLOPT_CAINFO, $this->sslLocalCert);
}
$response = curl_exec($ch);
$rawResponse = $response = curl_exec($ch);
curl_close($ch);
if ($response !== false) {
$response = json_decode($response, true);
} else {
throw new ConnectionFailureException('Unable to establish a connection');
}
} else {
$stream = fopen(trim($this->url), 'r', false, $this->buildContext($payload, $headers));
$requestHeaders = $headers;
$stream = fopen(trim($this->url), 'r', false, $this->buildContext($payload, $requestHeaders));

if (! is_resource($stream)) {
if (!is_resource($stream)) {
throw new ConnectionFailureException('Unable to establish a connection');
}

$metadata = stream_get_meta_data($stream);
$headers = $metadata['wrapper_data'];
$headers = $metadata['wrapper_data'];
$response = json_decode(stream_get_contents($stream), true);

fclose($stream);
}

if ($this->debug) {
error_log('==> Request: '.PHP_EOL.(is_string($payload) ? $payload : json_encode($payload, JSON_PRETTY_PRINT)));
error_log('==> Headers: '.PHP_EOL.var_export($headers, true));
error_log('==> Response: '.PHP_EOL.json_encode($response, JSON_PRETTY_PRINT));
$debugResponse = json_decode($rawResponse, true);
if ($debugResponse && json_last_error() === JSON_ERROR_NONE) {
$debugResponse = json_encode($response, JSON_PRETTY_PRINT);
} else {
$debugResponse = $rawResponse;
}
error_log('==> Request: ' . PHP_EOL . implode(PHP_EOL,
$requestHeaders) . PHP_EOL . (is_string($payload) ? $payload : json_encode($payload,
JSON_PRETTY_PRINT)));
error_log('==> Response: ' . PHP_EOL . implode(PHP_EOL, $headers) . PHP_EOL . $debugResponse);
}

$this->handleExceptions($headers);
Expand All @@ -325,8 +335,8 @@ public function execute($payload, array $headers = array())
* Prepare stream context
*
* @access protected
* @param string $payload
* @param string[] $headers
* @param string $payload
* @param string[] $headers
* @return resource
*/
protected function buildContext($payload, array $headers = array())
Expand All @@ -335,16 +345,16 @@ protected function buildContext($payload, array $headers = array())

$options = array(
'http' => array(
'method' => 'POST',
'method' => 'POST',
'protocol_version' => 1.1,
'timeout' => $this->timeout,
'max_redirects' => 2,
'header' => implode("\r\n", $headers),
'content' => $payload,
'ignore_errors' => true,
'timeout' => $this->timeout,
'max_redirects' => 2,
'header' => implode("\r\n", $headers),
'content' => $payload,
'ignore_errors' => true,
),
'ssl' => array(
'verify_peer' => $this->verifySslCertificate,
'ssl' => array(
'verify_peer' => $this->verifySslCertificate,
'verify_peer_name' => $this->verifySslCertificate,
)
);
Expand All @@ -360,7 +370,7 @@ protected function buildContext($payload, array $headers = array())
* Parse cookies from response
*
* @access protected
* @param array $headers
* @param array $headers
*/
protected function parseCookies(array $headers)
{
Expand All @@ -374,8 +384,8 @@ protected function parseCookies(array $headers)
$item = explode('=', $cookie);

if (count($item) === 2) {
$name = trim($item[0]);
$value = $item[1];
$name = trim($item[0]);
$value = $item[1];
$this->cookies[$name] = $value;
}
}
Expand All @@ -387,7 +397,7 @@ protected function parseCookies(array $headers)
* Throw an exception according the HTTP response
*
* @access public
* @param array $headers
* @param array $headers
* @throws AccessDeniedException
* @throws ServerErrorException
*/
Expand All @@ -402,8 +412,8 @@ public function handleExceptions(array $headers)

foreach ($headers as $header) {
foreach ($exceptions as $code => $exception) {
if (strpos($header, 'HTTP/1.0 '.$code) !== false || strpos($header, 'HTTP/1.1 '.$code) !== false) {
throw new $exception('Response: '.$header);
if (strpos($header, 'HTTP/1.0 ' . $code) !== false || strpos($header, 'HTTP/1.1 ' . $code) !== false) {
throw new $exception('Response: ' . $header);
}
}
}
Expand Down

0 comments on commit 845b85d

Please sign in to comment.