From 11beddcfb12be8edbe1d8b52ec7718fa25a7835c Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Sat, 6 Jun 2020 13:36:10 +0200 Subject: [PATCH] Add rudimentary support for specifying redirects. --- config.go | 2 ++ handler.go | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/config.go b/config.go index 7acd510..4735fc6 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,7 @@ type Config struct { DocBase string HomeDocBase string LogPath string + Redirects map[string]string CGIPath string SCGIPaths map[string]string } @@ -28,6 +29,7 @@ func getConfig(filename string) (Config, error) { config.DocBase = "/var/gemini/" config.HomeDocBase = "users" config.LogPath = "molly.log" + config.Redirects = make(map[string]string) config.CGIPath = "^/var/gemini/cgi-bin/" config.SCGIPaths = make(map[string]string) diff --git a/handler.go b/handler.go index edf3f86..0677a39 100644 --- a/handler.go +++ b/handler.go @@ -72,6 +72,16 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry) return } + // Check for redirects + for src, dst := range config.Redirects { + if URL.Path == src { + URL.Path = dst + conn.Write([]byte("30 " + URL.String() + "\r\n")) + log.Status = 30 + return + } + } + // Check whether this URL is mapped to an SCGI app for scgi_url, scgi_socket := range config.SCGIPaths { matched, err := regexp.Match(scgi_url, []byte(URL.Path))