Permalink
Browse files

Item14544: Improve header code and add unit test

Doesn't make sense to split every host / forwarded-host header unless
it's actually a forwarded-host.  Also add a unit test.
  • Loading branch information...
gac410 committed Dec 10, 2017
1 parent b8511bb commit b248f35bea3c08fa42f0c4b8f683606c53362ded
Showing with 44 additions and 7 deletions.
  1. +35 −0 UnitTestContrib/test/unit/RequestTests.pm
  2. +9 −7 core/lib/Foswiki/Request.pm
@@ -236,6 +236,41 @@ sub test_queryString {
);
}
sub test_forwarded_for {
my $this = shift;
my $req = new Foswiki::Request("");
$req->secure('1');
$req->header( Host => 'myhost.com' );
$req->header( 'X-Forwarded-Host' => 'hop1.com, hop2.com' );
$req->action('view');
$req->path_info('/Main/WebHome');
my $base = 'https://hop1.com';
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host header'
);
print STDERR $req->url() . "\n";
$req->header( 'X-Forwarded-Host' => 'onehop.com:8080' );
$base = 'https://onehop.com:8080';
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host multiple header'
);
$base = 'http://your.domain.com';
$Foswiki::cfg{ForceDefaultUrlHost} = 1;
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host single header + forceDefaultUrlHost'
);
print STDERR $req->url() . "\n";
}
sub perform_url_test {
my $this = shift;
my $req = new Foswiki::Request("");
@@ -298,14 +298,16 @@ sub url {
$url = $Foswiki::cfg{DefaultUrlHost};
}
else {
my $vh =
$this->header('X-Forwarded-Host')
|| $this->header('Host')
|| '';
$vh = ( split /[, ]+/, $vh )[0];
my $host;
if ( $this->header('X-Forwarded-Host') ) {
$host = ( split /[, ]+/, $this->header('X-Forwarded-Host') )[0];
}
else {
$host = $this->header('Host');
}
$url =
$vh
? $this->protocol . '://' . $vh
$host
? $this->protocol . '://' . $host
: $Foswiki::cfg{DefaultUrlHost};
}
return $url if $base;

0 comments on commit b248f35

Please sign in to comment.