Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/http_build_url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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']),
'/'
Expand All @@ -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);

Expand All @@ -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'];
}

Expand All @@ -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'])) {
Expand All @@ -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'];
}

Expand All @@ -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'];
}

Expand Down