Skip to content

Commit

Permalink
Start using PSR7 interfaces more internally.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 17, 2016
1 parent c03a217 commit 38d3cc5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
14 changes: 6 additions & 8 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Client/Auth/Digest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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);

Expand Down
24 changes: 10 additions & 14 deletions src/Http/Client/Auth/Oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
23 changes: 18 additions & 5 deletions src/Http/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 38d3cc5

Please sign in to comment.