Subject: Re: gitweb: false base href sent when integrated via reverse proxy and path_info is active

From: Jakub Narebski <[email-masked]>
Date: Sun, 28 Nov 2010 23:05:35 +0100

On Sun, 28 Nov 2010, Jonathan Nieder wrote:
> Daniel Reichelt wrote:
> 
> > However, I just tried that and it failed. $base_url gets ignored in
> > gitweb.conf and even setting $my_url and $my_uri in gitweb.conf seems to
> > have no effect at all.
> 
> Aha.  The bug fixed by v1.7.3-rc0~85^2 (gitweb: allow configurations
> that change with each request, 2010-07-30) strikes again.
> 
> Daniel, could you try again with version 1:1.7.2.3-2.1 (from Debian sid)?
> 
> Jakub, we should probably tweak evaluate_uri to do something special if
> $base_url is already set, or advertise that $base_url needs to be set
> in per_request_config when that feature is enabled.

Jonathan, something like this, perhaps?

-- >8 ---- >8 --
From: Jakub Narebski <[email-masked]>
Subject: [PATCH/RFC] gitweb: Preserve $base_url if it was set

If $base_url was defined, then do not redefine it in evaluate_uri().
This matters only in the case when $base_url was set in gitweb config
file, and $per_request_config is false or is coderef; without this
change $base_url would get overwritten on subsequent requests from the
value set in config.

If you need $base_url that can change with request, but the default
value set by gitweb doesn't work for you, and you want most of gitweb
config file to be evaluated only once, you need to use $per_request_config
coderef to set it.

Possible-issue-reported-by: Jonathan Nieder <[email-masked]>
Signed-off-by: Jakub Narebski <[email-masked]>
---
On top of (gitweb: selectable configurations that change with each
request, 2010-11-28).

 gitweb/gitweb.perl |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 1d94718..5efee0d 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -36,10 +36,9 @@ sub evaluate_uri {
 	our $my_url = $cgi->url();
 	our $my_uri = $cgi->url(-absolute => 1);
 
-	# Base URL for relative URLs in gitweb ($logo, $favicon, ...),
-	# needed and used only for URLs with nonempty PATH_INFO
-	our $base_url = $my_url;
-
+	# $base_url contains base URL for relative URLs in gitweb ($logo, $favicon, ...);
+	# it is needed and it is used only for URLs with nonempty PATH_INFO
+	#
 	# 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
@@ -57,7 +56,11 @@ sub evaluate_uri {
 		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'};
+			# preserve existing $base_url
+			$base_url ||= $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
+		} else {
+			# in case e.g. $ENV{'SCRIPT_NAME'} is not defined
+			$base_url ||= $my_url;
 		}
 	}
 
-- 
1.7.3