middleware.go 986 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package httpd // import "miniflux.app/service/httpd"
  5. import (
  6. "context"
  7. "net/http"
  8. "miniflux.app/config"
  9. "miniflux.app/http/request"
  10. "miniflux.app/logger"
  11. )
  12. func middleware(next http.Handler) http.Handler {
  13. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  14. clientIP := request.FindClientIP(r)
  15. ctx := r.Context()
  16. ctx = context.WithValue(ctx, request.ClientIPContextKey, clientIP)
  17. if r.Header.Get("X-Forwarded-Proto") == "https" {
  18. config.Opts.HTTPS = true
  19. }
  20. protocol := "HTTP"
  21. if config.Opts.HTTPS {
  22. protocol = "HTTPS"
  23. }
  24. logger.Debug("[%s] %s %s %s", protocol, clientIP, r.Method, r.RequestURI)
  25. if config.Opts.HTTPS && config.Opts.HasHSTS() {
  26. w.Header().Set("Strict-Transport-Security", "max-age=31536000")
  27. }
  28. next.ServeHTTP(w, r.WithContext(ctx))
  29. })
  30. }