Browse Source

Support protocol relative URL in HTTP Client.

Some servers set //example.com as a Location header. `buildUrl()` needs
to support that in order to build absolute urls.
Robert Pustułka 8 years ago
parent
commit
1feffe862d
2 changed files with 17 additions and 3 deletions
  1. 8 3
      src/Http/Client.php
  2. 9 0
      tests/TestCase/Http/ClientTest.php

+ 8 - 3
src/Http/Client.php

@@ -448,15 +448,20 @@ class Client
             $url .= $q;
             $url .= $q;
             $url .= is_string($query) ? $query : http_build_query($query);
             $url .= is_string($query) ? $query : http_build_query($query);
         }
         }
-        if (preg_match('#^https?://#', $url)) {
-            return $url;
-        }
         $defaults = [
         $defaults = [
             'host' => null,
             'host' => null,
             'port' => null,
             'port' => null,
             'scheme' => 'http',
             'scheme' => 'http',
         ];
         ];
         $options += $defaults;
         $options += $defaults;
+
+        if (preg_match('#^//#', $url)) {
+            $url = $options['scheme'] . ':' . $url;
+        }
+        if (preg_match('#^https?://#', $url)) {
+            return $url;
+        }
+
         $defaultPorts = [
         $defaultPorts = [
             'http' => 80,
             'http' => 80,
             'https' => 443
             'https' => 443

+ 9 - 0
tests/TestCase/Http/ClientTest.php

@@ -137,6 +137,15 @@ class ClientTest extends TestCase
                 [],
                 [],
                 'query string data with some already on the url.'
                 'query string data with some already on the url.'
             ],
             ],
+            [
+                'http://example.com/test.html',
+                '//example.com/test.html',
+                [],
+                [
+                    'scheme' => 'http'
+                ],
+                'url without a scheme',
+            ],
         ];
         ];
     }
     }