validate_host.go 412 B

12345678910111213141516171819202122
  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 *macaron.Context) {
  9. h := c.Req.Host
  10. if i := strings.Index(h, ":"); i >= 0 {
  11. h = h[:i]
  12. }
  13. if !strings.EqualFold(h, domain) {
  14. c.Redirect(strings.TrimSuffix(setting.AppUrl, "/")+c.Req.RequestURI, 301)
  15. return
  16. }
  17. }
  18. }