middleware.go 937 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package httpd // import "miniflux.app/http/server"
  4. import (
  5. "context"
  6. "net/http"
  7. "miniflux.app/config"
  8. "miniflux.app/http/request"
  9. "miniflux.app/logger"
  10. )
  11. func middleware(next http.Handler) http.Handler {
  12. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. clientIP := request.FindClientIP(r)
  14. ctx := r.Context()
  15. ctx = context.WithValue(ctx, request.ClientIPContextKey, clientIP)
  16. if r.Header.Get("X-Forwarded-Proto") == "https" {
  17. config.Opts.HTTPS = true
  18. }
  19. protocol := "HTTP"
  20. if config.Opts.HTTPS {
  21. protocol = "HTTPS"
  22. }
  23. logger.Debug("[%s] %s %s %s", protocol, clientIP, r.Method, r.RequestURI)
  24. if config.Opts.HTTPS && config.Opts.HasHSTS() {
  25. w.Header().Set("Strict-Transport-Security", "max-age=31536000")
  26. }
  27. next.ServeHTTP(w, r.WithContext(ctx))
  28. })
  29. }