/** as per https://github.com/indieweb/php-mf2/issues/35 */ public function testResolvesProtocolRelativeUrlsCorrectly() { $expected = 'http://cdn.example.org/thing/asset.css'; $actual = Mf2\resolveUrl('http://example.com', '//cdn.example.org/thing/asset.css'); $this->assertEquals($expected, $actual); $expected = 'https://cdn.example.org/thing/asset.css'; $actual = Mf2\resolveUrl('https://example.com', '//cdn.example.org/thing/asset.css'); $this->assertEquals($expected, $actual); }
/** * rel-me Document URL * * Given a URL, resolves any redirects and returns the resolved URL, whether or not * the redirect chain is secure (doesn’t change protocol) and the redirect chain, * for inspection. * * Example usage: * * list($profileUrl, $isSecure, $redirectChain) = IndieWeb\relMeDocumentUrl($me); * * $followOneRedirect defaults to IndieWeb\followOneRedirect but can be replaced for * testing purposes. * * Returns [string URL, bool isSecure, array redirectChain] * @return array */ function relMeDocumentUrl($url, $followOneRedirect = null) { if (!is_callable($followOneRedirect)) { $followOneRedirect = __NAMESPACE__ . '\\followOneRedirect'; } $previous = array(); $secure = true; $is_https = false; $currentUrl = $url; while (true) { // TODO: is resolving this URL correct behaviour here? // should it be resolved just to the host? $redirectedUrl = Mf2\resolveUrl($currentUrl, $followOneRedirect($currentUrl)); if ($redirectedUrl === null) { break; } elseif (in_array($redirectedUrl, $previous)) { break; } elseif ($is_https && parse_url($currentUrl, PHP_URL_SCHEME) !== parse_url($redirectedUrl, PHP_URL_SCHEME)) { $secure = false; $previous[] = $currentUrl = $redirectedUrl; break; } else { if (parse_url($currentUrl, PHP_URL_SCHEME) == 'https') { $is_https = true; } $currentUrl = $redirectedUrl; $previous[] = $currentUrl; } } return array($currentUrl, $secure, $previous); }