Skip to content

Commit

Permalink
Item13023: Bootstrap fixes
Browse files Browse the repository at this point in the history
It no longer depends on the $ENV{SCRIPT_NAME}, which didn't work on
lighttpd.
  • Loading branch information
gac410 committed Sep 26, 2014
1 parent 49fcf06 commit 0a1780d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 76 deletions.
81 changes: 28 additions & 53 deletions lib/Foswiki/Plugins/TestBootstrapPlugin.pm
Expand Up @@ -185,6 +185,8 @@ sub _bootstrapConfig {
if ( TRAUTO && $Foswiki::cfg{ScriptSuffix} );

my $protocol = $ENV{HTTPS} ? 'https' : 'http';

# Figure out the DefaultUrlHost
if ( $ENV{HTTP_HOST} ) {
$Foswiki::cfg{DefaultUrlHost} = "$protocol://$ENV{HTTP_HOST}";
print STDERR
Expand Down Expand Up @@ -216,67 +218,40 @@ sub _bootstrapConfig {
# and then recovers it. When the jsonrpc script is called to save the configuration
# it then has the VIEWPATH parameter available. If "view" was never called during
# configuration, then it will not be set correctly.
print STDERR "AUTOCONFIG: REQUEST_URI is $ENV{REQUEST_URI} \n";
print STDERR "AUTOCONFIG: REQUEST_URI is $ENV{REQUEST_URI} \n" if (TRAUTO);
print STDERR "AUTOCONFIG: PATH_INFO is $ENV{PATH_INFO} \n" if (TRAUTO);

if ( $ENV{SCRIPT_NAME} ) {
print STDERR "AUTOCONFIG: Found SCRIPT $ENV{SCRIPT_NAME} \n"
if (TRAUTO);
# Determine the prefix of the script part of the URI.
my $pfx = '';
if ( my $idx = index( $ENV{REQUEST_URI}, $ENV{PATH_INFO} ) ) {
$pfx = substr( $ENV{REQUEST_URI}, 0, $idx );
}
print STDERR "AUTOCONFIG: URI Prefix is $pfx\n";

if ( $ENV{SCRIPT_NAME} =~ m{^(.*?)/$script(\b|$)} ) {
# We don't use the SCRIPT_NAME, but report it anyway for debugging
print STDERR "AUTOCONFIG: Found SCRIPT $ENV{SCRIPT_NAME} \n"
if ( TRAUTO && $ENV{SCRIPT_NAME} );

if ( index $ENV{REQUEST_URI}, $ENV{SCRIPT_NAME} eq 0 ) {
print STDERR
" : SCRIPT_NAME $ENV{SCRIPT_NAME} fully contained in REQUEST_URI $ENV{REQUEST_URI}\n";
# Work out the URL path for Short and standard URLs
if ( $ENV{REQUEST_URI} =~ m{^(.*?)/$script(\b|$)} ) {
print STDERR
"AUTOCONFIG: SCRIPT $script fully contained in REQUEST_URI $ENV{REQUEST_URI}, Not short URLs\n";

# Conventional URLs with path and script
$Foswiki::cfg{ScriptUrlPath} = $1;
$Foswiki::cfg{ScriptUrlPaths}{view} =
$1 . '/view' . $Foswiki::cfg{ScriptSuffix};
# Conventional URLs with path and script
$Foswiki::cfg{ScriptUrlPath} = $1;
$Foswiki::cfg{ScriptUrlPaths}{view} =
$1 . '/view' . $Foswiki::cfg{ScriptSuffix};

# This might not work, depending on the websrver config,
# but it's the best we can do
$Foswiki::cfg{PubUrlPath} = "$1/../pub";
}
else {
my @parts = split( '/', $1 );
my $path = '';
foreach my $part (@parts) {
next unless $part;
print STDERR
"Testing $ENV{REQUEST_URI} for $path/$part index: "
. index( $ENV{REQUEST_URI}, "$path/$part" ) . "\n";
if ( index( $ENV{REQUEST_URI}, "$path/$part" ) eq 0 ) {
print STDERR "Found match ($part)\n";
$path .= "/$part";
}
else {
last;
}
}
print STDERR
"AUTOCONFIG: Found path, partial script match. short URLs \n"
if (TRAUTO);
$Foswiki::cfg{ScriptUrlPath} = $path . '/bin';
$Foswiki::cfg{ScriptUrlPaths}{view} = $path;
$Foswiki::cfg{PubUrlPath} = $path . '/pub';
}
}
else {
# Short URLs but with a path
print STDERR "AUTOCONFIG: Found path, but no script. short URLs \n"
if (TRAUTO);
$Foswiki::cfg{ScriptUrlPath} = $ENV{SCRIPT_NAME} . '/bin';
$Foswiki::cfg{ScriptUrlPaths}{view} = $ENV{SCRIPT_NAME};
$Foswiki::cfg{PubUrlPath} = $ENV{SCRIPT_NAME} . '/pub';
}
# This might not work, depending on the websrver config,
# but it's the best we can do
$Foswiki::cfg{PubUrlPath} = "$1/../pub";
}
else {
# No script, no path, shortest URLs
print STDERR "AUTOCONFIG: No path, No script, probably shorter URLs \n"
print STDERR "AUTOCONFIG: Building Short URL paths using prefix \n"
if (TRAUTO);
$Foswiki::cfg{ScriptUrlPaths}{view} = '';
$Foswiki::cfg{ScriptUrlPath} = '/bin';
$Foswiki::cfg{PubUrlPath} = '/pub';
$Foswiki::cfg{ScriptUrlPath} = $pfx . '/bin';
$Foswiki::cfg{ScriptUrlPaths}{view} = $pfx;
$Foswiki::cfg{PubUrlPath} = $pfx . '/pub';
}

if (TRAUTO) {
Expand Down
72 changes: 49 additions & 23 deletions test/unit/TestBootstrapPlugin/TestBootstrapPluginTests.pm
Expand Up @@ -35,6 +35,7 @@ sub tear_down {
sub fixture_groups {

return (
[ 'Suffix', 'Nosuffix', ],
[ 'FullURLs', 'ShortURLs', 'MinimumURLs' ],
[ 'HTTP', 'HTTPS', ],
[ 'Apache', 'Lighttpd', ],
Expand All @@ -43,12 +44,12 @@ sub fixture_groups {

sub ShortURLs {
my $this = shift;
$this->{url} = "short";
$this->{host} = "mysite.com";
$this->{script} = "/foswiki";
$this->{viewURL} = "/foswiki/Main/WebHome";
$this->{editURL} = "/foswiki/bin/edit/Main/WebHome";
$this->{pubURL} = "/foswiki/pub/System/Somefile.txt";
$this->{url} = "short";
$this->{host} = "mysite.com";
$this->{script} = "/foswiki";
$this->{viewURL} = "/foswiki/Main/WebHome";
$this->{pathinfo} = "/Main/WebHome";
$this->{pubURL} = "/foswiki/pub/System/Somefile.txt";

$this->{PubUrlPath} = '/foswiki/pub';
$this->{ScriptUrlPath} = '/foswiki/bin';
Expand All @@ -63,7 +64,7 @@ sub MinimumURLs {
$this->{host} = "mysite.com";
$this->{viewURL} = "/Main/WebHome";
$this->{script} = "";
$this->{editURL} = "/bin/edit/Main/WebHome";
$this->{pathinfo} = "/Main/WebHome";
$this->{pubURL} = "/pub/System/Somefile.txt";
$this->{PubUrlPath} = '/pub';

Expand Down Expand Up @@ -107,15 +108,31 @@ sub HTTPS {
#$ENV{SERVER_NAME} = foswiki.fenachrone.com
#$ENV{SERVER_PORT} = 80

sub FullURLs {
sub Suffix {
my $this = shift;

$this->{suffix} = '.pl';

return;
}

sub Nosuffix {
my $this = shift;

$this->{suffix} = '';

return;
}

sub FullURLs {
my $this = shift;
$this->{url} = "full";
$this->{host} = "mysite.com";
$this->{script} = "/foswiki/bin/view";
$this->{viewURL} = "/foswiki/bin/view/Main/WebHome";
$this->{editURL} = "/foswiki/bin/edit/Main/WebHome";
$this->{pubURL} = "/foswiki/pub/System/Somefile.txt";

$this->{url} = "full";
$this->{host} = "mysite.com";
$this->{script} = "/foswiki/bin/view" . $this->{suffix};
$this->{viewURL} = "/foswiki/bin/view$this->{suffix}/Main/WebHome";
$this->{pathinfo} = "/Main/WebHome";
$this->{pubURL} = "/foswiki/pub/System/Somefile.txt";

$this->{PubUrlPath} = '/foswiki/bin/../pub';
$this->{ScriptUrlPath} = '/foswiki/bin';
Expand All @@ -134,6 +151,7 @@ sub Apache {
SCRIPT_URI => $this->{protocol} . $this->{host} . $this->{viewURL},
SCRIPT_URL => $this->{viewURL},
SCRIPT_NAME => $this->{script},
PATH_INFO => $this->{pathinfo},
};
return;
}
Expand All @@ -147,7 +165,8 @@ sub Lighttpd {
REQUEST_URI => $this->{viewURL},
SCRIPT_URI => $this->{protocol} . $this->{host} . $this->{viewURL},
SCRIPT_URL => $this->{viewURL},
SCRIPT_NAME => 'bin/view', # Script name is always present in Lighttpd
SCRIPT_NAME => '/bin/view', # Script name is always present in Lighttpd
PATH_INFO => $this->{pathinfo},
};

return;
Expand All @@ -167,21 +186,23 @@ sub verify_Test_Bootstrap {
my $log;
open STDERR, '>', \$log;

( $boot_cfg, $resp ) = _runBootstrap(0);
( $boot_cfg, $resp ) = $this->_runBootstrap(0);
close STDERR;
$msg .= $resp . "\n\n";
$msg .= $log;
}

#print STDERR "BOOTSTRAP RETURNS:\n $msg\n";

#print STDERR Data::Dumper::Dumper( \$boot_cfg );

$this->assert_str_equals( $this->{PubUrlPath}, $boot_cfg->{PubUrlPath} );
$this->assert_str_equals( $this->{ScriptUrlPath},
$boot_cfg->{ScriptUrlPath} );
$this->assert_str_equals( $this->{ViewScriptUrlPath},
$boot_cfg->{ScriptUrlPaths}{view} );
my $suffix = ( $this->{url} eq 'full' ) ? $this->{suffix} : '';
$this->assert_str_equals(
$this->{ViewScriptUrlPath} . $suffix,
$boot_cfg->{ScriptUrlPaths}{view}
);

return;
}
Expand All @@ -200,7 +221,7 @@ sub verify_Core_Bootstrap {
my $log;
open STDERR, '>', \$log;

( $boot_cfg, $resp ) = _runBootstrap(1);
( $boot_cfg, $resp ) = $this->_runBootstrap(1);
close STDERR;
$msg .= $resp . "\n\n";
$msg .= $log;
Expand All @@ -213,8 +234,11 @@ sub verify_Core_Bootstrap {
$this->assert_str_equals( $this->{PubUrlPath}, $boot_cfg->{PubUrlPath} );
$this->assert_str_equals( $this->{ScriptUrlPath},
$boot_cfg->{ScriptUrlPath} );
$this->assert_str_equals( $this->{ViewScriptUrlPath},
$boot_cfg->{ScriptUrlPaths}{view} );
my $suffix = ( $this->{url} eq 'full' ) ? $this->{suffix} : '';
$this->assert_str_equals(
$this->{ViewScriptUrlPath} . $suffix,
$boot_cfg->{ScriptUrlPaths}{view}
);

return;
}
Expand Down Expand Up @@ -293,7 +317,9 @@ sub verify_Core_Bootstrap {
sub _again { return; }

sub _runBootstrap {
my $this = shift;
my $coreTest = shift;

local %Foswiki::cfg = ( Engine => $Foswiki::cfg{Engine} );
my $msg;

Expand All @@ -302,7 +328,7 @@ sub _runBootstrap {
*FindBin::again = \&_again;
use warnings 'redefine';
$FindBin::Bin = '/var/www/foswiki/distro/core/bin';
$FindBin::Script = 'view';
$FindBin::Script = 'view' . $this->{suffix};

if ( $coreTest && Foswiki::Configure::Load->can('bootstrapConfig') ) {
$msg = Foswiki::Configure::Load::bootstrapConfig(1);
Expand Down

0 comments on commit 0a1780d

Please sign in to comment.