Skip to content

Commit 92c1ce5

Browse files
committed
feat: Improve virtual host extraction using net.SplitHostPort and add detailed logging for host lookups and unfound domains
1 parent 31e01c4 commit 92c1ce5

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

internal/server/server.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package server
22

33
import (
44
"fmt"
5+
"net"
56
"net/http"
67
"os"
7-
"strings"
88
"sync"
99

1010
"github.com/armon/go-radix"
@@ -201,13 +201,22 @@ func startVirtualHostServer(port int, configs []config.SiteConfig, mwManager *mi
201201
serverConf := config.SiteConfig{Port: port}
202202

203203
mainHandler := func(w_ http.ResponseWriter, r_ *http.Request) {
204-
host := r_.Host
205-
if colonIndex := strings.Index(host, ":"); colonIndex != -1 {
206-
host = host[:colonIndex]
204+
host, _, err := net.SplitHostPort(r_.Host)
205+
if err != nil {
206+
// Host might not have a port (e.g. "example.com")
207+
host = r_.Host
207208
}
209+
210+
lg.Debugf("Virtual host lookup for host: '%s' (raw: '%s')", host, r_.Host)
211+
208212
if h, found := radixTree.Get(host); found {
209213
h.(http.Handler).ServeHTTP(w_, r_)
210214
} else {
215+
lg.Warnf("Host NOT FOUND in radix tree: '%s'. Registered domains for this port:", host)
216+
radixTree.Walk(func(s string, v any) bool {
217+
lg.Warnf(" - %s", s)
218+
return false
219+
})
211220
assets.RenderErrorPage(w_, http.StatusNotFound, "Page Not Found", "The page you are looking for does not exist.")
212221
}
213222
}

0 commit comments

Comments
 (0)