@matheusbsbs / matheus-bagliano / commits / 81d3fe9f48
Fork 0

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 <giuseppe.bilotta@gmail.com> Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Giuseppe Bilotta committed Feb 15, 2009 at 10:18 UTC 81d3fe9f4871e42ebd1af0221fa091fe5476e2f7
2 files changed +26 -8
gitweb/gitweb.perl
+22 -6
@@ -27,13 +27,29 @@ our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute => 1);
29
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:
46 our $path_info = $ENV{"PATH_INFO"};
47 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 + }
53 }
54
55 # core git executable to use
@@ -2908,7 +2924,7 @@ EOF
2924 # the stylesheet, favicon etc urls won't work correctly with path_info
2925 # unless we set the appropriate base URL
2926 if ($ENV{'PATH_INFO'}) {
2911 - print '<base href="'.esc_url($my_url).'" />\n';
2927 + print "<base href=\"".esc_url($base_url)."\" />\n";
2928 }
2929 # print out each stylesheet that exist, providing backwards capability
2930 # for those people who defined $stylesheet in a config file
t/t9500-gitweb-standalone-no-errors.sh
+4 -2
@@ -43,9 +43,11 @@ gitweb_run () {
43 GATEWAY_INTERFACE="CGI/1.1"
44 HTTP_ACCEPT="*/*"
45 REQUEST_METHOD="GET"
46 + SCRIPT_NAME="$TEST_DIRECTORY/../gitweb/gitweb.perl"
47 QUERY_STRING=""$1""
48 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
51
52 GITWEB_CONFIG=$(pwd)/gitweb_config.perl
53 export GITWEB_CONFIG
@@ -54,7 +56,7 @@ gitweb_run () {
56 # written to web server logs, so we are not interested in that:
57 # we are interested only in properly formatted errors/warnings
58 rm -f gitweb.log &&
57 - perl -- "$TEST_DIRECTORY/../gitweb/gitweb.perl" \
59 + perl -- "$SCRIPT_NAME" \
60 >/dev/null 2>gitweb.log &&
61 if grep "^[[]" gitweb.log >/dev/null 2>&1; then false; else true; fi
62