Skip to content
Permalink
Browse files

Start using PSR7 interfaces more internally.

  • Loading branch information...
markstory committed May 17, 2016
1 parent c03a217 commit 38d3cc5d7096d6ec76153d219366f2b40cdeb17f
Showing with 37 additions and 30 deletions.
  1. +6 −8 src/Http/Client.php
  2. +3 −3 src/Http/Client/Auth/Digest.php
  3. +10 −14 src/Http/Client/Auth/Oauth.php
  4. +18 −5 src/Http/Client/Request.php
@@ -421,17 +421,15 @@ public function buildUrl($url, $query = [], $options = [])
*/
protected function _createRequest($method, $url, $data, $options)
{
$request = new Request($url, $method, $data);
$headers = isset($options['headers']) ? (array)$options['headers'] : [];
if (isset($options['type'])) {
$request->header($this->_typeHeaders($options['type']));
}
if (isset($options['headers'])) {
$request->header($options['headers']);
$headers = array_merge($headers, $this->_typeHeaders($options['type']));
}
if (is_string($data) && !$request->header('content-type')) {
$request->header('Content-Type', 'application/x-www-form-urlencoded');
if (is_string($data) && !isset($headers['Content-Type']) && !isset($headers['content-type'])) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
$request = new Request($url, $method, $headers, $data);
$request->cookie($this->_cookies->get($url));
if (isset($options['cookies'])) {
$request->cookie($options['cookies']);
@@ -85,12 +85,12 @@ protected function _getServerInfo(Request $request, $credentials)
['auth' => []]
);
if (!$response->header('WWW-Authenticate')) {
if (!$response->getHeader('WWW-Authenticate')) {
return [];
}
preg_match_all(
'@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@',
$response->header('WWW-Authenticate'),
$response->getHeaderLine('WWW-Authenticate'),
$matches,
PREG_SET_ORDER
);
@@ -112,7 +112,7 @@ protected function _getServerInfo(Request $request, $credentials)
*/
protected function _generateHeader(Request $request, $credentials)
{
$path = parse_url($request->url(), PHP_URL_PATH);
$path = $request->getUri()->getPath();
$a1 = md5($credentials['username'] . ':' . $credentials['realm'] . ':' . $credentials['password']);
$a2 = md5($request->method() . ':' . $path);
@@ -150,8 +150,8 @@ protected function _hmacSha1($request, $credentials)
public function baseString($request, $oauthValues)
{
$parts = [
$request->method(),
$this->_normalizedUrl($request->url()),
$request->getMethod(),
$this->_normalizedUrl($request->getUri()),
$this->_normalizedParams($request, $oauthValues),
];
$parts = array_map([$this, '_encode'], $parts);
@@ -163,27 +163,23 @@ public function baseString($request, $oauthValues)
*
* Section 9.1.2. of the Oauth spec
*
* @param string $url URL
* @param Psr\Http\Message\UriInterface $url URL
* @return string Normalized URL
* @throws \Cake\Core\Exception\Exception On invalid URLs
*/
protected function _normalizedUrl($url)
protected function _normalizedUrl($uri)
{
$parts = parse_url($url);
if (!$parts) {
throw new Exception('Unable to parse URL');
}
$scheme = strtolower($parts['scheme'] ?: 'http');
$scheme = $uri->getScheme();
$defaultPorts = [
'http' => 80,
'https' => 443
];
if (isset($parts['port']) && $parts['port'] != $defaultPorts[$scheme]) {
$parts['host'] .= ':' . $parts['port'];
$port = $uri->getPort();
if ($port && $port != $defaultPorts[$scheme]) {
$parts['host'] .= ':' . $port;
}
$out = $scheme . '://';
$out .= strtolower($parts['host']);
$out .= $parts['path'];
$out .= strtolower($uri->getHost());
$out .= $uri->getPath();
return $out;
}
@@ -37,18 +37,20 @@ class Request extends Message implements RequestInterface
*
* @param string $url The request URL
* @param string $method The HTTP method to use.
* @param array $headers The HTTP headers to set.
* @param array|string $body The request body to use.
*/
public function __construct($url = '', $method = self::METHOD_GET, $data = null)
public function __construct($url = '', $method = self::METHOD_GET, array $headers = [], $data = null)
{
$this->validateMethod($method);
$this->method = $method;
$this->uri = $this->createUri($url);
$this->body($data);
$this->header([
$headers += [
'Connection' => 'close',
'User-Agent' => 'CakePHP'
]);
];
$this->addHeaders($headers);
}
/**
@@ -139,12 +141,23 @@ public function header($name = null, $value = null)
if ($value !== null && !is_array($name)) {
$name = [$name => $value];
}
foreach ($name as $key => $val) {
$this->addHeaders($name);
return $this;
}
/**
* Add an array of headers to the request.
*
* @param array $headers The headers to add.
* @return void
*/
protected function addHeaders($headers)
{
foreach ($headers as $key => $val) {
$normalized = strtolower($key);
$this->headers[$key] = (array)$val;
$this->headerNames[$normalized] = $key;
}
return $this;
}
/**

0 comments on commit 38d3cc5

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