From 04adf5fdf53951f270923c41171a52575db53e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Fri, 27 Apr 2018 22:25:00 -0700 Subject: [PATCH] Add middleware to read X-Forwarded-Proto header --- daemon/routes.go | 1 + http/handler/handler.go | 4 ---- middleware/header_config.go | 19 +++++++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 middleware/header_config.go diff --git a/daemon/routes.go b/daemon/routes.go index 74445c34..f11e6d64 100644 --- a/daemon/routes.go +++ b/daemon/routes.go @@ -39,6 +39,7 @@ func routes(cfg *config.Config, store *storage.Storage, feedHandler *feed.Handle router = router.PathPrefix(cfg.BasePath()).Subrouter() } + router.Use(middleware.HeaderConfig) router.Use(middleware.Logging) router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { diff --git a/http/handler/handler.go b/http/handler/handler.go index 59e8acd5..137ff110 100644 --- a/http/handler/handler.go +++ b/http/handler/handler.go @@ -34,10 +34,6 @@ func (h *Handler) Use(f ControllerFunc) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer timer.ExecutionTime(time.Now(), r.URL.Path) - if r.Header.Get("X-Forwarded-Proto") == "https" { - h.cfg.IsHTTPS = true - } - ctx := NewContext(r, h.store, h.router, h.translator) request := NewRequest(r) response := NewResponse(h.cfg, w, r, h.template) diff --git a/middleware/header_config.go b/middleware/header_config.go new file mode 100644 index 00000000..91d18201 --- /dev/null +++ b/middleware/header_config.go @@ -0,0 +1,19 @@ +// Copyright 2018 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package middleware + +import ( + "net/http" +) + +// HeaderConfig changes config values according to HTTP headers. +func (m *Middleware) HeaderConfig(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("X-Forwarded-Proto") == "https" { + m.cfg.IsHTTPS = true + } + next.ServeHTTP(w, r) + }) +}