threads / patch / 17761

patchgitweb: fix wrong base URL when non-root DirectoryIndex

Subject: [PATCH] gitweb: fix wrong base URL when non-root DirectoryIndex

## tl;dr

13 messages between Feb 12, 2009 and Feb 15, 2009. Diffs are folded; open one to read it.

replies: 12people: 3as markdown or json

Giuseppe Bilotta· Feb 12, 2009, 21:11 UTC · lore
From: Giuseppe Bilotta <oblomov@rbot.ratry.ru>

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
documentation about the need to strip PATH_INFO manually while we're at
it.
---
 gitweb/gitweb.perl |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)
Show changes to gitweb/gitweb.perl +12 −4
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index c2c8080..48cfd5f 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -27,15 +27,23 @@ our $version = "++GIT_VERSION++";
 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
+# 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
 our $path_info = $ENV{"PATH_INFO"};
 if ($path_info) {
 	$my_url =~ s,\Q$path_info\E$,,;
 	$my_uri =~ s,\Q$path_info\E$,,;
 }
 
+# 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, we have to build the base URL ourselves:
+our $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
+
 # core git executable to use
 # this can just be "git" if your webserver has a sensible PATH
 our $GIT = "++GIT_BINDIR++/git";
@@ -2908,7 +2916,7 @@ EOF
 	# 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
-- 
1.5.6.4
Jakub Narebski· Feb 12, 2009, 22:03 UTC · re: Giuseppe Bilotta · lore

Re: [PATCH] gitweb: fix wrong base URL when non-root DirectoryIndex

On Thu, 12 Feb 2009, Giuseppe Bilotta wrote:
> From: Giuseppe Bilotta <oblomov@rbot.ratry.ru>

Why different email? Was it intended, or was it an accident, and authorship of this patch should be to

        Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
Show 14 quoted lines
> 
> 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
> documentation about the need to strip PATH_INFO manually while we're at
> it.

Very good. The only thing I wonder about is if we should add this info also to gitweb/README, or if it is good as it is now.

Signoff?
I do not use gitweb as DirectoryIndex, but it looks sane, so:
Acked-by: Jakub Narebski <jnareb@gmail.com>
Show 48 quoted lines
> ---
>  gitweb/gitweb.perl |   16 ++++++++++++----
>  1 files changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
> index c2c8080..48cfd5f 100755
> --- a/gitweb/gitweb.perl
> +++ b/gitweb/gitweb.perl
> @@ -27,15 +27,23 @@ our $version = "++GIT_VERSION++";
>  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
> +# 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
>  our $path_info = $ENV{"PATH_INFO"};
>  if ($path_info) {
>  	$my_url =~ s,\Q$path_info\E$,,;
>  	$my_uri =~ s,\Q$path_info\E$,,;
>  }
>  
> +# 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, we have to build the base URL ourselves:
> +our $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
> +
>  # core git executable to use
>  # this can just be "git" if your webserver has a sensible PATH
>  our $GIT = "++GIT_BINDIR++/git";
> @@ -2908,7 +2916,7 @@ EOF
>  	# 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
> -- 
> 1.5.6.4
> 
> 
-- 
Jakub Narebski
Poland
Giuseppe Bilotta· Feb 13, 2009, 07:36 UTC · re: Jakub Narebski · lore

Re: [PATCH] gitweb: fix wrong base URL when non-root DirectoryIndex

On Thu, Feb 12, 2009 at 11:03 PM, Jakub Narebski <jnareb@gmail.com> wrote:
Show 8 quoted lines
> On Thu, 12 Feb 2009, Giuseppe Bilotta wrote:
>
>> From: Giuseppe Bilotta <oblomov@rbot.ratry.ru>
>
> Why different email? Was it intended, or was it an accident, and
> authorship of this patch should be to
>
>        Giuseppe Bilotta <giuseppe.bilotta@gmail.com>

I created this patch on a different computer and forgot to set the authorship as usual. I'll resend.

> Very good. The only thing I wonder about is if we should add this info
> also to gitweb/README, or if it is good as it is now.

I think there is no need to adjust the README, as in fact this fix is needed to make sure that the instructions in README are correct even for subpaths.

> Signoff?
Will add that too.
-- 
Giuseppe "Oblomov" Bilotta
Giuseppe Bilotta· Feb 13, 2009, 07:40 UTC · re: Jakub Narebski · lore

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 documentation about the need to strip PATH_INFO manually while we're at it.

Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
---
 gitweb/gitweb.perl |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)
Show changes to gitweb/gitweb.perl +12 −4
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index c2c8080..48cfd5f 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -27,15 +27,23 @@ our $version = "++GIT_VERSION++";
 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
+# 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
 our $path_info = $ENV{"PATH_INFO"};
 if ($path_info) {
 	$my_url =~ s,\Q$path_info\E$,,;
 	$my_uri =~ s,\Q$path_info\E$,,;
 }
 
+# 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, we have to build the base URL ourselves:
+our $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
+
 # core git executable to use
 # this can just be "git" if your webserver has a sensible PATH
 our $GIT = "++GIT_BINDIR++/git";
@@ -2908,7 +2916,7 @@ EOF
 	# 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
-- 
1.5.6.5
Jakub Narebski· Feb 13, 2009, 08:45 UTC · re: Giuseppe Bilotta · lore

Re: [PATCH] gitweb: fix wrong base URL when non-root DirectoryIndex

On Fri, 13 Feb 2009, Giuseppe Bilotta wrote:
Show 15 quoted lines
> 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
> documentation about the need to strip PATH_INFO manually while we're at
> it.
> 
> Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sounds good. I don't use gitweb as DirectoryIndex myself, but
Acked-by: Jakub Narebski <jnareb@gmail.com>
Show 48 quoted lines
> ---
>  gitweb/gitweb.perl |   16 ++++++++++++----
>  1 files changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
> index c2c8080..48cfd5f 100755
> --- a/gitweb/gitweb.perl
> +++ b/gitweb/gitweb.perl
> @@ -27,15 +27,23 @@ our $version = "++GIT_VERSION++";
>  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
> +# 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
>  our $path_info = $ENV{"PATH_INFO"};
>  if ($path_info) {
>  	$my_url =~ s,\Q$path_info\E$,,;
>  	$my_uri =~ s,\Q$path_info\E$,,;
>  }
>  
> +# 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, we have to build the base URL ourselves:
> +our $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
> +
>  # core git executable to use
>  # this can just be "git" if your webserver has a sensible PATH
>  our $GIT = "++GIT_BINDIR++/git";
> @@ -2908,7 +2916,7 @@ EOF
>  	# 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
> -- 
> 1.5.6.5
> 
> 
-- 
Jakub Narebski
Poland
Junio C Hamano· Feb 14, 2009, 02:14 UTC · re: Jakub Narebski · lore

Re: [PATCH] gitweb: fix wrong base URL when non-root DirectoryIndex

Jakub Narebski <jnareb@gmail.com> writes:
Show 11 quoted lines
>> Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
>
> Sounds good. I don't use gitweb as DirectoryIndex myself, but
> Acked-by: Jakub Narebski <jnareb@gmail.com>
>
>> +# 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, we have to build the base URL ourselves:
>> +our $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
Breaks t9500 with 
    [Sat Feb 14 02:12:59 2009] gitweb.perl: Use of uninitialized value in concatenation (.) or string at /pub/git/t/../gitweb/gitweb.perl line 45.

Please be more careful when giving an Ack, and more importantly please do not send a patch that does not even pass the test suite by itself.

Jakub Narebski· Feb 14, 2009, 02:42 UTC · re: Junio C Hamano · lore

Re: [PATCH] gitweb: fix wrong base URL when non-root DirectoryIndex

On Sat, 14 Feb 2009, Junio C Hamano wrote:
Show 21 quoted lines
> Jakub Narebski <jnareb@gmail.com> writes:
> 
>>> Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
>>
>> Sounds good. I don't use gitweb as DirectoryIndex myself, but
>> Acked-by: Jakub Narebski <jnareb@gmail.com>
>>
>>> +# 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, we have to build the base URL ourselves:
>>> +our $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
> 
> Breaks t9500 with 
> 
>     [Sat Feb 14 02:12:59 2009] gitweb.perl: Use of uninitialized value in
>     concatenation (.) or string at /pub/git/t/../gitweb/gitweb.perl line 45. 
> 
> Please be more careful when giving an Ack, and more importantly please do
> not send a patch that does not even pass the test suite by itself.
Actually this is not a bug in _gitweb_, but in _test_ itself. 

In t/t9500-gitweb-standalone-no-errors.sh we run gitweb.perl as a standalone script (not from a web server), and we set _some_ of CGI environmental variables. Up till now we could get by using only most important ones: GATEWAY_INTERFACE (I'm not sure if needed), HTTP_ACCEPT (used to select Content-Type to use), REQUEST_METHOD (git_feed exits early on HEAD request), and of course QUERY_STRING and PATH_INFO. The required variable SCRIPT_NAME is simply not set...

BTW. we could have set also SERVER_NAME, but it is not required by gitweb (gitweb can deal with situation when it is unset)...

So patch should be squashed with the following improvement to test suite:

-- >8 --

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 requires it if PATH_INFO is not empty, as is the case for some of tests in t9500).

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
 t/t9500-gitweb-standalone-no-errors.sh |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
Show changes to t/t9500-gitweb-standalone-no-errors.sh +4 −2
diff --git a/t/t9500-gitweb-standalone-no-errors.sh b/t/t9500-gitweb-standalone-no-errors.sh
index 43cd6ee..7c6f70b 100755
--- i/t/t9500-gitweb-standalone-no-errors.sh
+++ w/t/t9500-gitweb-standalone-no-errors.sh
@@ -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
@@ -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
 
-- 
Jakub Narebski
Poland
Giuseppe Bilotta· Feb 14, 2009, 08:52 UTC · re: Jakub Narebski · lore

Re: [PATCH] gitweb: fix wrong base URL when non-root DirectoryIndex

On Sat, Feb 14, 2009 at 3:42 AM, Jakub Narebski <jnareb@gmail.com> wrote:
Show 22 quoted lines
> On Sat, 14 Feb 2009, Junio C Hamano wrote:
>> Jakub Narebski <jnareb@gmail.com> writes:
>>
>>>> Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
>>>
>>> Sounds good. I don't use gitweb as DirectoryIndex myself, but
>>> Acked-by: Jakub Narebski <jnareb@gmail.com>
>>>
>>>> +# 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, we have to build the base URL ourselves:
>>>> +our $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
>>
>> Breaks t9500 with
>>
>>     [Sat Feb 14 02:12:59 2009] gitweb.perl: Use of uninitialized value in
>>     concatenation (.) or string at /pub/git/t/../gitweb/gitweb.perl line 45.
>>
>> Please be more careful when giving an Ack, and more importantly please do
>> not send a patch that does not even pass the test suite by itself.

Sorry, my fault. For future reference for myself, is there a way to only run the gitweb tests of the testsuite?

Show 9 quoted lines
> Actually this is not a bug in _gitweb_, but in _test_ itself.
>
> In t/t9500-gitweb-standalone-no-errors.sh we run gitweb.perl as
> a standalone script (not from a web server), and we set _some_ of CGI
> environmental variables.  Up till now we could get by using only most
> important ones: GATEWAY_INTERFACE (I'm not sure if needed), HTTP_ACCEPT
> (used to select Content-Type to use), REQUEST_METHOD (git_feed exits
> early on HEAD request), and of course QUERY_STRING and PATH_INFO.
> The required variable SCRIPT_NAME is simply not set...

Thank you for the additional patch. One thing we should also probably add to the test is the HTTP_IF_MODIFIED_SINCE. I will try to think of a bunch of tests to run against feed production after I implement the reflog data check.

-- 
Giuseppe "Oblomov" Bilotta
Junio C Hamano· Feb 14, 2009, 09:30 UTC · re: Giuseppe Bilotta · lore

Re: [PATCH] gitweb: fix wrong base URL when non-root DirectoryIndex

Giuseppe Bilotta <giuseppe.bilotta@gmail.com> writes:
> For future reference for myself, is there a way to
> only run the gitweb tests of the testsuite?
cd t && sh ./t9500-gitweb-standalone-no-errors.sh [-i] [-v]
> Thank you for the additional patch. One thing we should also probably
> add to the test is the HTTP_IF_MODIFIED_SINCE. I will try to think of
> a bunch of tests to run against feed production after I implement the
> reflog data check.
Thanks.
Jakub Narebski· Feb 14, 2009, 10:04 UTC · re: Jakub Narebski · lore

[PATCH v3] gitweb: fix wrong base URL when non-root DirectoryIndex

From: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>

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>
---
On Sat, 14 Feb 2009, Jakub Narebski wrote:
> So patch should be squashed with the following improvement to test
> suite:
[...]
Here it is. (And with [PATCH v3] :-)).

Additionally I tried to make it more robust, first by constructing $base_url _only_ when needed (when we strip PATH_INFO), and second by checking if SCRIPT_NAME is defined.

Passes test (after its modification), but not testes itself!

P.S. I wonder why Giuseppe's patch failed to apply, and failed to do fallback 3-way merge...

  Applying: gitweb: fix wrong base URL when non-root DirectoryIndex
  error: patch failed: gitweb/gitweb.perl:2908
  error: gitweb/gitweb.perl: patch does not apply
  fatal: sha1 information is lacking or useless (gitweb/gitweb.perl).
  Repository lacks necessary blobs to fall back on 3-way merge.
  Cannot fall back to three-way merge.
  Patch failed at 0001.
On top of v1.6.2-rc0-64-ge9cc02f, gitweb/gitweb.perl is 54108742...
 gitweb/gitweb.perl                     |   28 ++++++++++++++++++++++------
 t/t9500-gitweb-standalone-no-errors.sh |    6 ++++--
 2 files changed, 26 insertions(+), 8 deletions(-)
Show changes to 2 files +26 −8

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

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 5410874..83c2ad7 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -27,13 +27,29 @@ our $version = "++GIT_VERSION++";
 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
@@ -2908,7 +2924,7 @@ EOF
 	# 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
diff --git a/t/t9500-gitweb-standalone-no-errors.sh b/t/t9500-gitweb-standalone-no-errors.sh
index 43cd6ee..7c6f70b 100755
--- a/t/t9500-gitweb-standalone-no-errors.sh
+++ b/t/t9500-gitweb-standalone-no-errors.sh
@@ -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
@@ -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
 
-- 
1.6.1
Giuseppe Bilotta· Feb 14, 2009, 14:29 UTC · re: Jakub Narebski · lore

Re: [PATCH v3] gitweb: fix wrong base URL when non-root DirectoryIndex

On Sat, Feb 14, 2009 at 11:04 AM, Jakub Narebski <jnareb@gmail.com> wrote:
>
> P.S. I wonder why Giuseppe's patch failed to apply, and failed
> to do fallback 3-way merge...

Because it depends on an additional quoting fix that I sent to the mailing list but that apparently didn't get through.

> -               print '<base href="'.esc_url($my_url).'" />\n';
> +               print '<base href="'.esc_url($base_url).'" />\n';

The quoting fix was here: double quotes are needed because otherwise the \n gets in literally. We can probably squash it with this patch. Gimme a sec and I'll cook it up again.

-- 
Giuseppe "Oblomov" Bilotta
Giuseppe Bilotta· Feb 15, 2009, 09:18 UTC · re: Giuseppe Bilotta · lore

[PATCHv4] 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>
---

There was a bug in Jakub's rewritten logic for the PATH_INFO stripping: using || instead of && meant that $my_uri was not being stripped if $my_url was, resulting in wrong relative links all around. Instead, if we can strip PATH_INFO from $my_url then we can (and must) strip it from $my_uri too (&&), and then check for the definition of SCRIPT_NAME.

This patch also included a quoting patch that apparently didn't make it to the list: the <base> tag was inserting a literal \n instead of a newline. Since that line got rewritten anyway, I thought it wasn't a big deal to squash it in.

 gitweb/gitweb.perl                     |   28 ++++++++++++++++++++++------
 t/t9500-gitweb-standalone-no-errors.sh |    6 ++++--
 2 files changed, 26 insertions(+), 8 deletions(-)
Show changes to 2 files +26 −8

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

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 8dffa3f..7c48181 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -27,13 +27,29 @@ our $version = "++GIT_VERSION++";
 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
@@ -2908,7 +2924,7 @@ EOF
 	# 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
diff --git a/t/t9500-gitweb-standalone-no-errors.sh b/t/t9500-gitweb-standalone-no-errors.sh
index 43cd6ee..7c6f70b 100755
--- a/t/t9500-gitweb-standalone-no-errors.sh
+++ b/t/t9500-gitweb-standalone-no-errors.sh
@@ -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
@@ -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
 
-- 
1.5.6.5
Junio C Hamano· Feb 15, 2009, 19:33 UTC · re: Giuseppe Bilotta · lore

Re: [PATCH v3] gitweb: fix wrong base URL when non-root DirectoryIndex

Giuseppe Bilotta <giuseppe.bilotta@gmail.com> writes:
Show 14 quoted lines
> On Sat, Feb 14, 2009 at 11:04 AM, Jakub Narebski <jnareb@gmail.com> wrote:
>>
>> P.S. I wonder why Giuseppe's patch failed to apply, and failed
>> to do fallback 3-way merge...
>
> Because it depends on an additional quoting fix that I sent to the
> mailing list but that apparently didn't get through.
>
>> -               print '<base href="'.esc_url($my_url).'" />\n';
>> +               print '<base href="'.esc_url($base_url).'" />\n';
>
> The quoting fix was here: double quotes are needed because otherwise
> the \n gets in literally. We can probably squash it with this patch.
> Gimme a sec and I'll cook it up again.
Thanks, both.

← back to recent threads