Skip to content
Permalink
Browse files

Updated to HttpSocket, HttpResponse and CakeSocket use SocketException.

  • Loading branch information...
jrbasso committed Dec 15, 2010
1 parent c0e2f63 commit 754c9b2c3dd1198c6fb91c479520fc7c0ef6a396
@@ -100,7 +100,7 @@ function __construct($config = array()) {
* Connect the socket to the given host and port.
*
* @return boolean Success
* @throws Exception
* @throws SocketException
*/
public function connect() {
if ($this->connection != null) {
@@ -120,7 +120,7 @@ public function connect() {
if (!empty($errNum) || !empty($errStr)) {
$this->setLastError($errStr, $errNum);
throw new Exception($errStr, $errNum);
throw new SocketException($errStr, $errNum);
}
$this->connected = is_resource($this->connection);
@@ -430,6 +430,14 @@ class CakeSessionException 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
* encounters an error.
@@ -123,15 +123,15 @@ public function isOk() {
*
* @param string $message Message to parse
* @return void
* @throw Exception
* @throw SocketException
*/
public function parseResponse($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)) {
throw new Exception(__('Invalid HTTP response.'));
throw new SocketException(__('Invalid HTTP response.'));
}
list(, $statusLine, $header) = $match;
@@ -187,7 +187,7 @@ protected function _decodeBody($body, $encoding = 'chunked') {
*
* @param string $body A string continaing the chunked body to decode.
* @return mixed Array of response headers and body or false.
* @throws Exception
* @throws SocketException
*/
protected function _decodeChunkedBody($body) {
if (!is_string($body)) {
@@ -199,7 +199,7 @@ protected function _decodeChunkedBody($body) {
while ($chunkLength !== 0) {
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;
@@ -205,15 +205,15 @@ public function configProxy($host, $port = 3128, $method = null, $user = null, $
*
* @param mixed $resource Resource or false to disable the resource use
* @return void
* @throw Exception
* @throw SocketException
*/
public function setContentResource($resource) {
if ($resource === false) {
$this->_contentResource = null;
return;
}
if (!is_resource($resource)) {
throw new Exception(__('Invalid resource.'));
throw new SocketException(__('Invalid resource.'));
}
$this->_contentResource = $resource;
}
@@ -366,7 +366,7 @@ public function request($request = array()) {
}
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;
$this->response = new $responseClass($response);
@@ -525,7 +525,7 @@ public function url($url = null, $uriTemplate = null) {
* Set authentication in request
*
* @return void
* @throws Exception
* @throws SocketException
*/
protected function _setAuth() {
if (empty($this->_auth)) {
@@ -534,10 +534,10 @@ protected function _setAuth() {
$method = key($this->_auth);
$authClass = Inflector::camelize($method) . 'Authentication';
if (!App::import('Lib', 'http/' . $authClass)) {
throw new Exception(__('Unknown authentication method.'));
throw new SocketException(__('Unknown authentication method.'));
}
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]);
}
@@ -546,7 +546,7 @@ protected function _setAuth() {
* Set the proxy configuration and authentication
*
* @return void
* @throws Exception
* @throws SocketException
*/
protected function _setProxy() {
if (empty($this->_proxy) || !isset($this->_proxy['host'], $this->_proxy['port'])) {
@@ -560,10 +560,10 @@ protected function _setProxy() {
}
$authClass = Inflector::camelize($this->_proxy['method']) . 'Authentication';
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')) {
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);
}
@@ -773,15 +773,15 @@ protected function _parseQuery($query) {
* @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
* @return string Request line
* @throws Exception
* @throws SocketException
*/
protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1.1') {
$asteriskMethods = array('OPTIONS');
if (is_string($request)) {
$isValid = preg_match("/(.+) (.+) (.+)\r\n/U", $request, $match);
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;
} elseif (!is_array($request)) {
@@ -799,7 +799,7 @@ protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1
}
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";
}
@@ -117,7 +117,7 @@ public static function invalidConnections() {
* testInvalidConnection method
*
* @dataProvider invalidConnections
* @expectedException Exception
* @expectedException SocketException
* return void
*/
public function testInvalidConnection($data) {
@@ -295,7 +295,7 @@ public static function invalidParseResponseDataProvider() {
* testInvalidParseResponseData
*
* @dataProvider invalidParseResponseDataProvider
* @expectedException Exception
* @expectedException SocketException
* return void
*/
public function testInvalidParseResponseData($value) {

0 comments on commit 754c9b2

Please sign in to comment.
You can’t perform that action at this time.