validate_host.go 472 B

123456789101112131415161718192021222324252627
  1. package middleware
  2. import (
  3. "strings"
  4. "github.com/grafana/grafana/pkg/setting"
  5. "gopkg.in/macaron.v1"
  6. )
  7. func ValidateHostHeader(domain string) macaron.Handler {
  8. return func(c *Context) {
  9. // ignore local render calls
  10. if c.IsRenderCall {
  11. return
  12. }
  13. h := c.Req.Host
  14. if i := strings.Index(h, ":"); i >= 0 {
  15. h = h[:i]
  16. }
  17. if !strings.EqualFold(h, domain) {
  18. c.Redirect(strings.TrimSuffix(setting.AppUrl, "/")+c.Req.RequestURI, 301)
  19. return
  20. }
  21. }
  22. }