Skip to content

Commit

Permalink
Item14380: Unit test failing - needed some adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
gac410 committed Nov 30, 2017
1 parent eee54fa commit 707f775
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
9 changes: 4 additions & 5 deletions UnitTestContrib/test/unit/BootstrapTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ sub test_DefaultHostUrl {
( $msg, $boot_cfg ) = $this->_runBootstrap(1);

$this->assert_matches(
qr{AUTOCONFIG: Set DefaultUrlHost http://foobar.com from HTTP_HOST foobar.com},
qr{AUTOCONFIG: Set \(http://foobar.com\) from detected},

$msg
);
$this->assert_str_equals( $boot_cfg->{DefaultUrlHost},
Expand All @@ -398,8 +399,7 @@ qr{AUTOCONFIG: Set DefaultUrlHost http://foobar.com from HTTP_HOST foobar.com},
( $msg, $boot_cfg ) = $this->_runBootstrap(1);

$this->assert_matches(
qr{AUTOCONFIG: Set DefaultUrlHost http://foobar.com from SERVER_NAME},
$msg );
qr{AUTOCONFIG: Set \(http://foobar.com\) from detected}, $msg );
$this->assert_str_equals( $boot_cfg->{DefaultUrlHost},
'http://foobar.com' );

Expand All @@ -414,8 +414,7 @@ qr{AUTOCONFIG: Set DefaultUrlHost http://foobar.com from HTTP_HOST foobar.com},
( $msg, $boot_cfg ) = $this->_runBootstrap(1);

$this->assert_matches(
qr{AUTOCONFIG: Set DefaultUrlHost https://foobar.com from SCRIPT_URI},
$msg );
qr{AUTOCONFIG: Set \(https://foobar.com\) from detected}, $msg );
$this->assert_str_equals( $boot_cfg->{DefaultUrlHost},
'https://foobar.com' );

Expand Down
3 changes: 3 additions & 0 deletions core/lib/Foswiki/Configure/Bootstrap.pm
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ PROXY

$Foswiki::cfg{DefaultUrlHost} = "$protocol://" . $host . $port;

print STDERR
"AUTOCONFIG: Set ($Foswiki::cfg{DefaultUrlHost}) from detected\n";

# Examine the CGI path. The 'view' script it typically removed from the
# URL when using "Short URLs. If this BEGIN block is being run by
# 'view', then $Foswiki::cfg{ScriptUrlPaths}{view} will be correctly
Expand Down
25 changes: 21 additions & 4 deletions core/lib/Foswiki/Engine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,27 @@ sub _getConnectionData {
my ( $client, $proto, $host, $port, $proxy );

# These are the defaults populated in a conventional server, no proxy
$client = $ENV{REMOTE_ADDR};
$proto = ( $ENV{HTTPS} && uc( $ENV{HTTPS} ) eq 'ON' ) ? 'https' : 'http';
$host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
$port = $ENV{SERVER_PORT};
$client = $ENV{REMOTE_ADDR} || '';
$proto =
( $ENV{HTTPS} && ( uc( $ENV{HTTPS} ) eq 'ON' || $ENV{HTTPS} eq '1' ) )
? 'https'
: 'http';
$host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
unless ($host) {
if ( defined $ENV{SCRIPT_URI}
&& $ENV{SCRIPT_URI} =~ m#^(https?)://([^/]+)#i )
{
$proto = $1;
$host = $2;
}
}

#SMELL: Give up - no obvious hostname in the request.
unless ($host) {
$host = 'localhost';
}
$port = $ENV{SERVER_PORT} || 80;
$proxy = '';

if ( $detectProxy
|| $Foswiki::cfg{PROXY}{UseForwardedFor}
Expand Down
3 changes: 3 additions & 0 deletions core/lib/Foswiki/Engine/CGI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ sub prepareConnection {
Foswiki::Engine::_getConnectionData();

$req->remoteAddress($client);
$req->serverPort($port);
$req->header( -name => 'Host', -value => $host );
$req->method( $ENV{REQUEST_METHOD} );
if ( $protocol eq 'https' ) {
$req->secure(1);
Expand All @@ -127,6 +129,7 @@ sub prepareHeaders {
foreach my $header ( keys %ENV ) {
next unless $header =~ m/^(?:HTTP|CONTENT|COOKIE)/i;
( my $field = $header ) =~ s/^HTTPS?_//;
next if ( $field eq 'HOST' ); # HOST set in prepareConnection
$req->header( $field => $ENV{$header} );
}
$req->remoteUser( $ENV{REMOTE_USER} );
Expand Down
11 changes: 6 additions & 5 deletions core/lib/Foswiki/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,17 @@ sub url {
}
$name =~ s(//+)(/)g;
if ($full) {

if ( $Foswiki::cfg{ForceDefaultUrlHost} ) {
$url = $Foswiki::cfg{DefaultUrlHost};
}
else {
my ( $client, $protocol, $host, $port ) =
Foswiki::Engine::_getConnectionData();
my $host = $this->header('Host');
my $port = $this->serverPort();
$port = ( $port && $port != 80 && $port != 443 ) ? ":$port" : '';

$url = $protocol . '://' . $host . $port;
$url =
$host
? $this->protocol . '://' . $host . $port
: $Foswiki::cfg{DefaultUrlHost};
}
return $url if $base;
$url .= $name;
Expand Down

0 comments on commit 707f775

Please sign in to comment.