diff options
author | Christian A. Weber | 2016-06-06 19:03:33 +0200 |
---|---|---|
committer | Christian A. Weber | 2016-06-06 19:03:33 +0200 |
commit | 6c59336f4a8adb5cc31934b04409ed0d54b5fbb4 (patch) | |
tree | 10c1e165bf68b6119a89527b8f3ce6b083b5e76a | |
parent | 53e62e554f8e12e8268de7aae30e30c67def0a86 (diff) | |
download | itools-6c59336f4a8adb5cc31934b04409ed0d54b5fbb4.tar.gz itools-6c59336f4a8adb5cc31934b04409ed0d54b5fbb4.tar.bz2 itools-6c59336f4a8adb5cc31934b04409ed0d54b5fbb4.zip |
add 2nd parameter to it_url::absolute() to force a protocol
-rw-r--r-- | it_url.class | 27 | ||||
-rwxr-xr-x | tests/it_url.t | 32 |
2 files changed, 42 insertions, 17 deletions
diff --git a/it_url.class b/it_url.class index 52a72d0..dffd805 100644 --- a/it_url.class +++ b/it_url.class @@ -708,27 +708,32 @@ function _atomicwrite($path, $data) } /** - * Make an URL absolute by using host an protocol from current Apache request (but not port number) + * Make an URL absolute by using host and protocol from current Apache request (but not port number) * @param $url Optional URL ( foo.html, /foo.html, //host/bar.html, http://host/bar.html ), default self + * @param $proto_force Optional protocol to enforce, default protocol of current request or http if in script context * @return absolute version of URL ( http[s]://host/bar.html ) */ -static function absolute($url=null) +static function absolute($url = null, $proto_force = null) { if (!isset($url)) $url = $_SERVER['PHP_SELF']; - if (!preg_match('/^\w+:/', $url)) + if (list($proto_url, $urltmp) = it::match('^(\w+):(.*)$', $url)) { - if (!preg_match('#^//#', $url)) - { - $dir = preg_replace('#/[^/]*$#', '/', $_SERVER['PHP_SELF']); - $url = preg_match('#^/#', $url) ? $url : "$dir$url"; - $url = "//" . $_SERVER['HTTP_HOST'] . $url; - } - $url = "http" . (isset($_SERVER['HTTPS']) ? 's':'') . ":$url"; + $url = $urltmp; + $proto = $proto_force ?: $proto_url; + } + else + $proto = $proto_force ?: (isset($_SERVER['HTTPS']) ? 'https' : 'http'); + + if (!preg_match('#^//#', $url)) + { + $dir = preg_replace('#/[^/]*$#', '/', $_SERVER['PHP_SELF']); + $url = preg_match('#^/#', $url) ? $url : "$dir$url"; + $url = "//" . $_SERVER['HTTP_HOST'] . $url; } - return $url; + return "$proto:$url"; } /** diff --git a/tests/it_url.t b/tests/it_url.t index b6569b1..00052d6 100755 --- a/tests/it_url.t +++ b/tests/it_url.t @@ -71,30 +71,50 @@ is( 'punycode $url->realhostname' ); -$_SERVER['HTTP_HOST'] = "gna.ch"; +# it_url::absolute() tests +list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null]; is( it_url::absolute("/"), 'http://gna.ch/', 'it_url::absolute basic' ); -$_SERVER['SERVER_PORT'] = 42; -$_SERVER['HTTP_HOST'] = "gna.ch:42"; +list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch:42", 42, null]; is( it_url::absolute("/port"), 'http://gna.ch:42/port', 'it_url::absolute with non-standard port' ); -$_SERVER['HTTPS'] = true; -$_SERVER['HTTP_HOST'] = "gna.ch"; -$_SERVER['SERVER_PORT'] = 443; +list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", 443, true]; is( it_url::absolute("/https"), 'https://gna.ch/https', 'it_url::absolute for https' ); +list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null]; +is( + it_url::absolute("/https", "https"), + 'https://gna.ch/https', + 'it_url::absolute force https in http context' +); + +list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", 443, true]; +is( + it_url::absolute("/foo", "http"), + 'http://gna.ch/foo', + 'it_url::absolute force http in https context' +); + +list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null]; +is( + it_url::absolute("http://gna.ch/foo", "https"), + 'https://gna.ch/foo', + 'it_url::absolute force https overwriting existing url' +); + + $url = new it_url('http://www.gna.ch/'); $page = $url->get(); ok( |