From d0a32133b9ea19ed75b2c42317a84fa2f5d3b263 Mon Sep 17 00:00:00 2001 From: Hubert Kowalski Date: Wed, 30 Oct 2019 20:32:11 +0100 Subject: [PATCH 1/4] Add PSR-3 log for frame logging, move current per-byte logging to dedicated debugLog --- composer.json | 3 ++- src/AfriCC/EPP/AbstractClient.php | 36 ++++++++++++++++++++++++++++--- src/AfriCC/EPP/Client.php | 12 +++++++---- src/AfriCC/EPP/HTTPClient.php | 24 +++++++++++++++++++-- 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 32d19bb..a68254c 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "require": { "php": ">=5.5.0", "ext-intl": "*", - "ext-openssl": "*" + "ext-openssl": "*", + "psr/log": "^1.1" }, "require-dev": { "php-coveralls/php-coveralls": "^1.0|^2.0" diff --git a/src/AfriCC/EPP/AbstractClient.php b/src/AfriCC/EPP/AbstractClient.php index e772b50..d537835 100644 --- a/src/AfriCC/EPP/AbstractClient.php +++ b/src/AfriCC/EPP/AbstractClient.php @@ -6,6 +6,8 @@ use AfriCC\EPP\Frame\Response as ResponseFrame; use AfriCC\EPP\Frame\ResponseFactory; use Exception; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; /** * An abstract client client for the Extensible Provisioning Protocol (EPP) @@ -18,8 +20,10 @@ * As this class is abstract and relies on subclass implementation details it's untestable * @codeCoverageIgnore */ -abstract class AbstractClient implements ClientInterface +abstract class AbstractClient implements ClientInterface, LoggerAwareInterface { + use LoggerAwareTrait; + protected $host; protected $port; protected $username; @@ -45,7 +49,30 @@ abstract public function connect($newPassword = false); abstract public function close(); - abstract protected function log($message); + /** + * Prints out debugging data (eg raw frame bytes) + * + * @param string $message + */ + abstract protected function debugLog($message); + + protected function logCommand(FrameInterface $frame) + { + if (isset($this->logger)) { + $command = \get_class($frame); + $frame_xml = (string) $frame; + $this->logger->info("Sending EPP Command '$command'. Full frame: $frame_xml"); + } + } + + protected function logResponse(ResponseFrame $frame) + { + if (isset($this->logger)) { + $type = \get_class($frame); + $frame_xml = (string) $frame; + $this->logger->info("Received EPP Response '$type'. Full frame: $frame_xml"); + } + } /** * Send frame to EPP server @@ -73,11 +100,14 @@ public function request(FrameInterface $frame) ); } + $this->logCommand($frame); $this->sendFrame($frame); $return = $this->getFrame(); + $response = ResponseFactory::build($return, $this->objectSpec); + $this->logResponse($response); - return ResponseFactory::build($return, $this->objectSpec); + return $response; } public function __construct(array $config, ObjectSpec $objectSpec = null) diff --git a/src/AfriCC/EPP/Client.php b/src/AfriCC/EPP/Client.php index 4358d3e..95835cc 100644 --- a/src/AfriCC/EPP/Client.php +++ b/src/AfriCC/EPP/Client.php @@ -13,6 +13,8 @@ use AfriCC\EPP\Frame\Command\Logout as LogoutCommand; use Exception; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; /** * A high level TCP (SSL) based client for the Extensible Provisioning Protocol (EPP) @@ -22,8 +24,10 @@ * As this class deals directly with sockets it's untestable * @codeCoverageIgnore */ -class Client extends AbstractClient implements ClientInterface +class Client extends AbstractClient implements ClientInterface, LoggerAwareInterface { + use LoggerAwareTrait; + protected $socket; protected $chunk_size; protected $verify_peer_name; @@ -196,7 +200,7 @@ public function sendFrame(FrameInterface $frame) return $this->send($header . $buffer); } - protected function log($message, $color = '0;32') + protected function debugLog($message, $color = '0;32') { if ($message === '' || !$this->debug) { return; @@ -236,7 +240,7 @@ private function recv($length) // If the buffer actually contains something then add it to the result if ($buffer !== false) { - $this->log($buffer); + $this->debuLog($buffer); $result .= $buffer; // break if all data received @@ -289,7 +293,7 @@ private function send($buffer) // If we read something, bump up the position if ($written) { if ($this->debug) { - $this->log(mb_substr($buffer, $pos, $wlen, 'ASCII'), '1;31'); + $this->debugLog(mb_substr($buffer, $pos, $wlen, 'ASCII'), '1;31'); } $pos += $written; diff --git a/src/AfriCC/EPP/HTTPClient.php b/src/AfriCC/EPP/HTTPClient.php index 03c9be7..3671a01 100644 --- a/src/AfriCC/EPP/HTTPClient.php +++ b/src/AfriCC/EPP/HTTPClient.php @@ -3,6 +3,8 @@ namespace AfriCC\EPP; use AfriCC\EPP\Frame\Command\Logout as LogoutCommand; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\LoggerAwareInterface; /** * A high level HTTP(S) based client for the Extensible Provisioning Protocol (EPP) @@ -12,10 +14,13 @@ * As this class deals directly with cURL it's untestable * @codeCoverageIgnore */ -class HTTPClient extends AbstractClient implements ClientInterface +class HTTPClient extends AbstractClient implements ClientInterface, LoggerAwareInterface { + use LoggerAwareTrait; + protected $curl; protected $cookiejar; + protected $curlDebugStream; public function __construct(array $config, ObjectSpec $objectSpec = null) { @@ -68,6 +73,10 @@ private function setupCurl() if ($this->ssl) { $this->setupCurlSSL(); } + + if ($this->debug) { + $this->setupCurlDebug(); + } } private function setupCurlOpts() @@ -109,6 +118,13 @@ private function setupCurlSSL() } } + private function setupCurlDebug() + { + curl_setopt($this->curl, CURLOPT_VERBOSE, true); + $this->curlDebugStream = fopen('php://temp', 'w+'); + curl_setopt($this->curl, CURLOPT_STDERR, $this->curlDebugStream); + } + /** * Open a new connection to the EPP server * @@ -156,16 +172,20 @@ public function getFrame() if ($return === false) { $code = curl_errno($this->curl); $msg = curl_error($this->curl); + $this->debugLog("cURL error: $msg"); throw new \Exception($msg, $code); } return $return; } - protected function log($message) + protected function debugLog($message) { if ($this->debug) { \error_log($message); + rewind($this->curlDebugStream); + $curlDebug = stream_get_contents($this->curlDebugStream); + \error_log("Full info:\n$curlDebug"); } } From 4b37e2353265b84c4583927b9199aef89c14fe6e Mon Sep 17 00:00:00 2001 From: Hubert Kowalski Date: Wed, 30 Oct 2019 21:19:47 +0100 Subject: [PATCH 2/4] StyleCI Fixes --- src/AfriCC/EPP/HTTPClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AfriCC/EPP/HTTPClient.php b/src/AfriCC/EPP/HTTPClient.php index 3671a01..9069e51 100644 --- a/src/AfriCC/EPP/HTTPClient.php +++ b/src/AfriCC/EPP/HTTPClient.php @@ -3,8 +3,8 @@ namespace AfriCC\EPP; use AfriCC\EPP\Frame\Command\Logout as LogoutCommand; -use Psr\Log\LoggerAwareTrait; use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; /** * A high level HTTP(S) based client for the Extensible Provisioning Protocol (EPP) From 3083f4c2248b61eb9e8a2a092bf15f95e5f8bb25 Mon Sep 17 00:00:00 2001 From: Hubert Kowalski Date: Wed, 30 Oct 2019 21:35:22 +0100 Subject: [PATCH 3/4] Scruntizer Fixes --- src/AfriCC/EPP/AbstractClient.php | 9 ++++++--- src/AfriCC/EPP/Client.php | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/AfriCC/EPP/AbstractClient.php b/src/AfriCC/EPP/AbstractClient.php index d537835..5236fd2 100644 --- a/src/AfriCC/EPP/AbstractClient.php +++ b/src/AfriCC/EPP/AbstractClient.php @@ -65,12 +65,15 @@ protected function logCommand(FrameInterface $frame) } } - protected function logResponse(ResponseFrame $frame) + /** + * @param ResponseFrame|string $frame + */ + protected function logResponse($frame) { if (isset($this->logger)) { - $type = \get_class($frame); + $type = $frame instanceof ResponseFrame ? \get_class($frame) : 'INCORRECT'; $frame_xml = (string) $frame; - $this->logger->info("Received EPP Response '$type'. Full frame: $frame_xml"); + $this->logger->info("Received EPP '$type' Response. Full frame: $frame_xml"); } } diff --git a/src/AfriCC/EPP/Client.php b/src/AfriCC/EPP/Client.php index 95835cc..64a5648 100644 --- a/src/AfriCC/EPP/Client.php +++ b/src/AfriCC/EPP/Client.php @@ -240,7 +240,7 @@ private function recv($length) // If the buffer actually contains something then add it to the result if ($buffer !== false) { - $this->debuLog($buffer); + $this->debugLog($buffer); $result .= $buffer; // break if all data received From 09db884032e549fb989432eca6e0a54012693485 Mon Sep 17 00:00:00 2001 From: Hubert Kowalski Date: Wed, 30 Oct 2019 21:39:29 +0100 Subject: [PATCH 4/4] Add info to Readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 03aa43b..52cf37b 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Features * modern PHP standards * [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/) & [PSR-4](http://www.php-fig.org/psr/psr-4/) + * [PSR-3](http://www.php-fig.org/psr/psr-3/) for logging command and response frames * notice and warning free (find them, and I'll fix it!) * high-level usage (Plug & Play) * simplified client for socket and http(s) connections (auto login/logout, auto inject clTRID)