2015-04-01 02:00:17 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2016-01-13 08:11:23 -06:00
|
|
|
"github.com/go-macaron/gzip"
|
2019-10-15 11:08:06 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2016-01-13 08:38:54 -06:00
|
|
|
"gopkg.in/macaron.v1"
|
2015-04-01 02:00:17 -05:00
|
|
|
)
|
|
|
|
|
2020-03-05 12:44:07 -06:00
|
|
|
const resourcesPath = "/resources"
|
|
|
|
|
2021-03-25 13:36:52 -05:00
|
|
|
var gzipIgnoredPathPrefixes = []string{
|
|
|
|
"/api/datasources/proxy", // Ignore datasource proxy requests.
|
|
|
|
"/api/plugin-proxy/",
|
|
|
|
"/metrics",
|
2021-05-12 10:39:57 -05:00
|
|
|
"/api/live/ws", // WebSocket does not support gzip compression.
|
|
|
|
"/api/live/push", // WebSocket does not support gzip compression.
|
2021-03-25 13:36:52 -05:00
|
|
|
}
|
|
|
|
|
2015-04-01 02:00:17 -05:00
|
|
|
func Gziper() macaron.Handler {
|
2019-10-15 11:08:06 -05:00
|
|
|
gziperLogger := log.New("gziper")
|
|
|
|
gziper := gzip.Gziper()
|
2015-04-01 02:00:17 -05:00
|
|
|
|
|
|
|
return func(ctx *macaron.Context) {
|
|
|
|
requestPath := ctx.Req.URL.RequestURI()
|
|
|
|
|
2021-03-25 13:36:52 -05:00
|
|
|
for _, pathPrefix := range gzipIgnoredPathPrefixes {
|
|
|
|
if strings.HasPrefix(requestPath, pathPrefix) {
|
|
|
|
return
|
|
|
|
}
|
2017-10-23 02:35:46 -05:00
|
|
|
}
|
|
|
|
|
2020-03-05 12:44:07 -06:00
|
|
|
// ignore resources
|
|
|
|
if (strings.HasPrefix(requestPath, "/api/datasources/") || strings.HasPrefix(requestPath, "/api/plugins/")) && strings.Contains(requestPath, resourcesPath) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-15 11:08:06 -05:00
|
|
|
if _, err := ctx.Invoke(gziper); err != nil {
|
|
|
|
gziperLogger.Error("Invoking gzip handler failed", "err", err)
|
|
|
|
}
|
2015-04-01 02:00:17 -05:00
|
|
|
}
|
|
|
|
}
|