validate_host.go 517 B

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