miniflux/internal/http/server/middleware.go

39 lines
985 B
Go
Raw Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
2018-11-12 19:23:39 +01:00
package httpd // import "miniflux.app/v2/internal/http/server"
2018-11-12 19:23:39 +01:00
import (
"context"
"net/http"
"miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/http/request"
"miniflux.app/v2/internal/logger"
2018-11-12 19:23:39 +01:00
)
func middleware(next http.Handler) http.Handler {
2018-11-12 19:23:39 +01:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
clientIP := request.FindClientIP(r)
ctx := r.Context()
ctx = context.WithValue(ctx, request.ClientIPContextKey, clientIP)
if r.Header.Get("X-Forwarded-Proto") == "https" {
config.Opts.HTTPS = true
2018-11-12 19:23:39 +01:00
}
protocol := "HTTP"
if config.Opts.HTTPS {
2018-11-12 19:23:39 +01:00
protocol = "HTTPS"
}
logger.Debug("[%s] %s %s %s", protocol, clientIP, r.Method, r.RequestURI)
if config.Opts.HTTPS && config.Opts.HasHSTS() {
2018-11-12 19:23:39 +01:00
w.Header().Set("Strict-Transport-Security", "max-age=31536000")
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}