Skip to contentSkip to conversation

gitweb: make static files accessible with PATH_INFO

PATCH In review Giuseppe Bilotta 3 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

v5 cover gitweb: make static files accessible with PATH_INFO

This patchset includes

1. the single patch to make static files accessible with PATH_INFO, with the same quoting standard as the nearby code;
2. an additional patch that extends the README with some examples on how to configure Apache to use gitweb with PATH_INFO enabled by default (and gitweb as directory index);
3. the repeatedly recommended comment fix to align comments to code in the git_header_html() sub.

Giuseppe Bilotta (3):
gitweb: make static files accessible with PATH_INFO
gitweb: webserver config for PATH_INFO
gitweb: align comments to code

 gitweb/README      |   76 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 gitweb/gitweb.perl |    9 +++++-
 2 files changed, 83 insertions(+), 2 deletions(-)
#1 lore raw

PATCH v5 1/3 gitweb: make static files accessible with PATH_INFO

Gitweb links to a number of static files such as CSS stylesheets, favicon or the git logo. When, such as with the default Makefile, the paths to these files are relative (i.e. doesn't start with a "/"), the files become inaccessible in any view other tha project list and summary page if gitweb is invoked with a non-empty PATH_INFO.

Fix this by adding a <base> element pointing to the script's own URL, which ensure that all relative paths will be resolved correctly.

---
 gitweb/gitweb.perl |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
gitweb/gitweb.perl +5 -0
index 931db4f..538bd5a 100755
@@ -2901,6 +2901,11 @@ 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($my_url).'" />\n';
+	}
 # print out each stylesheet that exist
 	if (defined $stylesheet) {
 #provides backwards capability for those people who define style sheet in a config file
Signature · 1 line
1.5.6.5
#2 lore raw

PATCH v5 2/3 gitweb: webserver config for PATH_INFO

Document some possible Apache configurations when the path_info feature is enabled in gitweb.

---
 gitweb/README |   76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 0 deletions(-)
gitweb/README +76 -0
index 825162a..52ad88b 100644
@@ -322,6 +322,82 @@ something like the following in your gitweb.conf (or gitweb_config.perl) file:
   $home_link = "/";
 
 
+PATH_INFO usage
+-----------------------
+If you enable PATH_INFO usage in gitweb by putting
+
+   $feature{'pathinfo'}{'default'} = [1];
+
+in your gitweb.conf, it is possible to set up your server so that it
+consumes and produces URLs in the form
+
+http://git.example.com/project.git/shortlog/sometag
+
+by using a configuration such as the following, that assumes that
+/var/www/gitweb is the DocumentRoot of your webserver, and that it
+contains the gitweb.cgi script and complementary static files
+(stylesheet, favicon):
+
+<VirtualHost *:80>
+	ServerAlias git.example.com
+
+	DocumentRoot /var/www/gitweb
+
+	<Directory /var/www/gitweb>
+		Options ExecCGI
+		AddHandler cgi-script cgi
+
+		DirectoryIndex gitweb.cgi
+
+		RewriteEngine On
+		RewriteCond %{REQUEST_FILENAME} !-f
+		RewriteCond %{REQUEST_FILENAME} !-d
+		RewriteRule ^.* /gitweb.cgi/$0 [L,PT]
+	</Directory>
+</VirtualHost>
+
+The rewrite rule guarantees that existing static files will be properly
+served, whereas any other URL will be passed to gitweb as PATH_INFO
+parameter.
+
+Notice that in this case you don't need special settings for
+@stylesheets, $my_uri and $home_link, but you lose "dumb client" access
+to your project .git dirs. A possible workaround for the latter is the
+following: in your project root dir (e.g. /pub/git) have the projects
+named without a .git extension (e.g. /pub/git/project instead of
+/pub/git/project.git) and configure Apache as follows:
+
+<VirtualHost *:80>
+	ServerAlias git.example.com
+
+	DocumentRoot /var/www/gitweb
+
+	AliasMatch ^(/.*?)(\.git)(/.*)? /pub/git$1$3
+	<Directory /var/www/gitweb>
+		Options ExecCGI
+		AddHandler cgi-script cgi
+
+		DirectoryIndex gitweb.cgi
+
+		RewriteEngine On
+		RewriteCond %{REQUEST_FILENAME} !-f
+		RewriteCond %{REQUEST_FILENAME} !-d
+		RewriteRule ^.* /gitweb.cgi/$0 [L,PT]
+	</Directory>
+</VirtualHost>
+
+The additional AliasMatch makes it so that
+
+http://git.example.com/project.git
+
+will give raw access to the project's git dir (so that the project can
+be cloned), while
+
+http://git.example.com/project
+
+will provide human-friendly gitweb access.
+
+
 Originally written by:
   Kay Sievers <[email protected]>
 
Signature · 1 line
1.5.6.5
#3 lore raw

PATCH v5 3/3 gitweb: align comments to code

---
 gitweb/gitweb.perl |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
gitweb/gitweb.perl +4 -4
index 538bd5a..c5f0130 100755
@@ -2901,14 +2901,14 @@ 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
+	# 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 out each stylesheet that exist
+	# print out each stylesheet that exist, providing backwards capability
+	# for those people who defined $stylesheet in a config file
 	if (defined $stylesheet) {
-#provides backwards capability for those people who define style sheet in a config file
 		print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
 	} else {
 		foreach my $stylesheet (@stylesheets) {
Signature · 1 line
1.5.6.5
#4 lore raw

[PATCH 4/3] gitweb: Update README that gitweb works better with PATH_INFO

One had to configure gitweb for it to find static files (stylesheets, images) when using path_info URLs. Now that it is not necessary thanks to adding BASE element to HTML head if needed, update README to reflect this fact.

---
This is update of gitweb/README to fit state after Giuseppe patches.

There is a little problem either with terminology, or with CGI.pm module itself. RFC 1808 "Relative Uniform Resource Locators" says that:
http://git.example.com/cgi-bin/gitweb.cgi is absolute URL
/cgi-bin/gitweb.cgi is relative URL
but absolute path
gitweb.cgi is relative URL
and relative path

while CGI.pm (or, to be more exact, its url() method) says:

  url() or url(-full)  returns http://git.example.com/cgi-bin/gitweb.cgi
  url(-absolute)       returns /cgi-bin/gitweb.cgi
  url(-relative)       returns gitweb.cgi (or /cgi-bin/gitweb.cgi)

That is why I used "Full URL" for $my_url = $cgi->url(); and "absolute URL" (as before) for $my_uri = $cgi->url(-absolute); in gitweb/README.

 gitweb/README |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
gitweb/README +4 -6
index 52ad88b..a9dc2e5 100644
@@ -162,14 +162,12 @@ not include variables usually directly set during build):
    $GITWEB_LIST during installation.  If empty, $projectroot is used
    to scan for repositories.
  * $my_url, $my_uri
-   URL and absolute URL of gitweb script; you might need to set those
-   variables if you are using 'pathinfo' feature: see also below.
+   Full URL and absolute URL of gitweb script;
+   in earlier versions of gitweb you might have need to set those
+   variables, now there should be no need to do it.
  * $home_link
    Target of the home link on top of all pages (the first part of view
-   "breadcrumbs").  By default set to absolute URI of a page; you might
-   need to set it up to [base] gitweb URI if you use 'pathinfo' feature
-   (alternative format of the URLs, with project name embedded directly
-   in the path part of URL).
+   "breadcrumbs").  By default set to absolute URI of a page ($my_uri).
  * @stylesheets
    List of URIs of stylesheets (relative to base URI of a page). You
    might specify more than one stylesheet, for example use gitweb.css
Signature · 1 line
1.6.1
#5 lore raw
In reply to Giuseppe Bilotta
Quoted 9 lines · Giuseppe Bilotta

This patchset includes

1. the single patch to make static files accessible with PATH_INFO, with the same quoting standard as the nearby code;
2. an additional patch that extends the README with some examples on how to configure Apache to use gitweb with PATH_INFO enabled by default (and gitweb as directory index);
3. the repeatedly recommended comment fix to align comments to code in the git_header_html() sub.

I like it. Ack for the whole series.

Quoted 10 lines

Giuseppe Bilotta (3):
gitweb: make static files accessible with PATH_INFO
gitweb: webserver config for PATH_INFO
gitweb: align comments to code

 gitweb/README      |   76 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 gitweb/gitweb.perl |    9 +++++-
 2 files changed, 83 insertions(+), 2 deletions(-)
Signature · 2 lines
Jakub Narebski
Poland
#6 lore raw
Quoted 12 lines · Jakub Narebski
Quoted 9 lines · Giuseppe Bilotta

This patchset includes

1. the single patch to make static files accessible with PATH_INFO, with the same quoting standard as the nearby code;
2. an additional patch that extends the README with some examples on how to configure Apache to use gitweb with PATH_INFO enabled by default (and gitweb as directory index);
3. the repeatedly recommended comment fix to align comments to code in the git_header_html() sub.

Quoted 1 line

I like it. Ack for the whole series.

Thanks, both. Applied.