Skip to content

Commit

Permalink
Support protocol relative URL in HTTP Client.
Browse files Browse the repository at this point in the history
Some servers set //example.com as a Location header. `buildUrl()` needs
to support that in order to build absolute urls.
  • Loading branch information
robertpustulka committed Sep 7, 2017
1 parent 6d25953 commit 1feffe8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Http/Client.php
Expand Up @@ -448,15 +448,20 @@ public function buildUrl($url, $query = [], $options = [])
$url .= $q;
$url .= is_string($query) ? $query : http_build_query($query);
}
if (preg_match('#^https?://#', $url)) {
return $url;
}
$defaults = [
'host' => null,
'port' => null,
'scheme' => 'http',
];
$options += $defaults;

if (preg_match('#^//#', $url)) {
$url = $options['scheme'] . ':' . $url;
}
if (preg_match('#^https?://#', $url)) {
return $url;
}

$defaultPorts = [
'http' => 80,
'https' => 443
Expand Down
9 changes: 9 additions & 0 deletions tests/TestCase/Http/ClientTest.php
Expand Up @@ -137,6 +137,15 @@ public static function urlProvider()
[],
'query string data with some already on the url.'
],
[
'http://example.com/test.html',
'//example.com/test.html',
[],
[
'scheme' => 'http'
],
'url without a scheme',
],
];
}

Expand Down

0 comments on commit 1feffe8

Please sign in to comment.