-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
indexer: add Accept-Encoding aware middleware
This commit adds a middleware that examines the incoming "accept-encoding" header and attempt to compress the payload accordingly, and sets the `clair` binary to use it automatically.
- Loading branch information
Showing
4 changed files
with
163 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/klauspost/compress/flate" | ||
"github.com/klauspost/compress/gzip" | ||
"github.com/klauspost/compress/snappy" | ||
"github.com/markusthoemmes/goautoneg" | ||
) | ||
|
||
// Compress wraps the provided http.Handler and provides transparent body | ||
// compression based on a Request's "Accept-Encoding" header. | ||
func Compress(next http.Handler) http.Handler { | ||
h := compressHandler{ | ||
next: next, | ||
} | ||
h.snappy.New = func() interface{} { | ||
return snappy.NewBufferedWriter(nil) | ||
} | ||
h.gzip.New = func() interface{} { | ||
w, _ := gzip.NewWriterLevel(nil, gzip.BestSpeed) | ||
return w | ||
} | ||
h.flate.New = func() interface{} { | ||
w, _ := flate.NewWriter(nil, flate.BestSpeed) | ||
return w | ||
} | ||
|
||
return &h | ||
} | ||
|
||
var _ http.Handler = (*compressHandler)(nil) | ||
|
||
// CompressHandler performs transparent HTTP body compression. | ||
type compressHandler struct { | ||
snappy, gzip, flate sync.Pool | ||
next http.Handler | ||
} | ||
|
||
// Header is an interface that has the http.ResponseWriter's Header-related | ||
// methods. | ||
type header interface { | ||
Header() http.Header | ||
WriteHeader(int) | ||
} | ||
|
||
// ServeHTTP implements http.Handler. | ||
func (c *compressHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
var ( | ||
flusher http.Flusher | ||
pusher http.Pusher | ||
cw io.WriteCloser | ||
) | ||
flusher, _ = w.(http.Flusher) | ||
pusher, _ = w.(http.Pusher) | ||
|
||
// Find the first accept-encoding we support. | ||
for _, a := range goautoneg.ParseAccept(r.Header.Get("accept-encoding")) { | ||
switch a.Type { | ||
case "gzip": | ||
w.Header().Set("content-encoding", "gzip") | ||
gz := c.gzip.Get().(*gzip.Writer) | ||
defer c.gzip.Put(gz) | ||
cw = gz | ||
case "deflate": | ||
w.Header().Set("content-encoding", "deflate") | ||
z := c.flate.Get().(*flate.Writer) | ||
defer c.flate.Put(z) | ||
cw = z | ||
case "snappy": // Nonstandard | ||
w.Header().Set("content-encoding", "snappy") | ||
s := c.snappy.Get().(*snappy.Writer) | ||
defer c.snappy.Put(s) | ||
cw = s | ||
case "identity": | ||
w.Header().Set("content-encoding", "identity") | ||
case "*": | ||
default: | ||
continue | ||
} | ||
break | ||
} | ||
// Do some setup so we can see the error, albeit as a trailer. | ||
if cw != nil { | ||
const errHeader = `clair-error` | ||
w.Header().Add("trailer", errHeader) | ||
defer func() { | ||
if err := cw.Close(); err != nil { | ||
w.Header().Add(errHeader, err.Error()) | ||
} | ||
}() | ||
} | ||
|
||
// Nw is the http.ResponseWriter for our next http.Handler. | ||
var nw http.ResponseWriter | ||
// This is a giant truth table to make anonymous types that satisfy as many | ||
// optional interfaces as possible. | ||
// | ||
// We care about 3 interfaces, so there are 2^3 == 8 combinations | ||
switch { | ||
case flusher == nil && pusher == nil && cw == nil: | ||
nw = w | ||
case flusher == nil && pusher == nil && cw != nil: | ||
nw = struct { | ||
header | ||
io.Writer | ||
}{w, cw} | ||
case flusher == nil && pusher != nil && cw == nil: | ||
nw = struct { | ||
http.ResponseWriter | ||
http.Pusher | ||
}{w, pusher} | ||
case flusher == nil && pusher != nil && cw != nil: | ||
nw = struct { | ||
header | ||
io.Writer | ||
http.Pusher | ||
}{w, cw, pusher} | ||
case flusher != nil && pusher == nil && cw == nil: | ||
nw = struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
}{w, flusher} | ||
case flusher != nil && pusher == nil && cw != nil: | ||
nw = struct { | ||
header | ||
io.Writer | ||
http.Flusher | ||
}{w, cw, flusher} | ||
case flusher != nil && pusher != nil && cw == nil: | ||
nw = struct { | ||
http.ResponseWriter | ||
http.Flusher | ||
http.Pusher | ||
}{w, flusher, pusher} | ||
case flusher != nil && pusher != nil && cw != nil: | ||
nw = struct { | ||
header | ||
io.Writer | ||
http.Flusher | ||
http.Pusher | ||
}{w, cw, flusher, pusher} | ||
default: | ||
panic(fmt.Sprintf("unexpect type combination: %T/%T/%T", flusher, pusher, cw)) | ||
} | ||
c.next.ServeHTTP(nw, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters