Browse Source

Start using PSR7 interfaces more internally.

Mark Story 9 years ago
parent
commit
38d3cc5d70
4 changed files 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

+ 6 - 8
src/Http/Client.php

@@ -421,17 +421,15 @@ class Client
      */
     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']);

+ 3 - 3
src/Http/Client/Auth/Digest.php

@@ -85,12 +85,12 @@ class Digest
             ['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 @@ class Digest
      */
     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);
 

+ 10 - 14
src/Http/Client/Auth/Oauth.php

@@ -150,8 +150,8 @@ class Oauth
     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 @@ class Oauth
      *
      * 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;
     }
 

+ 18 - 5
src/Http/Client/Request.php

@@ -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 @@ class Request extends Message implements RequestInterface
         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;
     }
 
     /**