Updated to HttpSocket, HttpResponse and CakeSocket use SocketException.

This commit is contained in:
Juan Basso 2010-12-15 02:01:00 -02:00
parent c0e2f63f7d
commit 754c9b2c3d
6 changed files with 29 additions and 21 deletions

View file

@ -100,7 +100,7 @@ class CakeSocket {
* Connect the socket to the given host and port. * Connect the socket to the given host and port.
* *
* @return boolean Success * @return boolean Success
* @throws Exception * @throws SocketException
*/ */
public function connect() { public function connect() {
if ($this->connection != null) { if ($this->connection != null) {
@ -120,7 +120,7 @@ class CakeSocket {
if (!empty($errNum) || !empty($errStr)) { if (!empty($errNum) || !empty($errStr)) {
$this->setLastError($errStr, $errNum); $this->setLastError($errStr, $errNum);
throw new Exception($errStr, $errNum); throw new SocketException($errStr, $errNum);
} }
$this->connected = is_resource($this->connection); $this->connected = is_resource($this->connection);

View file

@ -430,6 +430,14 @@ class CakeSessionException extends CakeException { }
*/ */
class ConfigureException extends CakeException { } class ConfigureException extends CakeException { }
/**
* Exception class for Socket. This exception will be thrown from CakeSocket, HttpSocket and HttpResponse when it
* encounters an error.
*
* @package cake.libs
*/
class SocketException extends CakeException { }
/** /**
* Exception class for Xml. This exception will be thrown from Xml when it * Exception class for Xml. This exception will be thrown from Xml when it
* encounters an error. * encounters an error.

View file

@ -123,15 +123,15 @@ class HttpResponse implements ArrayAccess {
* *
* @param string $message Message to parse * @param string $message Message to parse
* @return void * @return void
* @throw Exception * @throw SocketException
*/ */
public function parseResponse($message) { public function parseResponse($message) {
if (!is_string($message)) { if (!is_string($message)) {
throw new Exception(__('Invalid response.')); throw new SocketException(__('Invalid response.'));
} }
if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) { if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) {
throw new Exception(__('Invalid HTTP response.')); throw new SocketException(__('Invalid HTTP response.'));
} }
list(, $statusLine, $header) = $match; list(, $statusLine, $header) = $match;
@ -187,7 +187,7 @@ class HttpResponse implements ArrayAccess {
* *
* @param string $body A string continaing the chunked body to decode. * @param string $body A string continaing the chunked body to decode.
* @return mixed Array of response headers and body or false. * @return mixed Array of response headers and body or false.
* @throws Exception * @throws SocketException
*/ */
protected function _decodeChunkedBody($body) { protected function _decodeChunkedBody($body) {
if (!is_string($body)) { if (!is_string($body)) {
@ -199,7 +199,7 @@ class HttpResponse implements ArrayAccess {
while ($chunkLength !== 0) { while ($chunkLength !== 0) {
if (!preg_match("/^([0-9a-f]+) *(?:;(.+)=(.+))?\r\n/iU", $body, $match)) { if (!preg_match("/^([0-9a-f]+) *(?:;(.+)=(.+))?\r\n/iU", $body, $match)) {
throw new Exception(__('HttpSocket::_decodeChunkedBody - Could not parse malformed chunk.')); throw new SocketException(__('HttpSocket::_decodeChunkedBody - Could not parse malformed chunk.'));
} }
$chunkSize = 0; $chunkSize = 0;

View file

@ -205,7 +205,7 @@ class HttpSocket extends CakeSocket {
* *
* @param mixed $resource Resource or false to disable the resource use * @param mixed $resource Resource or false to disable the resource use
* @return void * @return void
* @throw Exception * @throw SocketException
*/ */
public function setContentResource($resource) { public function setContentResource($resource) {
if ($resource === false) { if ($resource === false) {
@ -213,7 +213,7 @@ class HttpSocket extends CakeSocket {
return; return;
} }
if (!is_resource($resource)) { if (!is_resource($resource)) {
throw new Exception(__('Invalid resource.')); throw new SocketException(__('Invalid resource.'));
} }
$this->_contentResource = $resource; $this->_contentResource = $resource;
} }
@ -366,7 +366,7 @@ class HttpSocket extends CakeSocket {
} }
if (!App::import('Lib', $this->responseClass)) { if (!App::import('Lib', $this->responseClass)) {
throw new Exception(__('Class %s not found.', $this->responseClass)); throw new SocketException(__('Class %s not found.', $this->responseClass));
} }
$responseClass = $this->responseClass; $responseClass = $this->responseClass;
$this->response = new $responseClass($response); $this->response = new $responseClass($response);
@ -525,7 +525,7 @@ class HttpSocket extends CakeSocket {
* Set authentication in request * Set authentication in request
* *
* @return void * @return void
* @throws Exception * @throws SocketException
*/ */
protected function _setAuth() { protected function _setAuth() {
if (empty($this->_auth)) { if (empty($this->_auth)) {
@ -534,10 +534,10 @@ class HttpSocket extends CakeSocket {
$method = key($this->_auth); $method = key($this->_auth);
$authClass = Inflector::camelize($method) . 'Authentication'; $authClass = Inflector::camelize($method) . 'Authentication';
if (!App::import('Lib', 'http/' . $authClass)) { if (!App::import('Lib', 'http/' . $authClass)) {
throw new Exception(__('Unknown authentication method.')); throw new SocketException(__('Unknown authentication method.'));
} }
if (!method_exists($authClass, 'authentication')) { if (!method_exists($authClass, 'authentication')) {
throw new Exception(sprintf(__('The %s do not support authentication.'), $authClass)); throw new SocketException(sprintf(__('The %s do not support authentication.'), $authClass));
} }
call_user_func("$authClass::authentication", $this, &$this->_auth[$method]); call_user_func("$authClass::authentication", $this, &$this->_auth[$method]);
} }
@ -546,7 +546,7 @@ class HttpSocket extends CakeSocket {
* Set the proxy configuration and authentication * Set the proxy configuration and authentication
* *
* @return void * @return void
* @throws Exception * @throws SocketException
*/ */
protected function _setProxy() { protected function _setProxy() {
if (empty($this->_proxy) || !isset($this->_proxy['host'], $this->_proxy['port'])) { if (empty($this->_proxy) || !isset($this->_proxy['host'], $this->_proxy['port'])) {
@ -560,10 +560,10 @@ class HttpSocket extends CakeSocket {
} }
$authClass = Inflector::camelize($this->_proxy['method']) . 'Authentication'; $authClass = Inflector::camelize($this->_proxy['method']) . 'Authentication';
if (!App::import('Lib', 'http/' . $authClass)) { if (!App::import('Lib', 'http/' . $authClass)) {
throw new Exception(__('Unknown authentication method for proxy.')); throw new SocketException(__('Unknown authentication method for proxy.'));
} }
if (!method_exists($authClass, 'proxyAuthentication')) { if (!method_exists($authClass, 'proxyAuthentication')) {
throw new Exception(sprintf(__('The %s do not support proxy authentication.'), $authClass)); throw new SocketException(sprintf(__('The %s do not support proxy authentication.'), $authClass));
} }
call_user_func("$authClass::proxyAuthentication", $this, &$this->_proxy); call_user_func("$authClass::proxyAuthentication", $this, &$this->_proxy);
} }
@ -773,7 +773,7 @@ class HttpSocket extends CakeSocket {
* @param array $request Needs to contain a 'uri' key. Should also contain a 'method' key, otherwise defaults to GET. * @param array $request Needs to contain a 'uri' key. Should also contain a 'method' key, otherwise defaults to GET.
* @param string $versionToken The version token to use, defaults to HTTP/1.1 * @param string $versionToken The version token to use, defaults to HTTP/1.1
* @return string Request line * @return string Request line
* @throws Exception * @throws SocketException
*/ */
protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1.1') { protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1.1') {
$asteriskMethods = array('OPTIONS'); $asteriskMethods = array('OPTIONS');
@ -781,7 +781,7 @@ class HttpSocket extends CakeSocket {
if (is_string($request)) { if (is_string($request)) {
$isValid = preg_match("/(.+) (.+) (.+)\r\n/U", $request, $match); $isValid = preg_match("/(.+) (.+) (.+)\r\n/U", $request, $match);
if (!$this->quirksMode && (!$isValid || ($match[2] == '*' && !in_array($match[3], $asteriskMethods)))) { if (!$this->quirksMode && (!$isValid || ($match[2] == '*' && !in_array($match[3], $asteriskMethods)))) {
throw new Exception(__('HttpSocket::_buildRequestLine - Passed an invalid request line string. Activate quirks mode to do this.')); throw new SocketException(__('HttpSocket::_buildRequestLine - Passed an invalid request line string. Activate quirks mode to do this.'));
} }
return $request; return $request;
} elseif (!is_array($request)) { } elseif (!is_array($request)) {
@ -799,7 +799,7 @@ class HttpSocket extends CakeSocket {
} }
if (!$this->quirksMode && $request['uri'] === '*' && !in_array($request['method'], $asteriskMethods)) { if (!$this->quirksMode && $request['uri'] === '*' && !in_array($request['method'], $asteriskMethods)) {
throw new Exception(__('HttpSocket::_buildRequestLine - The "*" asterisk character is only allowed for the following methods: %s. Activate quirks mode to work outside of HTTP/1.1 specs.', implode(',', $asteriskMethods))); throw new SocketException(__('HttpSocket::_buildRequestLine - The "*" asterisk character is only allowed for the following methods: %s. Activate quirks mode to work outside of HTTP/1.1 specs.', implode(',', $asteriskMethods)));
} }
return $request['method'] . ' ' . $request['uri'] . ' ' . $versionToken . "\r\n"; return $request['method'] . ' ' . $request['uri'] . ' ' . $versionToken . "\r\n";
} }

View file

@ -117,7 +117,7 @@ class CakeSocketTest extends CakeTestCase {
* testInvalidConnection method * testInvalidConnection method
* *
* @dataProvider invalidConnections * @dataProvider invalidConnections
* @expectedException Exception * @expectedException SocketException
* return void * return void
*/ */
public function testInvalidConnection($data) { public function testInvalidConnection($data) {

View file

@ -295,7 +295,7 @@ class HttpResponseTest extends CakeTestCase {
* testInvalidParseResponseData * testInvalidParseResponseData
* *
* @dataProvider invalidParseResponseDataProvider * @dataProvider invalidParseResponseDataProvider
* @expectedException Exception * @expectedException SocketException
* return void * return void
*/ */
public function testInvalidParseResponseData($value) { public function testInvalidParseResponseData($value) {