Received: from ola.halmen.xyz (ola.halmen.xyz [88.99.33.217]) by mail-b.sr.ht (Postfix) with ESMTPS id 7B335FF0C5 for <~emersion/public-inbox@lists.sr.ht>; Sat, 21 Nov 2020 01:24:49 +0000 (UTC) Received: from halmen.xyz (77.119.128.148.wireless.dyn.drei.com [77.119.128.148]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: der@halmen.xyz) by ola.halmen.xyz (Postfix) with ESMTPSA id 2F30F805BD; Sat, 21 Nov 2020 02:24:47 +0100 (CET) From: Jonathan Halmen To: ~emersion/public-inbox@lists.sr.ht Cc: Jonathan Halmen Subject: [PATCH kimchi] allow wildcard sites Date: Sat, 21 Nov 2020 02:23:14 +0100 Message-Id: <20201121012312.49346-1-slowjo@halmen.xyz> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit closes: https://todo.sr.ht/~emersion/kimchi/14 --- This might not be the way you want this implemented, but with my limited testing it seems to work. directives.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/directives.go b/directives.go index 96b153c..72a74d4 100644 --- a/directives.go +++ b/directives.go @@ -44,6 +44,11 @@ func parseConfig(srv *Server, cfg scfg.Block) error { func parseSite(srv *Server, dir *scfg.Directive) error { for _, uriStr := range dir.Params { + var wild bool + if strings.Contains(uriStr, "*.") { + uriStr = strings.Replace(uriStr, "*.", "", 1) + wild = true + } if !strings.Contains(uriStr, "//") { uriStr = "//" + uriStr } @@ -114,11 +119,40 @@ func parseSite(srv *Server, dir *scfg.Directive) error { } } - ln.Mux.Handle(pattern, handler) + if wild { + wildcard.Handle(ln, host, handler) + } else { + ln.Mux.Handle(pattern, handler) + } } return nil } +type wildcardHandler map[string]http.Handler + +func (w wildcardHandler) Handle(ln *Listener, host string, handler http.Handler) { + if len(w) == 0 { + ln.Mux.Handle("/", w) + } + w[host] = handler +} + +func (w wildcardHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + if host, _, err := net.SplitHostPort(req.Host); err == nil { + for pattern, handler := range w { + if strings.HasSuffix(host, "." + pattern) { + handler.ServeHTTP(rw, req) + return + } + } + } + http.NotFound(rw, req) +} + +var wildcard = wildcardHandler{} + type parseBackendFunc func(dir *scfg.Directive) (http.Handler, error) var backends = map[string]parseBackendFunc{ -- 2.29.2