Skip to content

Commit

Permalink
Merge with 2.0-http.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Dec 15, 2010
2 parents 478d158 + ad5e724 commit c0e2f63
Show file tree
Hide file tree
Showing 11 changed files with 2,102 additions and 984 deletions.
28 changes: 9 additions & 19 deletions cake/libs/cake_socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function __construct($config = array()) {
* Connect the socket to the given host and port.
*
* @return boolean Success
* @throws Exception
*/
public function connect() {
if ($this->connection != null) {
Expand All @@ -112,14 +113,14 @@ public function connect() {
}

if ($this->config['persistent'] == true) {
$tmp = null;
$this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
} else {
$this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
}

if (!empty($errNum) || !empty($errStr)) {
$this->setLastError($errStr, $errNum);
throw new Exception($errStr, $errNum);
}

$this->connected = is_resource($this->connection);
Expand All @@ -137,9 +138,8 @@ public function connect() {
public function host() {
if (Validation::ip($this->config['host'])) {
return gethostbyaddr($this->config['host']);
} else {
return gethostbyaddr($this->address());
}
return gethostbyaddr($this->address());
}

/**
Expand All @@ -150,9 +150,8 @@ public function host() {
public function address() {
if (Validation::ip($this->config['host'])) {
return $this->config['host'];
} else {
return gethostbyname($this->config['host']);
}
return gethostbyname($this->config['host']);
}

/**
Expand All @@ -163,9 +162,8 @@ public function address() {
public function addresses() {
if (Validation::ip($this->config['host'])) {
return array($this->config['host']);
} else {
return gethostbynamel($this->config['host']);
}
return gethostbynamel($this->config['host']);
}

/**
Expand All @@ -176,16 +174,16 @@ public function addresses() {
public function lastError() {
if (!empty($this->lastError)) {
return $this->lastError['num'] . ': ' . $this->lastError['str'];
} else {
return null;
}
return null;
}

/**
* Set the last error.
*
* @param integer $errNum Error code
* @param string $errStr Error string
* @return void
*/
public function setLastError($errNum, $errStr) {
$this->lastError = array('num' => $errNum, 'str' => $errStr);
Expand Down Expand Up @@ -229,17 +227,8 @@ public function read($length = 1024) {
return false;
}
return $buffer;
} else {
return false;
}
}

/**
* Abort socket operation.
*
* @return boolean Success
*/
public function abort() {
return false;
}

/**
Expand Down Expand Up @@ -272,6 +261,7 @@ function __destruct() {
/**
* Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
*
* @param array $state Array with key and values to reset
* @return boolean True on success
*/
public function reset($state = null) {
Expand Down
68 changes: 68 additions & 0 deletions cake/libs/http/basic_authentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Basic authentication
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.http
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

/**
* Basic authentication
*
* @package cake
* @subpackage cake.cake.libs.http
*/
class BasicAuthentication {

/**
* Authentication
*
* @param HttpSocket $http
* @param array $authInfo
* @return void
* @see http://www.ietf.org/rfc/rfc2617.txt
*/
public static function authentication(HttpSocket $http, &$authInfo) {
if (isset($authInfo['user'], $authInfo['pass'])) {
$http->request['header']['Authorization'] = self::_generateHeader($authInfo['user'], $authInfo['pass']);
}
}

/**
* Proxy Authentication
*
* @param HttpSocket $http
* @param array $proxyInfo
* @return void
* @see http://www.ietf.org/rfc/rfc2617.txt
*/
public static function proxyAuthentication(HttpSocket $http, &$proxyInfo) {
if (isset($proxyInfo['user'], $proxyInfo['pass'])) {
$http->request['header']['Proxy-Authorization'] = self::_generateHeader($proxyInfo['user'], $proxyInfo['pass']);
}
}

/**
* Generate basic [proxy] authentication header
*
* @param string $user
* @param string $pass
* @return string
*/
protected static function _generateHeader($user, $pass) {
return 'Basic ' . base64_encode($user . ':' . $pass);
}

}
106 changes: 106 additions & 0 deletions cake/libs/http/digest_authentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Digest authentication
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.http
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

/**
* Digest authentication
*
* @package cake
* @subpackage cake.cake.libs.http
*/
class DigestAuthentication {

/**
* Authentication
*
* @param HttpSocket $http
* @param array $authInfo
* @return void
* @link http://www.ietf.org/rfc/rfc2617.txt
*/
public static function authentication(HttpSocket $http, &$authInfo) {
if (isset($authInfo['user'], $authInfo['pass'])) {
if (!isset($authInfo['realm']) && !self::_getServerInformation($http, $authInfo)) {
return;
}
$http->request['header']['Authorization'] = self::_generateHeader($http, $authInfo);
}
}

/**
* Retrive information about the authetication
*
* @param HttpSocket $http
* @parma array $authInfo
* @return boolean
*/
protected static function _getServerInformation(HttpSocket $http, &$authInfo) {
$originalRequest = $http->request;
$http->configAuth(false);
$http->request($http->request);
$http->request = $originalRequest;
$http->configAuth('Digest', $authInfo);

if (empty($http->response['header']['WWW-Authenticate'])) {
return false;
}
preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $http->response['header']['WWW-Authenticate'], $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$authInfo[$match[1]] = $match[2];
}
if (!empty($authInfo['qop']) && empty($authInfo['nc'])) {
$authInfo['nc'] = 1;
}
return true;
}

/**
* Generate the header Authorization
*
* @param HttpSocket $http
* @param array $authInfo
* @return string
*/
protected static function _generateHeader(HttpSocket $http, &$authInfo) {
$a1 = md5($authInfo['user'] . ':' . $authInfo['realm'] . ':' . $authInfo['pass']);
$a2 = md5($http->request['method'] . ':' . $http->request['uri']['path']);

if (empty($authInfo['qop'])) {
$response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $a2);
} else {
$authInfo['cnonce'] = uniqid();
$nc = sprintf('%08x', $authInfo['nc']++);
$response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $nc . ':' . $authInfo['cnonce'] . ':auth:' . $a2);
}

$authHeader = 'Digest ';
$authHeader .= 'username="' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $authInfo['user']) . '", ';
$authHeader .= 'realm="' . $authInfo['realm'] . '", ';
$authHeader .= 'nonce="' . $authInfo['nonce'] . '", ';
$authHeader .= 'uri="' . $http->request['uri']['path'] . '", ';
$authHeader .= 'response="' . $response . '"';
if (!empty($authInfo['opaque'])) {
$authHeader .= ', opaque="' . $authInfo['opaque'] . '"';
}
if (!empty($authInfo['qop'])) {
$authHeader .= ', qop="auth", nc=' . $nc . ', cnonce="' . $authInfo['cnonce'] . '"';
}
return $authHeader;
}
}
Loading

0 comments on commit c0e2f63

Please sign in to comment.