cgit

commit 9c931b1e6e68f8dc891a5653035c3d70038ae3c0

Author: Eric Wong <normalperson@yhbt.net>

use Host: header to generate cgit_hosturl

I run an instance of lighttpd for cgit behind nginx (nginx
doesn't execute CGI).  So the port (SERVER_PORT=33333) that
lighttpd runs on sends to cgit is different from the standard
port 80 that public clients connect to (via nginx).

This was causing the Atom feed URL to show the private port
number that lighttpd was running on.

Since the HTTP/1.1 "Host" header includes the port number if
running on a non-standard port, it allows non-client-facing HTTP
servers to transparently generate public URLs that clients can
see.

So use the "Host" header if it is available and fall back to
SERVER_NAME/SERVER_PORT for some clients that don't set
HTTP_HOST.

Signed-off-by: Eric Wong <normalperson@yhbt.net>

 ui-shared.c | 19 ++++++++++++-------


diff --git a/ui-shared.c b/ui-shared.c
index 4818e70983064ac5e1d8d944e530365f240ed63e..c23bc7540b9862bb200afba1bd46b7af7a119896 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -38,14 +38,19 @@ char *cgit_hosturl()
 {
 	char *host, *port;
 
-	host = getenv("SERVER_NAME");
-	if (!host)
-		return NULL;
-	port = getenv("SERVER_PORT");
-	if (port && atoi(port) != 80)
-		host = xstrdup(fmt("%s:%d", host, atoi(port)));
-	else
+	host = getenv("HTTP_HOST");
+	if (host) {
 		host = xstrdup(host);
+	} else {
+		host = getenv("SERVER_NAME");
+		if (!host)
+			return NULL;
+		port = getenv("SERVER_PORT");
+		if (port && atoi(port) != 80)
+			host = xstrdup(fmt("%s:%d", host, atoi(port)));
+		else
+			host = xstrdup(host);
+	}
 	return host;
 }