git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] gitweb: Replace <base> tag with full URLs (when using PATH_INFO)

From
TGTobias Girstmair <tobi@isticktoit.net>
Date
Jul 11, 2020, 20:39 UTC
Message-ID
<20200711203947.23520-1-tobi@isticktoit.net>

using a base tag has the side-effect of not just changing the few URLs of gitweb's static resources, but all other relative links (e.g. those in a README.html), too.

Signed-off-by: Tobias Girstmair <tobi@isticktoit.net>
---
My exact use case is this: Many of my repositories have README.{rst,md}
files with relative links to more md/rst files. I'm generating a
README.html from that file with a post-update hook, and rewrite links,
so they display the file. Some cloned repos' READMEs even have images in
them, whose links I'm fixing up as well.

Due to the precence of the <base> tag, I can't just prepend "blob_plain/HEAD:" to the URLs, but have to recreate the full URL, including the last few directories of $GIT_DIR. That's annoying to keep portable (move a repo from e.g. /srv/git/foo.git to /srv/git/**public**/foo.git and the URL must be changed in the hook, and the hook re-executed).

Looking forward to your feedback,
	tobias
 gitweb/gitweb.perl | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 0959a78..049bb64 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -2232,7 +2232,7 @@ sub git_get_avatar {
 		return $pre_white .
 		       "<img width=\"$size\" " .
 		            "class=\"avatar\" " .
-		            "src=\"".esc_url($url)."\" " .
+		            "src=\"".get_base_url().esc_url($url)."\" " .
 			    "alt=\"\" " .
 		       "/>" . $post_white;
 	} else {
@@ -2674,6 +2674,13 @@ sub get_feed_info {
 	return %res;
 }
 
+# the stylesheet, favicon etc urls won't work correctly with path_info
+# unless we set the appropriate base URL. not using a <base> tag to not
+# also change relative URLs inserted by the user.
+sub get_base_url {
+	return $ENV{'PATH_INFO'}? esc_url($base_url)."/" : "";
+}
+
 ## ----------------------------------------------------------------------
 ## git utility subroutines, invoking git commands
 
@@ -4099,17 +4106,17 @@ sub print_header_links {
 	# print out each stylesheet that exist, providing backwards capability
 	# for those people who defined $stylesheet in a config file
 	if (defined $stylesheet) {
-		print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
+		print '<link rel="stylesheet" type="text/css" href="'.get_base_url().esc_url($stylesheet).'"/>'."\n";
 	} else {
 		foreach my $stylesheet (@stylesheets) {
 			next unless $stylesheet;
-			print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
+			print '<link rel="stylesheet" type="text/css" href="'.get_base_url().esc_url($stylesheet).'"/>'."\n";
 		}
 	}
 	print_feed_meta()
 		if ($status eq '200 OK');
 	if (defined $favicon) {
-		print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
+		print qq(<link rel="shortcut icon" href=").get_base_url().esc_url($favicon).qq(" type="image/png" />\n);
 	}
 }
 
@@ -4212,11 +4219,6 @@ sub git_header_html {
 <meta name="robots" content="index, nofollow"/>
 <title>$title</title>
 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($base_url)."\" />\n";
-	}
 	print_header_links($status);
 
 	if (defined $site_html_head_string) {
@@ -4234,7 +4236,7 @@ sub git_header_html {
 	if (defined $logo) {
 		print $cgi->a({-href => esc_url($logo_url),
 		               -title => $logo_label},
-		              $cgi->img({-src => esc_url($logo),
+		              $cgi->img({-src => get_base_url().esc_url($logo),
 		                         -width => 72, -height => 27,
 		                         -alt => "git",
 		                         -class => "logo"}));
@@ -4299,7 +4301,7 @@ sub git_footer_html {
 		insert_file($site_footer);
 	}
 
-	print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
+	print qq!<script type="text/javascript" src="!.get_base_url().esc_url($javascript).qq!"></script>\n!;
 	if (defined $action &&
 	    $action eq 'blame_incremental') {
 		print qq!<script type="text/javascript">\n!.
@@ -8273,7 +8275,7 @@ sub git_feed {
 		if (defined $logo || defined $favicon) {
 			# prefer the logo to the favicon, since RSS
 			# doesn't allow both
-			my $img = esc_url($logo || $favicon);
+			my $img = get_base_url().esc_url($logo || $favicon);
 			print "<image>\n" .
 			      "<url>$img</url>\n" .
 			      "<title>$title</title>\n" .
@@ -8299,11 +8301,11 @@ sub git_feed {
 		      # use project owner for feed author
 		      "<author><name>$owner</name></author>\n";
 		if (defined $favicon) {
-			print "<icon>" . esc_url($favicon) . "</icon>\n";
+			print "<icon>" . get_base_url() . esc_url($favicon) . "</icon>\n";
 		}
 		if (defined $logo) {
 			# not twice as wide as tall: 72 x 27 pixels
-			print "<logo>" . esc_url($logo) . "</logo>\n";
+			print "<logo>" . get_base_url() . esc_url($logo) . "</logo>\n";
 		}
 		if (! %latest_date) {
 			# dummy date to keep the feed valid until commits trickle in:
-- 
2.21.3
Next: Tobias Girstmair
Message 1 of 10 in “gitweb: Replace <base> tag with full URLs (when using PATH_INFO)”
  1. gitweb: Replace <base> tag with full URLs (when using PATH_INFO)Tobias Girstmair, Jul 11, 2020
  2. gitweb: Replace <base> tag with full URLs (when using PATH_INFO)Tobias Girstmair, Jul 12, 2020
  3. gitweb: Replace <base> tag with full URLs (when using PATH_INFO)Tobias Girstmair, Jul 12, 2020
  4. Junio C HamanoJul 12, 2020
  5. Tobias GirstmairJul 12, 2020
  6. Junio C HamanoJul 13, 2020
  7. Tobias GirstmairJul 13, 2020
  8. Junio C HamanoJul 13, 2020
  9. Tobias GirstmairJul 13, 2020
  10. Tobias GirstmairNov 20, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.