Commit 5c392912 authored by mikeytown2's avatar mikeytown2

Issue #2205871 by bkildow, mikeytown2: Fix for when ['HTTP_HOST'] has a port number in it.

parent e3a8bdea
......@@ -165,9 +165,23 @@ function httprl_install_http_test($mode, $blocking = FALSE) {
$id = 'httprl_' . hash('sha512', mt_rand() . microtime(TRUE) . serialize($_SERVER));
}
if (!empty($_SERVER['HTTP_HOST'])) {
$hostname = $_SERVER['HTTP_HOST'];
// If HTTP_HOST contains the port, use SERVER_NAME.
if (strpos($_SERVER['HTTP_HOST'], ':') !== FALSE) {
$hostname = $_SERVER['SERVER_NAME'];
}
}
elseif (!empty($_SERVER['SERVER_NAME'])) {
$hostname = $_SERVER['SERVER_NAME'];
}
else {
$hostname = '';
}
// Set the headers to point to this hostname.
$headers = array(
'Host' => $_SERVER['HTTP_HOST'],
'Host' => $hostname,
'Connection' => 'closed',
);
......@@ -316,12 +330,28 @@ function httprl_install_http_test($mode, $blocking = FALSE) {
}
}
else {
// Set httprl_server_hostname to the HTTP_HOST.
if ( $_SERVER['HTTP_HOST'] !== 'default'
&& ip2long($_SERVER['HTTP_HOST']) === FALSE
&& variable_get('httprl_server_hostname', FALSE) != $_SERVER['HTTP_HOST']
if (!empty($_SERVER['HTTP_HOST'])) {
$hostname = $_SERVER['HTTP_HOST'];
// If HTTP_HOST contains the port, use SERVER_NAME.
if (strpos($_SERVER['HTTP_HOST'], ':') !== FALSE) {
$hostname = $_SERVER['SERVER_NAME'];
}
}
elseif (!empty($_SERVER['SERVER_NAME'])) {
$hostname = $_SERVER['SERVER_NAME'];
}
else {
$hostname = '';
}
// Set httprl_server_hostname to the HTTP_HOST or SERVER_NAME.
if ( !empty($hostname)
&& $hostname !== 'default'
&& ip2long($hostname) === FALSE
&& variable_get('httprl_server_hostname', FALSE) != $hostname
) {
variable_set('httprl_server_hostname', $_SERVER['HTTP_HOST']);
variable_set('httprl_server_hostname', $hostname);
}
return TRUE;
......
......@@ -311,16 +311,30 @@ function _httprl_build_drupal_root($level = 0, $hostname_mode = 0) {
// Use static for gethostbyname lookup.
static $dns_cache;
if (!empty($_SERVER['HTTP_HOST'])) {
$hostname = $_SERVER['HTTP_HOST'];
// If HTTP_HOST contains the port, use SERVER_NAME.
if (strpos($_SERVER['HTTP_HOST'], ':') !== FALSE) {
$hostname = $_SERVER['SERVER_NAME'];
}
}
elseif (!empty($_SERVER['SERVER_NAME'])) {
$hostname = $_SERVER['SERVER_NAME'];
}
else {
$hostname = '';
}
// Get Host.
$ip = httprl_variable_get('httprl_server_addr', FALSE);
if ($ip == -1 || $hostname_mode == 1) {
$ip = $_SERVER['HTTP_HOST'];
$ip = $hostname;
// If the host is bad don't use it.
if ((is_callable('drupal_is_cli') && drupal_is_cli()) || (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)))) {
if (!isset($dns_cache[$_SERVER['HTTP_HOST']])) {
$dns_cache[$_SERVER['HTTP_HOST']] = gethostbyname($_SERVER['HTTP_HOST']);
if (!isset($dns_cache[$hostname])) {
$dns_cache[$hostname] = gethostbyname($hostname);
}
if ($dns_cache[$_SERVER['HTTP_HOST']] == $_SERVER['HTTP_HOST']) {
if ($dns_cache[$hostname] == $hostname) {
$ip = '';
}
}
......@@ -335,13 +349,13 @@ function _httprl_build_drupal_root($level = 0, $hostname_mode = 0) {
elseif (preg_match('/^::\d+.\d+.\d+.\d+$/', $ip)) {
$ip = substr($ip, 2);
}
elseif (!empty($_SERVER['HTTP_HOST'])) {
elseif (!empty($hostname)) {
// Last option is to use the IP from the host name.
if (!isset($dns_cache[$_SERVER['HTTP_HOST']])) {
$dns_cache[$_SERVER['HTTP_HOST']] = gethostbyname($_SERVER['HTTP_HOST']);
if (!isset($dns_cache[$hostname])) {
$dns_cache[$hostname] = gethostbyname($hostname);
}
$ip = $dns_cache[$_SERVER['HTTP_HOST']];
if ($dns_cache[$_SERVER['HTTP_HOST']] == $_SERVER['HTTP_HOST']) {
$ip = $dns_cache[$hostname];
if ($dns_cache[$hostname] == $hostname) {
$ip = '';
}
}
......@@ -1837,11 +1851,25 @@ function httprl_parse_data(&$result) {
// If location isn't fully qualified URL (as per W3 RFC2616), build one.
if (empty($location['scheme']) || empty($location['host'])) {
if (!empty($_SERVER['HTTP_HOST'])) {
$hostname = $_SERVER['HTTP_HOST'];
// If HTTP_HOST contains the port, use SERVER_NAME.
if (strpos($_SERVER['HTTP_HOST'], ':') !== FALSE) {
$hostname = $_SERVER['SERVER_NAME'];
}
}
elseif (!empty($_SERVER['SERVER_NAME'])) {
$hostname = $_SERVER['SERVER_NAME'];
}
else {
$hostname = '';
}
// Get the important parts from the original request.
$original_location = @parse_url($result->url);
// Assume request is to self if none of this was setup correctly.
$location['scheme'] = !empty($location['scheme']) ? $location['scheme'] : $original_location['scheme'];
$location['host'] = !empty($location['host']) ? $location['host'] : !empty($original_location['host']) ? $original_location['host'] : $_SERVER['HTTP_HOST'];
$location['host'] = !empty($location['host']) ? $location['host'] : !empty($original_location['host']) ? $original_location['host'] : $hostname;
$location['port'] = !empty($location['port']) ? $location['port'] : !empty($original_location['port']) ? $original_location['port'] : '';
$location = httprl_glue_url($location);
}
......@@ -2248,8 +2276,24 @@ function httprl_queue_background_callback(&$args, &$result = NULL) {
}
// Set Host header.
if (empty($options['headers']['Host']) && !empty($_SERVER['HTTP_HOST'])) {
$options['headers']['Host'] = $_SERVER['HTTP_HOST'];
if (empty($options['headers']['Host'])) {
if (!empty($_SERVER['HTTP_HOST'])) {
$hostname = $_SERVER['HTTP_HOST'];
// If HTTP_HOST contains the port, use SERVER_NAME.
if (strpos($_SERVER['HTTP_HOST'], ':') !== FALSE) {
$hostname = $_SERVER['SERVER_NAME'];
}
}
elseif (!empty($_SERVER['SERVER_NAME'])) {
$hostname = $_SERVER['SERVER_NAME'];
}
else {
$hostname = '';
}
if (!empty($hostname)) {
$options['headers']['Host'] = $hostname;
}
}
// Set Session header if requested to.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment