Github user alficles commented on a diff in the pull request:
https://github.com/apache/incubator-trafficcontrol/pull/425#discussion_r111799950
--- Diff: traffic_monitor_golang/traffic_monitor/datareq/datareq.go ---
@@ -235,3 +276,39 @@ func addTrailingSlashEndpoints(dispatchMap
map[string]http.HandlerFunc) map[stri
}
return dispatchMap
}
+
+func acceptsGzip(r *http.Request) bool {
+ encodingHeaders := r.Header["Accept-Encoding"] // headers are
case-insensitive, but Go promises to Canonical-Case requests
+ for _, encodingHeader := range encodingHeaders {
+ encodingHeader := strings.Replace(encodingHeader, " ", "", -1)
+ encodings := strings.Split(encodingHeader, ",")
+ for _, encoding := range encodings {
+ if strings.ToLower(encoding) == "gzip" { // encoding is
case-insensitive, per the RFC
+ return true
+ }
+ }
+ }
+ return false
+}
+
+// gzipIfAccepts gzips the given bytes, writes a `Content-Encoding: gzip`
header to the given writer, and returns the gzipped bytes, if the Request
supports GZip (has an Accept-Encoding header). Else, returns the bytes
unmodified. Note the given bytes are NOT written to the given writer. It is
assumed the bytes may need to pass thru other middleware before being written.
+func gzipIfAccepts(r *http.Request, w http.ResponseWriter, b []byte)
([]byte, error) {
+ // TODO this could be made more efficient by wrapping ResponseWriter
with the GzipWriter, and letting callers writer directly to it - but then we'd
have to deal with Closing the gzip.Writer.
+ if len(b) == 0 || !acceptsGzip(r) {
--- End diff --
Since we're not using interfaces for composability, we've got the length.
Why bother compressing very small strings? Maybe set the minimum compression
length at some appropriate non-zero number?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---