validate_host.go 523 Bytes
Newer Older
1 2 3 4 5
package middleware

import (
	"strings"

6
	"github.com/grafana/grafana/pkg/models"
7
	"github.com/grafana/grafana/pkg/setting"
8
	"gopkg.in/macaron.v1"
9 10 11
)

func ValidateHostHeader(domain string) macaron.Handler {
12
	return func(c *models.ReqContext) {
13 14 15 16 17
		// ignore local render calls
		if c.IsRenderCall {
			return
		}

18 19 20 21 22 23 24 25 26 27 28
		h := c.Req.Host
		if i := strings.Index(h, ":"); i >= 0 {
			h = h[:i]
		}

		if !strings.EqualFold(h, domain) {
			c.Redirect(strings.TrimSuffix(setting.AppUrl, "/")+c.Req.RequestURI, 301)
			return
		}
	}
}