Skip to content

Commit

Permalink
Updated to HttpSocket, HttpResponse and CakeSocket use SocketException.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Dec 15, 2010
1 parent c0e2f63 commit 754c9b2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cake/libs/cake_socket.php
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions cake/libs/error/exceptions.php
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions cake/libs/http_response.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down
24 changes: 12 additions & 12 deletions cake/libs/http_socket.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -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]);
}
Expand All @@ -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'])) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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";
}
Expand Down
2 changes: 1 addition & 1 deletion cake/tests/cases/libs/cake_socket.test.php
Expand Up @@ -117,7 +117,7 @@ public static function invalidConnections() {
* testInvalidConnection method
*
* @dataProvider invalidConnections
* @expectedException Exception
* @expectedException SocketException
* return void
*/
public function testInvalidConnection($data) {
Expand Down
2 changes: 1 addition & 1 deletion cake/tests/cases/libs/http_response.test.php
Expand Up @@ -295,7 +295,7 @@ public static function invalidParseResponseDataProvider() {
* testInvalidParseResponseData
*
* @dataProvider invalidParseResponseDataProvider
* @expectedException Exception
* @expectedException SocketException
* return void
*/
public function testInvalidParseResponseData($value) {
Expand Down

0 comments on commit 754c9b2

Please sign in to comment.