diff options
| author | Christian Schneider | 2023-08-22 16:12:24 +0200 | 
|---|---|---|
| committer | Christian Schneider | 2023-08-22 16:12:24 +0200 | 
| commit | e70b53d3ef0991f3a09bc7fcaf070f27f2e4b83a (patch) | |
| tree | 08378e881aa0de9455d5b5c9c5a737bbd812b258 | |
| parent | 599fe205600c4d0634476a042c4b0c24d683ccb5 (diff) | |
| download | itools-e70b53d3ef0991f3a09bc7fcaf070f27f2e4b83a.tar.gz itools-e70b53d3ef0991f3a09bc7fcaf070f27f2e4b83a.tar.bz2 itools-e70b53d3ef0991f3a09bc7fcaf070f27f2e4b83a.zip  | |
Add prefix to it_url::absolute() for OneDomain support, add tests for relative paths
| -rw-r--r-- | it_url.class | 10 | ||||
| -rwxr-xr-x | test/it_url.t | 15 | 
2 files changed, 20 insertions, 5 deletions
diff --git a/it_url.class b/it_url.class index 1b2bfc7..05689c8 100644 --- a/it_url.class +++ b/it_url.class @@ -186,7 +186,7 @@ static function _default_headers($url, $p)  		'Host' => $url->realhostname . $url->explicitport,  		'User-Agent' => "Mozilla/5.0 (compatible; ITools; Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582)",  		'Accept-Language' => $p['headers']['Accept-Language'] ?? ($search_subrequest ? T_defaultlang() : T_lang()), # can prevent loading of it_text -		'Referer' => it::match('([-\w]+\.\w+)$', $url->hostname) == it::match('([-\w]+\.\w+)$', $_SERVER['HTTP_HOST']) ? it_url::absolute(U($_GET)) : null, +		'Referer' => it::match('([-\w]+\.\w+)$', $url->hostname) == it::match('([-\w]+\.\w+)$', $_SERVER['HTTP_HOST']) ? static::absolute(U($_GET)) : null,  		'X-Ultra-Https' => $_SERVER['HTTPS'],  	]); @@ -728,7 +728,7 @@ static function get_cache_contents($p)  			$result = self::_postprocess($result, $p);  	}  	else -		$result = it::error((array)$p['it_error'] + ['title' => $p['safety'] === 0 ? false : "failed getting " . it_url::absolute($p['url']), 'body' => $p]); +		$result = it::error((array)$p['it_error'] + ['title' => $p['safety'] === 0 ? false : "failed getting " . static::absolute($p['url']), 'body' => $p]);  	return $result;  } @@ -845,10 +845,10 @@ static function _atomicwrite($path, $data)   * @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, $proto_force = null) +static function absolute($url = null, $proto_force = null, $prefix = '')  {  	if (!isset($url)) -		$url = $_SERVER['PHP_SELF']; +		$url = $prefix . $_SERVER['PHP_SELF'];  	if (list($proto_url, $urltmp) = it::match('^(\w+):(.*)$', $url))  	{ @@ -860,7 +860,7 @@ static function absolute($url = null, $proto_force = null)  	if (!preg_match('#^//#', $url))  	{ -		$dir = preg_replace('#/[^/]*$#', '/', $_SERVER['PHP_SELF']); +		$dir = preg_replace('#/[^/]*$#', '/', $prefix . $_SERVER['PHP_SELF']);  		$url = preg_match('#^/#', $url) ? $url : "$dir$url";  		$url = "//" . $_SERVER['HTTP_HOST'] . $url;  	} diff --git a/test/it_url.t b/test/it_url.t index d2bb653..c147acc 100755 --- a/test/it_url.t +++ b/test/it_url.t @@ -74,6 +74,8 @@ is(  );  # it_url::absolute() tests +$php_self = $_SERVER['PHP_SELF']; +$_SERVER['PHP_SELF'] = '/foo/bar/self.php';  list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null];  is(  	it_url::absolute("/"), @@ -81,6 +83,18 @@ is(  	'it_url::absolute basic'  ); +is( +	it_url::absolute("qux"), +	'http://gna.ch/foo/bar/qux', +	'it_url::absolute relative path' +); + +is( +	it_url::absolute("qux", 'https', '/prefix'), +	'https://gna.ch/prefix/foo/bar/qux', +	'it_url::absolute relative path' +); +  list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch:42", 42, null];  is(  	it_url::absolute("/port"), @@ -115,6 +129,7 @@ is(  	'https://gna.ch/foo',  	'it_url::absolute force https overwriting existing url'  ); +$_SERVER['PHP_SELF'] = $php_self;  $url = new it_url('http://www.gna.ch/');  |