refactor: simplify logging of Host header since net/http doesn't accept requests without a Host header, there's no need for a fallback
Alan Pearce alan@alanpearce.eu
Wed, 22 May 2024 18:58:50 +0200
2 files changed, 2 insertions(+), 11 deletions(-)
M internal/server/logging.go → internal/server/logging.go
@@ -22,20 +22,13 @@ func NewLoggingResponseWriter(w http.ResponseWriter) *LoggingResponseWriter { return &LoggingResponseWriter{w, http.StatusOK} } -type wrappedHandlerOptions struct { - defaultHostname string -} - -func wrapHandlerWithLogging(wrappedHandler http.Handler, opts wrappedHandlerOptions) http.Handler { +func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { scheme := r.Header.Get("X-Forwarded-Proto") if scheme == "" { scheme = "http" } host := r.Host - if host == "" { - host = opts.defaultHostname - } lw := NewLoggingResponseWriter(w) wrappedHandler.ServeHTTP(lw, r) statusCode := lw.statusCode
M internal/server/server.go → internal/server/server.go
@@ -107,9 +107,7 @@ Addr: listenAddress, ReadHeaderTimeout: 1 * time.Minute, Handler: sentryHandler.Handle( serverHeaderHandler( - wrapHandlerWithLogging(top, wrappedHandlerOptions{ - defaultHostname: runtimeConfig.BaseURL.Hostname(), - }), + wrapHandlerWithLogging(top), ), ), },