Skip to content

Commit 81d3fe9

Browse files
Oblomovgitster
authored andcommitted
gitweb: fix wrong base URL when non-root DirectoryIndex
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]>
1 parent 4a5856c commit 81d3fe9

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

gitweb/gitweb.perl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,29 @@ BEGIN
2727
our $my_url = $cgi->url();
2828
our $my_uri = $cgi->url(-absolute => 1);
2929

30-
# if we're called with PATH_INFO, we have to strip that
31-
# from the URL to find our real URL
32-
# we make $path_info global because it's also used later on
30+
# Base URL for relative URLs in gitweb ($logo, $favicon, ...),
31+
# needed and used only for URLs with nonempty PATH_INFO
32+
our $base_url = $my_url;
33+
34+
# When the script is used as DirectoryIndex, the URL does not contain the name
35+
# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
36+
# have to do it ourselves. We make $path_info global because it's also used
37+
# later on.
38+
#
39+
# Another issue with the script being the DirectoryIndex is that the resulting
40+
# $my_url data is not the full script URL: this is good, because we want
41+
# generated links to keep implying the script name if it wasn't explicitly
42+
# indicated in the URL we're handling, but it means that $my_url cannot be used
43+
# as base URL.
44+
# Therefore, if we needed to strip PATH_INFO, then we know that we have
45+
# to build the base URL ourselves:
3346
our $path_info = $ENV{"PATH_INFO"};
3447
if ($path_info) {
35-
$my_url =~ s,\Q$path_info\E$,,;
36-
$my_uri =~ s,\Q$path_info\E$,,;
48+
if ($my_url =~ s,\Q$path_info\E$,, &&
49+
$my_uri =~ s,\Q$path_info\E$,, &&
50+
defined $ENV{'SCRIPT_NAME'}) {
51+
$base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
52+
}
3753
}
3854

3955
# core git executable to use
@@ -2908,7 +2924,7 @@ sub git_header_html {
29082924
# the stylesheet, favicon etc urls won't work correctly with path_info
29092925
# unless we set the appropriate base URL
29102926
if ($ENV{'PATH_INFO'}) {
2911-
print '<base href="'.esc_url($my_url).'" />\n';
2927+
print "<base href=\"".esc_url($base_url)."\" />\n";
29122928
}
29132929
# print out each stylesheet that exist, providing backwards capability
29142930
# for those people who defined $stylesheet in a config file

t/t9500-gitweb-standalone-no-errors.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ gitweb_run () {
4343
GATEWAY_INTERFACE="CGI/1.1"
4444
HTTP_ACCEPT="*/*"
4545
REQUEST_METHOD="GET"
46+
SCRIPT_NAME="$TEST_DIRECTORY/../gitweb/gitweb.perl"
4647
QUERY_STRING=""$1""
4748
PATH_INFO=""$2""
48-
export GATEWAY_INTERFACE HTTP_ACCEPT REQUEST_METHOD QUERY_STRING PATH_INFO
49+
export GATEWAY_INTERFACE HTTP_ACCEPT REQUEST_METHOD \
50+
SCRIPT_NAME QUERY_STRING PATH_INFO
4951

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

0 commit comments

Comments
 (0)