From f7b71ea9c6add87244e65ffe2cea68d03e84c320 Mon Sep 17 00:00:00 2001 From: Charl Joseph Mert <cmert@lbsd.net> Date: Fri, 8 Mar 2013 16:31:54 +0200 Subject: [PATCH] Net EPP using Exceptions instead of Pear Error Signed-off-by: Charl Mert <cmert@lbsd.net> Change-Id: Ia2a79274b408cba75220a98a1a863af69836c317 --- modules/registrars/cozaepp/Net/EPP/Client.php | 29 ++++++++++--------- .../registrars/cozaepp/Net/EPP/Protocol.php | 23 +++++++++------ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/modules/registrars/cozaepp/Net/EPP/Client.php b/modules/registrars/cozaepp/Net/EPP/Client.php index 7ba2c22..bf7af69 100644 --- a/modules/registrars/cozaepp/Net/EPP/Client.php +++ b/modules/registrars/cozaepp/Net/EPP/Client.php @@ -24,7 +24,6 @@ * @revision $Id: Client.php,v 1.13 2010/10/21 11:55:07 gavin Exp $ */ - require_once('PEAR.php'); require_once('Net/EPP/Protocol.php'); $GLOBALS['Net_EPP_Client_Version'] = '0.0.4'; @@ -45,13 +44,15 @@ * This method establishes the connection to the server. If the connection was * established, then this method will call getFrame() and return the EPP <greeting> * frame which is sent by the server upon connection. If connection fails, then - * a PEAR_Error object explaining the error will be returned instead. + * an exception with a message explaining the error will be thrown and handled + * in the calling code. * @param string $host the hostname * @param integer $port the TCP port * @param integer $timeout the timeout in seconds * @param boolean $ssl whether to connect using SSL * @param resource $context a stream resource to use when setting up the socket connection - * @return PEAR_Error|string a PEAR_Error on failure, or a string containing the server <greeting> + * @throws Exception on connection errors + * @return a string containing the server <greeting> */ function connect($host, $port=700, $timeout=1, $ssl=true, $context=NULL) { $target = sprintf('%s://%s:%d', ($ssl === true ? 'tls' : 'tcp'), $host, $port); @@ -61,8 +62,8 @@ } else { $result = stream_socket_client($target, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT); } - if (!$result) { - return new PEAR_Error("Error connecting to $target: $errstr (code $errno)"); + if ($result === False) { + throw new Exception("Error connecting to $target: $errstr (code $errno)"); } @@ -71,11 +72,11 @@ // Set stream timeout if (!stream_set_timeout($this->socket,$timeout)) { - return new PEAR_Error("Failed to set timeout on socket: $errstr (code $errno)"); + throw new Exception("Failed to set timeout on socket: $errstr (code $errno)"); } // Set blocking if (!stream_set_blocking($this->socket,0)) { - return new PEAR_Error("Failed to set blocking on socket: $errstr (code $errno)"); + throw new Exception("Failed to set blocking on socket: $errstr (code $errno)"); } return $this->getFrame(); @@ -85,9 +86,9 @@ * Get an EPP frame from the server. * This retrieves a frame from the server. Since the connection is blocking, this * method will wait until one becomes available. If the connection has been broken, - * this method will return a PEAR_Error object, otherwise it will return a string - * containing the XML from the server - * @return PEAR_Error|string a PEAR_Error on failure, or a string containing the frame + * this method will return a string containing the XML from the server + * @throws Exception on frame errors + * @return a string containing the frame */ function getFrame() { return Net_EPP_Protocol::getFrame($this->socket); @@ -97,6 +98,7 @@ * Send an XML frame to the server. * This method sends an EPP frame to the server. * @param string the XML data to send + * @throws Exception when it doesn't complete the write to the socket * @return boolean the result of the fwrite() operation */ function sendFrame($xml) { @@ -106,12 +108,11 @@ /** * a wrapper around sendFrame() and getFrame() * @param string $xml the frame to send to the server - * @return PEAR_Error|string the frame returned by the server, or an error object + * @throws Exception when it doesn't complete the write to the socket + * @return string the frame returned by the server, or an error object */ function request($xml) { - if (PEAR::isError($res = $this->sendFrame($xml))) { - return $res; - } + $res = $this->sendFrame($xml); return $this->getFrame(); } diff --git a/modules/registrars/cozaepp/Net/EPP/Protocol.php b/modules/registrars/cozaepp/Net/EPP/Protocol.php index b12f481..bd4f210 100644 --- a/modules/registrars/cozaepp/Net/EPP/Protocol.php +++ b/modules/registrars/cozaepp/Net/EPP/Protocol.php @@ -24,8 +24,6 @@ * @revision $Id: Protocol.php,v 1.4 2011/06/28 09:48:02 gavin Exp $ */ -require_once('PEAR.php'); - /** * Low-level functions useful for both EPP clients and servers * @package Net_EPP @@ -37,6 +35,8 @@ class Net_EPP_Protocol { // Loop reading and checking info to see if we hit timeout $info = stream_get_meta_data($socket); + $time_start = microtime(true); + while (!$info['timed_out'] && !feof($socket)) { // Try read remaining data from socket $buffer = @fread($socket,$length - strlen($result)); @@ -53,11 +53,15 @@ class Net_EPP_Protocol { } // Update metadata $info = stream_get_meta_data($socket); + $time_end = microtime(true); + if (($time_end - $time_start) > 5) { + throw new exception('Timeout while contacting EPP Server'); + } } // Check for timeout if ($info['timed_out']) { - return new PEAR_Error('Timeout while reading data from socket'); + throw new Exception('Timeout while reading data from socket'); } return $result; @@ -66,19 +70,18 @@ class Net_EPP_Protocol { /** * get an EPP frame from the remote peer * @param resource $socket a socket connected to the remote peer - * @return PEAR_Error|string either an error or a string + * @throws Exception on frame errors. + * @return string the frame */ static function getFrame($socket) { // Read header - if (PEAR::isError($hdr = Net_EPP_Protocol::_fread_nb($socket,4))) { - return $hdr; - } + $hdr = Net_EPP_Protocol::_fread_nb($socket,4); // Unpack first 4 bytes which is our length $unpacked = unpack('N', $hdr); $length = $unpacked[1]; if ($length < 5) { - return new PEAR_Error(sprintf('Got a bad frame header length of %d bytes from peer', $length)); + throw new Exception(sprintf('Got a bad frame header length of %d bytes from peer', $length)); } else { $length -= 4; // discard the length of the header itself @@ -91,6 +94,8 @@ class Net_EPP_Protocol { * send an EPP frame to the remote peer * @param resource $socket a socket connected to the remote peer * @param string $xml the XML to send + * @throws Exception when it doesn't complete the write to the socket + * @return the amount of bytes written to the frame */ static function sendFrame($socket, $xml) { // Grab XML length & add on 4 bytes for the counter @@ -98,7 +103,7 @@ class Net_EPP_Protocol { $res = fwrite($socket, pack('N',$length) . $xml); // Check our write matches if ($length != $res) { - return new PEAR_Error("Short write when sending XML"); + throw new Exception("Short write when sending XML"); } return $res; -- GitLab