MM-25095: Handles static JS and CSS using Brotli encoding. (#14524)
* MM-25095: Handles static JS and CSS files using Brotli. * MM-25095: Linting fix. * MM-25095: Adds missing license. * MM-25095: Moves initialization of slice. * MM-25095: Moves initialization of slice.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
6ae9513474
Коммит
d7cb890f34
0
vendor/github.com/NYTimes/gziphandler/.gitignore → vendor/github.com/mkraft/gziphandler/.gitignore
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/.gitignore → vendor/github.com/mkraft/gziphandler/.gitignore
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/.travis.yml → vendor/github.com/mkraft/gziphandler/.travis.yml
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/.travis.yml → vendor/github.com/mkraft/gziphandler/.travis.yml
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/CODE_OF_CONDUCT.md → vendor/github.com/mkraft/gziphandler/CODE_OF_CONDUCT.md
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/CODE_OF_CONDUCT.md → vendor/github.com/mkraft/gziphandler/CODE_OF_CONDUCT.md
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/CONTRIBUTING.md → vendor/github.com/mkraft/gziphandler/CONTRIBUTING.md
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/CONTRIBUTING.md → vendor/github.com/mkraft/gziphandler/CONTRIBUTING.md
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/LICENSE → vendor/github.com/mkraft/gziphandler/LICENSE
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/LICENSE → vendor/github.com/mkraft/gziphandler/LICENSE
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/README.md → vendor/github.com/mkraft/gziphandler/README.md
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/README.md → vendor/github.com/mkraft/gziphandler/README.md
сгенерированный
поставляемый
2
vendor/github.com/NYTimes/gziphandler/go.mod → vendor/github.com/mkraft/gziphandler/go.mod
сгенерированный
поставляемый
2
vendor/github.com/NYTimes/gziphandler/go.mod → vendor/github.com/mkraft/gziphandler/go.mod
сгенерированный
поставляемый
@@ -1,4 +1,4 @@
|
||||
module github.com/NYTimes/gziphandler
|
||||
module github.com/mkraft/gziphandler
|
||||
|
||||
go 1.11
|
||||
|
||||
0
vendor/github.com/NYTimes/gziphandler/go.sum → vendor/github.com/mkraft/gziphandler/go.sum
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/go.sum → vendor/github.com/mkraft/gziphandler/go.sum
сгенерированный
поставляемый
91
vendor/github.com/NYTimes/gziphandler/gzip.go → vendor/github.com/mkraft/gziphandler/gzip.go
сгенерированный
поставляемый
91
vendor/github.com/NYTimes/gziphandler/gzip.go → vendor/github.com/mkraft/gziphandler/gzip.go
сгенерированный
поставляемый
@@ -1,4 +1,4 @@
|
||||
package gziphandler // import "github.com/NYTimes/gziphandler"
|
||||
package gziphandler // import "github.com/mkraft/gziphandler"
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@@ -81,11 +81,13 @@ type GzipResponseWriter struct {
|
||||
|
||||
code int // Saves the WriteHeader value.
|
||||
|
||||
minSize int // Specifed the minimum response size to gzip. If the response length is bigger than this value, it is compressed.
|
||||
minSize int // Specifies the minimum response size to gzip. If the response length is bigger than this value, it is compressed.
|
||||
buf []byte // Holds the first part of the write before reaching the minSize or the end of the write.
|
||||
ignore bool // If true, then we immediately passthru writes to the underlying ResponseWriter.
|
||||
|
||||
contentTypes []parsedContentType // Only compress if the response is one of these content-types. All are accepted if empty.
|
||||
|
||||
contentTypeExceptions []parsedContentType // Only compress if the response is not one of these content-types. All are accepted if empty.
|
||||
}
|
||||
|
||||
type GzipResponseWriterWithCloseNotify struct {
|
||||
@@ -118,7 +120,7 @@ func (w *GzipResponseWriter) Write(b []byte) (int, error) {
|
||||
ce = w.Header().Get(contentEncoding)
|
||||
)
|
||||
// Only continue if they didn't already choose an encoding or a known unhandled content length or type.
|
||||
if ce == "" && (cl == 0 || cl >= w.minSize) && (ct == "" || handleContentType(w.contentTypes, ct)) {
|
||||
if ce == "" && (cl == 0 || cl >= w.minSize) && (ct == "" || handleContentType(w.contentTypes, w.contentTypeExceptions, ct)) {
|
||||
// If the current buffer is less than minSize and a Content-Length isn't set, then wait until we have more data.
|
||||
if len(w.buf) < w.minSize && cl == 0 {
|
||||
return len(b), nil
|
||||
@@ -131,7 +133,7 @@ func (w *GzipResponseWriter) Write(b []byte) (int, error) {
|
||||
w.Header().Set(contentType, ct)
|
||||
}
|
||||
// If the Content-Type is acceptable to GZIP, initialize the GZIP writer.
|
||||
if handleContentType(w.contentTypes, ct) {
|
||||
if handleContentType(w.contentTypes, w.contentTypeExceptions, ct) {
|
||||
if err := w.startGzip(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -324,10 +326,11 @@ func GzipHandlerWithOpts(opts ...option) (func(http.Handler) http.Handler, error
|
||||
w.Header().Add(vary, acceptEncoding)
|
||||
if acceptsGzip(r) {
|
||||
gw := &GzipResponseWriter{
|
||||
ResponseWriter: w,
|
||||
index: index,
|
||||
minSize: c.minSize,
|
||||
contentTypes: c.contentTypes,
|
||||
ResponseWriter: w,
|
||||
index: index,
|
||||
minSize: c.minSize,
|
||||
contentTypes: c.contentTypes,
|
||||
contentTypeExceptions: c.contentTypeExceptions,
|
||||
}
|
||||
defer gw.Close()
|
||||
|
||||
@@ -376,9 +379,10 @@ func (pct parsedContentType) equals(mediaType string, params map[string]string)
|
||||
|
||||
// Used for functional configuration.
|
||||
type config struct {
|
||||
minSize int
|
||||
level int
|
||||
contentTypes []parsedContentType
|
||||
minSize int
|
||||
level int
|
||||
contentTypes []parsedContentType
|
||||
contentTypeExceptions []parsedContentType
|
||||
}
|
||||
|
||||
func (c *config) validate() error {
|
||||
@@ -386,6 +390,10 @@ func (c *config) validate() error {
|
||||
return fmt.Errorf("invalid compression level requested: %d", c.level)
|
||||
}
|
||||
|
||||
if len(c.contentTypes) > 0 && len(c.contentTypeExceptions) > 0 {
|
||||
return fmt.Errorf("ContentTypes and ContentTypeExceptions are mutually exclusive")
|
||||
}
|
||||
|
||||
if c.minSize < 0 {
|
||||
return fmt.Errorf("minimum size must be more than zero")
|
||||
}
|
||||
@@ -411,6 +419,9 @@ func CompressionLevel(level int) option {
|
||||
// the Content-Type header to before compressing. If none
|
||||
// match, the response will be returned as-is.
|
||||
//
|
||||
// ContentTypes cannot be used with ContentTypeExceptions, the options
|
||||
// are mutually exclusive.
|
||||
//
|
||||
// Content types are compared in a case-insensitive, whitespace-ignored
|
||||
// manner.
|
||||
//
|
||||
@@ -437,6 +448,39 @@ func ContentTypes(types []string) option {
|
||||
}
|
||||
}
|
||||
|
||||
// ContentTypeExceptions specifies a list of content types to compare
|
||||
// the Content-Type header to before compressing. If any
|
||||
// match, the response will be returned as-is.
|
||||
//
|
||||
// Content types are compared in a case-insensitive, whitespace-ignored
|
||||
// manner.
|
||||
//
|
||||
// ContentTypeExceptions cannot be used with ContentTypes, the options
|
||||
// are mutually exclusive.
|
||||
//
|
||||
// A MIME type without any other directive will match a content type
|
||||
// that has the same MIME type, regardless of that content type's other
|
||||
// directives. I.e., "text/html" will match both "text/html" and
|
||||
// "text/html; charset=utf-8".
|
||||
//
|
||||
// A MIME type with any other directive will only match a content type
|
||||
// that has the same MIME type and other directives. I.e.,
|
||||
// "text/html; charset=utf-8" will only match "text/html; charset=utf-8".
|
||||
//
|
||||
// By default, responses are gzipped regardless of
|
||||
// Content-Type.
|
||||
func ContentTypeExceptions(types []string) option {
|
||||
return func(c *config) {
|
||||
c.contentTypeExceptions = []parsedContentType{}
|
||||
for _, v := range types {
|
||||
mediaType, params, err := mime.ParseMediaType(v)
|
||||
if err == nil {
|
||||
c.contentTypeExceptions = append(c.contentTypeExceptions, parsedContentType{mediaType, params})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GzipHandler wraps an HTTP handler, to transparently gzip the response body if
|
||||
// the client supports it (via the Accept-Encoding header). This will compress at
|
||||
// the default compression level.
|
||||
@@ -453,9 +497,11 @@ func acceptsGzip(r *http.Request) bool {
|
||||
}
|
||||
|
||||
// returns true if we've been configured to compress the specific content type.
|
||||
func handleContentType(contentTypes []parsedContentType, ct string) bool {
|
||||
// If contentTypes is empty we handle all content types.
|
||||
if len(contentTypes) == 0 {
|
||||
func handleContentType(whitelist, blacklist []parsedContentType, ct string) bool {
|
||||
// If whitelist and blacklist are empty we handle all content types.
|
||||
whiteLen := len(whitelist)
|
||||
blackLen := len(blacklist)
|
||||
if whiteLen == 0 && blackLen == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -464,13 +510,24 @@ func handleContentType(contentTypes []parsedContentType, ct string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, c := range contentTypes {
|
||||
var listToCheck []parsedContentType
|
||||
var whitelistMode bool
|
||||
|
||||
if whiteLen > 0 {
|
||||
whitelistMode = true
|
||||
listToCheck = whitelist
|
||||
} else {
|
||||
listToCheck = blacklist
|
||||
}
|
||||
|
||||
var isInList bool
|
||||
for _, c := range listToCheck {
|
||||
if c.equals(mediaType, params) {
|
||||
return true
|
||||
isInList = true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return (whitelistMode && isInList) || (!whitelistMode && !isInList)
|
||||
}
|
||||
|
||||
// parseEncodings attempts to parse a list of codings, per RFC 2616, as might
|
||||
0
vendor/github.com/NYTimes/gziphandler/gzip_go18.go → vendor/github.com/mkraft/gziphandler/gzip_go18.go
сгенерированный
поставляемый
0
vendor/github.com/NYTimes/gziphandler/gzip_go18.go → vendor/github.com/mkraft/gziphandler/gzip_go18.go
сгенерированный
поставляемый
6
vendor/modules.txt
поставляемый
6
vendor/modules.txt
поставляемый
@@ -1,9 +1,6 @@
|
||||
# github.com/Masterminds/squirrel v1.2.0
|
||||
## explicit
|
||||
github.com/Masterminds/squirrel
|
||||
# github.com/NYTimes/gziphandler v1.1.1
|
||||
## explicit
|
||||
github.com/NYTimes/gziphandler
|
||||
# github.com/armon/go-metrics v0.3.0
|
||||
## explicit
|
||||
github.com/armon/go-metrics
|
||||
@@ -239,6 +236,9 @@ github.com/mitchellh/go-testing-interface
|
||||
# github.com/mitchellh/mapstructure v1.2.3
|
||||
## explicit
|
||||
github.com/mitchellh/mapstructure
|
||||
# github.com/mkraft/gziphandler v1.1.2-0.20200509175700-73dc64f3ad90
|
||||
## explicit
|
||||
github.com/mkraft/gziphandler
|
||||
# github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
|
||||
github.com/modern-go/concurrent
|
||||
# github.com/modern-go/reflect2 v1.0.1
|
||||
|
||||
Ссылка в новой задаче
Block a user