# Re: [PATCH v2] gitweb: fix base url set if PATH_INFO is used, add a / at the end

2 messages from 2023-02-09 to 2023-02-14. Participants: Junio C Hamano, Daniel Abrecht.
Thread: https://gitlist.dev/t/59221

## Junio C Hamano, 2023-02-09 23:05

Subject: Re: [PATCH v2] gitweb: fix base url set if PATH_INFO is used, add a / at the end
Message-ID: <xmqqfsbel9sf.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqfsbel9sf.fsf%40gitster.g
In-Reply-To: <20230209222648.z-sqdvWiK7xa-NLagt3B_X_4bFQJBsA8cA06YvgXqWQ@z>

```
Daniel Abrecht <git-git@nodmarc.danielabrecht.ch> writes:

> In HTML, if there is a base tag like `<base href="/a/b">`, a relative
> URL like
> `c/d` will be resolved by the browser as `a/c/d` and not as
> `a/b/c/d`. But with
> a base tag like `<base href="/a/b/">` it will result in `a/b/c/d`.
>
> Signed-off-by: Daniel Abrecht <public@danielabrecht.ch>
> ---
>  gitweb/gitweb.perl | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
> index e66eb3d9ba..edcee1652c 100755
> --- a/gitweb/gitweb.perl
> +++ b/gitweb/gitweb.perl
> @@ -74,6 +74,11 @@ sub evaluate_uri {
>  		}
>  	}
>
> +	# $base_url is later used in the <base> tag as a URL, if it
> doesn't end in a /

It seems an overlog line was wrapped around and broke the patch.

> +	# the browser will strip away the last component for relative URLs.
> +	# Add the / if it's missing.

The above is not an incorrect statement per-se, but if $base were
pointing at the document, we would likely break if we add an extra
slash.  Don't we want to say something like 

	# $base_url at this point points at a directory, not a
        # single document, and later is used in the <base> tag.
        # Make sure it ends in a '/'.  Otherwise, we'd lose the last
        # component when forming a relative URL.

perhaps?

> +	$base_url .= '/' if not $base_url =~ /\/$/;
> +
>  	# target of the home link on top of all pages
>  	our $home_link = $my_uri || "/";
>  }

```

## Daniel Abrecht, 2023-02-14 10:15

Subject: Re: [PATCH v2] gitweb: fix base url set if PATH_INFO is used, add a / at the end
Message-ID: <522571c427c937ef52bb69b88cb3dea1@nodmarc.danielabrecht.ch>
URL: https://gitlist.dev/e/522571c427c937ef52bb69b88cb3dea1%40nodmarc.danielabrecht.ch
In-Reply-To: <xmqqfsbel9sf.fsf@gitster.g>

```
Am 2023-02-10 00:05, schrieb Junio C Hamano:
> Daniel Abrecht <git-git@nodmarc.danielabrecht.ch> writes:
>> +	# the browser will strip away the last component for relative URLs.
>> +	# Add the / if it's missing.
> 
> The above is not an incorrect statement per-se, but if $base were
> pointing at the document, we would likely break if we add an extra
> slash.  Don't we want to say something like
> 
> 	# $base_url at this point points at a directory, not a
>         # single document, and later is used in the <base> tag.
>         # Make sure it ends in a '/'.  Otherwise, we'd lose the last
>         # component when forming a relative URL.
> 
> perhaps?

After thinking about this for a while, I realized that this isn't as 
simple as I thought.

I'm currently using apache and some rewrite rules so I can access 
projects at /git/myproject.git/ (See my config below).
So in my case, $base_url is /git at this point, and I need to add a /.

But if I didn't have those rewrite rules, I could instead access the 
repos using /git/gitweb.cgi/myproject.git/ .
In that case, $base_url would be /git/gitweb.cgi at this point, so it 
does refer to the script / document.
The assets are still at /git/ and not at /git/gitweb.cgi/, so links are 
relative to /git/gitweb.cgi/ but assets are
relative to /git/. It seams relative URLs are only used for assets, so 
the $base_url should really be /git/gitweb.cgi
or alternatively just /git/ there, and just adding a slash would be 
wrong in that case.

Given this, I no longer think I should change the way it currently is.
I will just override $base_url in my config file instead.



<Macro gitweb $location>
   SetEnv GITWEB_CONFIG /etc/gitweb.conf
   SetEnv GIT_HTTP_EXPORT_ALL
   ScriptAliasMatch \
   "(?x)^/.*/(.*\.git/(HEAD | \
       info/refs | \
       objects/(info/[^/]+ | \
          [0-9a-f]{2}/[0-9a-f]{38} | \
          pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
       git-(upload|receive)-pack))$" \
   /usr/lib/git-core/git-http-backend/$1
   Alias $location/ /usr/share/gitweb/
   RewriteRule 
^$location/.*\.git/(HEAD|info|objects|refs|git-(upload|receive)-pack) - 
[E=GIT_PROJECT_ROOT:%{DOCUMENT_ROOT}$location/,PT,END]
   RewriteRule ^$location/(.*\.git(/.*)?)?$ $location/gitweb.cgi/$1 
[E=GIT_PROJECT_ROOT:%{DOCUMENT_ROOT}$location/,L,PT]
   <Location $location>
     Require all granted
     AllowOverride None
   </Location>
   <Directory /usr/share/gitweb/>
     Options +FollowSymLinks +ExecCGI
     AddHandler cgi-script .cgi
   </Directory>
</Macro>

<VirtualHost *:443>
   Use VHostSSL projects.dpa.li
   DocumentRoot /var/www/projects.dpa.li/
   Use gitweb /git
</VirtualHost>

```
