Skip to content

Commit

Permalink
gitweb: fix wrong base URL when non-root DirectoryIndex
Browse files Browse the repository at this point in the history
CGI::url() has some issues when rebuilding the script URL if the script
is a DirectoryIndex.

One of these issue is the inability to strip PATH_INFO, which is why
we had to do it ourselves.

Another issue is that the resulting URL cannot be used for the <base>
tag: it works if we're the DirectoryIndex at the root level, but not
otherwise.

We fix this by building the proper base URL ourselves, and improve the
comment about the need to strip PATH_INFO manually while we're at it.

Additionally t/t9500-gitweb-standalone-no-errors.sh had to be modified
to set SCRIPT_NAME variable (CGI standard states that it MUST be set,
and now gitweb uses it if PATH_INFO is not empty, as is the case for
some of tests in t9500).

Signed-off-by: Giuseppe Bilotta <[email protected]>
Signed-off-by: Jakub Narebski <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
Oblomov authored and gitster committed Feb 17, 2009
1 parent 4a5856c commit 81d3fe9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
28 changes: 22 additions & 6 deletions gitweb/gitweb.perl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,29 @@ BEGIN
our $my_url = $cgi->url();
our $my_uri = $cgi->url(-absolute => 1);

# if we're called with PATH_INFO, we have to strip that
# from the URL to find our real URL
# we make $path_info global because it's also used later on
# Base URL for relative URLs in gitweb ($logo, $favicon, ...),
# needed and used only for URLs with nonempty PATH_INFO
our $base_url = $my_url;

# When the script is used as DirectoryIndex, the URL does not contain the name
# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
# have to do it ourselves. We make $path_info global because it's also used
# later on.
#
# Another issue with the script being the DirectoryIndex is that the resulting
# $my_url data is not the full script URL: this is good, because we want
# generated links to keep implying the script name if it wasn't explicitly
# indicated in the URL we're handling, but it means that $my_url cannot be used
# as base URL.
# Therefore, if we needed to strip PATH_INFO, then we know that we have
# to build the base URL ourselves:
our $path_info = $ENV{"PATH_INFO"};
if ($path_info) {
$my_url =~ s,\Q$path_info\E$,,;
$my_uri =~ s,\Q$path_info\E$,,;
if ($my_url =~ s,\Q$path_info\E$,, &&
$my_uri =~ s,\Q$path_info\E$,, &&
defined $ENV{'SCRIPT_NAME'}) {
$base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
}
}

# core git executable to use
Expand Down Expand Up @@ -2908,7 +2924,7 @@ sub git_header_html {
# the stylesheet, favicon etc urls won't work correctly with path_info
# unless we set the appropriate base URL
if ($ENV{'PATH_INFO'}) {
print '<base href="'.esc_url($my_url).'" />\n';
print "<base href=\"".esc_url($base_url)."\" />\n";
}
# print out each stylesheet that exist, providing backwards capability
# for those people who defined $stylesheet in a config file
Expand Down
6 changes: 4 additions & 2 deletions t/t9500-gitweb-standalone-no-errors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ gitweb_run () {
GATEWAY_INTERFACE="CGI/1.1"
HTTP_ACCEPT="*/*"
REQUEST_METHOD="GET"
SCRIPT_NAME="$TEST_DIRECTORY/../gitweb/gitweb.perl"
QUERY_STRING=""$1""
PATH_INFO=""$2""
export GATEWAY_INTERFACE HTTP_ACCEPT REQUEST_METHOD QUERY_STRING PATH_INFO
export GATEWAY_INTERFACE HTTP_ACCEPT REQUEST_METHOD \
SCRIPT_NAME QUERY_STRING PATH_INFO

GITWEB_CONFIG=$(pwd)/gitweb_config.perl
export GITWEB_CONFIG
Expand All @@ -54,7 +56,7 @@ gitweb_run () {
# written to web server logs, so we are not interested in that:
# we are interested only in properly formatted errors/warnings
rm -f gitweb.log &&
perl -- "$TEST_DIRECTORY/../gitweb/gitweb.perl" \
perl -- "$SCRIPT_NAME" \
>/dev/null 2>gitweb.log &&
if grep "^[[]" gitweb.log >/dev/null 2>&1; then false; else true; fi

Expand Down

0 comments on commit 81d3fe9

Please sign in to comment.