From a4b6378eb2511a53aa87290afe096f22d1f55480 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Sun, 26 Jan 2014 22:13:19 -0800 Subject: [PATCH] Add new url_server utility function and make use of it The url_server function replaces the scheme, host and port of URL with the current CGI connection's values. The html.cgi templating engine now supports this with @@server(...)@@. The rootcert.html page now makes use of this to display the root certificate's URL properly. --- Girocco/Util.pm | 27 ++++++++++++++++++++++++++- cgi/html.cgi | 2 ++ html/rootcert.html | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Girocco/Util.pm b/Girocco/Util.pm index fffb4d8..12c5224 100644 --- a/Girocco/Util.pm +++ b/Girocco/Util.pm @@ -12,7 +12,7 @@ BEGIN { lock_file unlock_file valid_tag rand_adjust filedb_atomic_append filedb_atomic_edit filedb_grep filedb_atomic_grep valid_email valid_email_multi - valid_repo_url valid_web_url url_base url_path + valid_repo_url valid_web_url url_base url_path url_server projects_html_list parse_rfc2822_date parse_any_date); } @@ -211,6 +211,31 @@ sub url_path { return $url; } +# If both SERVER_NAME and SERVER_PORT are set pass the argument through url_path +# and then prefix it with the appropriate scheme (HTTPS=?on), host and port and +# return it. If a something that doesn't look like it could be the start of a +# URL path comes back from url_path or SERVER_NAME is a link-local IPv6 address +# then just return the argument unchanged. +sub url_server { + my $url = shift || ''; + my $path = url_path($url); + return $url unless $path eq '' || $path =~ m|^[/?#]|; + return $url unless $ENV{'SERVER_NAME'} && $ENV{'SERVER_PORT'} && + $ENV{'SERVER_PORT'} =~ /^[1-9][0-9]{0,4}$/; + return $url if $ENV{'SERVER_NAME'} =~ /^[[]?fe80:/i; + my $server = $ENV{'SERVER_NAME'}; + # Deal with Apache bug where IPv6 literal server names do not include + # the required surrounding '[' and ']' characters + $server = '[' . $server . ']' if $server =~ /:/ && $server !~ /^[[]/; + my $ishttps = $ENV{'HTTPS'} && $ENV{'HTTPS'} =~ /^on$/i; + my $portnum = 0 + $ENV{'SERVER_PORT'}; + my $port = ''; + if (($ishttps && $portnum != 443) || (!$ishttps && $portnum != 80)) { + $port = ':' . $portnum; + } + return 'http' . ($ishttps ? 's' : '') . '://' . $server . $port . $path; +} + sub _escapeHTML { my $str = shift; $str =~ s/\&/\&/gs; diff --git a/cgi/html.cgi b/cgi/html.cgi index 824bae0..2103693 100755 --- a/cgi/html.cgi +++ b/cgi/html.cgi @@ -17,6 +17,7 @@ use Girocco::Util; # /@@gitweburl@@/ substitute for gitweburl configuration variable # /@@base(gitweburl)@@/ substitute for base portion of gitweburl variable # /@@path(gitweburl)@@/ substitute for path portion of gitweburl variable +# /@@server(gitweburl)@@/ replace scheme://host:port portion of gitweburl variable # /@@ifmob@@...@@end@@/ remove unless mob defined # /@@ifssh@@...@@end@@/ remove unless pushurl defined # /@@ifhttps@@...@@end@@/ remove unless httpspushurl defined @@ -64,6 +65,7 @@ foreach (split(/\n/, $template)) { } else { s/@\@base\((\w+?)\)@@/url_base(${$Girocco::Config::{$1}})/ge; s/@\@path\((\w+?)\)@@/url_path(${$Girocco::Config::{$1}})/ge; + s/@\@server\((\w+?)\)@@/url_server(${$Girocco::Config::{$1}})/ge; s/@@(\w+?)@@/${$Girocco::Config::{$1}}/ge; print "$_\n"; } diff --git a/html/rootcert.html b/html/rootcert.html index 35e12cb..7076421 100644 --- a/html/rootcert.html +++ b/html/rootcert.html @@ -15,7 +15,7 @@ browser, this site uses its own root certificate.

The root certificate for this site is available from:

-@@webadmurl@@/@@nickname@@_root_cert.pem +@@server(webadmurl)@@/@@nickname@@_root_cert.pem

A side effect of using an unrecognized root certificate is that Git may -- 2.11.4.GIT