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

[cgit PATCH] use Host: header to generate cgit_hosturl

From
EWEric Wong <normalperson@yhbt.net>
Date
Sep 1, 2008, 06:30 UTC
Message-ID
<20080901063033.GA21848@untitled>

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 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/ui-shared.c b/ui-shared.c
index 37c60b2..5a848c1 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;
 }
 
-- 
Eric Wong
Next: Lars Hjemli
Message 1 of 6 in “use Host: header to generate cgit_hosturl”
  1. use Host: header to generate cgit_hosturlEric Wong, Sep 1, 2008
  2. Lars HjemliSep 1, 2008
  3. Eric WongSep 1, 2008
  4. Lars HjemliSep 1, 2008
  5. Eric WongSep 1, 2008
  6. Lars HjemliSep 2, 2008

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.