From cf7160264efcf7d1f2fcb99c5ae1701ca805100c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Koles=C3=A1r=20Andr=C3=A1s?= Date: Mon, 30 Nov 2015 16:12:43 +0100 Subject: [PATCH] handle relative urls and use empty --- src/http_build_url.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/http_build_url.php b/src/http_build_url.php index 95ed0fb..f44f79e 100644 --- a/src/http_build_url.php +++ b/src/http_build_url.php @@ -76,7 +76,7 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new $flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS; } - // Schema and host are alwasy replaced + // Schema and host are always replaced foreach (array('scheme', 'host') as $part) { if (isset($parts[$part])) { $url[$part] = $parts[$part]; @@ -90,8 +90,8 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new } } } else { - if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH)) { - if (isset($url['path']) && substr($parts['path'], 0, 1) !== '/') { + if (!empty($parts['path']) && ($flags & HTTP_URL_JOIN_PATH)) { + if (!empty($url['path']) && substr($parts['path'], 0, 1) !== '/') { $url['path'] = rtrim( str_replace(basename($url['path']), '', $url['path']), '/' @@ -101,8 +101,8 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new } } - if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) { - if (isset($url['query'])) { + if (!empty($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) { + if (!empty($url['query'])) { parse_str($url['query'], $url_query); parse_str($parts['query'], $parts_query); @@ -118,7 +118,9 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new } } - if (isset($url['path']) && substr($url['path'], 0, 1) !== '/') { + if (!empty($url['path']) && + !empty($url['host']) && // only absolute urls need leading slash + substr($url['path'], 0, 1) !== '/') { $url['path'] = '/' . $url['path']; } @@ -131,11 +133,11 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new $parsed_string = ''; - if (isset($url['scheme'])) { + if (!empty($url['scheme'])) { $parsed_string .= $url['scheme'] . '://'; } - if (isset($url['user'])) { + if (!empty($url['user'])) { $parsed_string .= $url['user']; if (isset($url['pass'])) { @@ -145,11 +147,11 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new $parsed_string .= '@'; } - if (isset($url['host'])) { + if (!empty($url['host'])) { $parsed_string .= $url['host']; } - if (isset($url['port'])) { + if (!empty($url['port'])) { $parsed_string .= ':' . $url['port']; } @@ -159,11 +161,11 @@ function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new $parsed_string .= '/'; } - if (isset($url['query'])) { + if (!empty($url['query'])) { $parsed_string .= '?' . $url['query']; } - if (isset($url['fragment'])) { + if (!empty($url['fragment'])) { $parsed_string .= '#' . $url['fragment']; }