Skip to content

Commit

Permalink
Revert "Restructure and test http compress package"
Browse files Browse the repository at this point in the history
This reverts commit ebad6ef.
  • Loading branch information
jlelse committed Jun 22, 2024
1 parent ebad6ef commit 45e03e2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 242 deletions.
166 changes: 83 additions & 83 deletions pkgs/httpcompress/httpCompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package httpcompress

import (
"bufio"
"cmp"
"errors"
"io"
"net"
Expand Down Expand Up @@ -58,12 +57,10 @@ type Compressor struct {
func NewCompressor(types ...string) *Compressor {
// If types are provided, set those as the allowed types. If none are
// provided, use the default list.
if len(types) == 0 {
types = defaultCompressibleContentTypes
}

// Build map based on types
allowedTypes := lo.SliceToMap(types, func(t string) (string, any) { return t, nil })
allowedTypes := lo.SliceToMap(
lo.If(len(types) > 0, types).Else(defaultCompressibleContentTypes),
func(t string) (string, any) { return t, nil },
)

c := &Compressor{
pooledEncoders: map[string]*sync.Pool{},
Expand All @@ -76,19 +73,6 @@ func NewCompressor(types ...string) *Compressor {
return c
}

// Interface for types that allow resetting io.Writers.
type compressWriter interface {
io.Writer
Reset(w io.Writer)
Flush() error
}

// An EncoderFunc is a function that wraps the provided io.Writer with a
// streaming compression algorithm and returns it.
//
// In case of failure, the function should return nil.
type EncoderFunc func(w io.Writer) compressWriter

// SetEncoder can be used to set the implementation of a compression algorithm.
//
// The encoding should be a standardised identifier. See:
Expand All @@ -112,109 +96,125 @@ func (c *Compressor) SetEncoder(encoding string, fn EncoderFunc) {
return fn(io.Discard)
},
}

c.encodingPrecedence = append([]string{encoding}, c.encodingPrecedence...)
}

type compressResponseWriter struct {
http.ResponseWriter // The response writer to delegate to.
encoding string // The accepted encoding.
encoder compressWriter // The encoder to use.
cleanup func() // Cleanup function to reset and repool encoder.
compressor *Compressor // Holds the compressor configuration.
wroteHeader bool // Whether the header has been written.
}

func (c *Compressor) findAcceptedEncoding(r *http.Request) string {
accepted := strings.Split(strings.ToLower(strings.ReplaceAll(r.Header.Get("Accept-Encoding"), " ", "")), ",")
for _, name := range c.encodingPrecedence {
if slices.Contains(accepted, name) {
// We found accepted encoding
if _, ok := c.pooledEncoders[name]; ok {
// And it also exists a pool for the encoder, we can use it
return name
}
}
}
return ""
}

func (cw *compressResponseWriter) doCleanup() {
if cw.cleanup != nil {
cw.cleanup()
cw.cleanup = nil
}
}

// Handler returns a new middleware that will compress the response based on the
// current Compressor.
func (c *Compressor) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
encoding := c.findAcceptedEncoding(r)
if encoding == "" {
// No encoding accepted, serve directly
next.ServeHTTP(w, r)
return
}
cw := &compressResponseWriter{
encoding: encoding,
compressor: c,
ResponseWriter: w,
request: r,
}
next.ServeHTTP(cw, r)
_ = cw.Close()
cw.doCleanup()
})
}

// An EncoderFunc is a function that wraps the provided io.Writer with a
// streaming compression algorithm and returns it.
//
// In case of failure, the function should return nil.
type EncoderFunc func(w io.Writer) compressWriter

// Interface for types that allow resetting io.Writers.
type compressWriter interface {
io.Writer
Reset(w io.Writer)
Flush() error
}

type compressResponseWriter struct {
http.ResponseWriter // The response writer to delegate to.
encoder compressWriter // The encoder to use (if any).
cleanup func() // Cleanup function to reset and repool encoder.
compressor *Compressor // Holds the compressor configuration.
request *http.Request // The request that is being handled.
wroteHeader bool // Whether the header has been written.
}

func (cw *compressResponseWriter) isCompressable() bool {
_, ok := cw.compressor.allowedTypes[strings.SplitN(cw.Header().Get("Content-Type"), ";", 2)[0]]
// Parse the first part of the Content-Type response header.
contentType := cw.Header().Get("Content-Type")
if idx := strings.Index(contentType, ";"); idx >= 0 {
contentType = contentType[0:idx]
}

// Is the content type compressable?
_, ok := cw.compressor.allowedTypes[contentType]
return ok
}

func (cw *compressResponseWriter) enableEncoder() {
pool := cw.compressor.pooledEncoders[cw.encoding]
cw.encoder = pool.Get().(compressWriter)
if cw.encoder == nil {
return
func (cw *compressResponseWriter) writer() io.Writer {
if cw.encoder != nil {
return cw.encoder
}
cw.cleanup = func() {
encoder := cw.encoder
return cw.ResponseWriter
}

// selectEncoder returns the encoder, the name of the encoder, and a closer function.
func (cw *compressResponseWriter) selectEncoder() (compressWriter, string, func()) {
// Parse the names of all accepted algorithms from the header.
accepted := strings.Split(strings.ToLower(strings.ReplaceAll(cw.request.Header.Get("Accept-Encoding"), " ", "")), ",")

// Find supported encoder by accepted list by precedence
for _, name := range cw.compressor.encodingPrecedence {
if slices.Contains(accepted, name) {
if pool, ok := cw.compressor.pooledEncoders[name]; ok {
encoder := pool.Get().(compressWriter)
cleanup := func() {
encoder.Reset(nil)
pool.Put(encoder)
}
encoder.Reset(cw.ResponseWriter)
return encoder, name, cleanup
}
}
}

// No encoder found to match the accepted encoding
return nil, "", nil
}

func (cw *compressResponseWriter) doCleanup() {
if cw.encoder != nil {
cw.encoder = nil
encoder.Reset(nil)
pool.Put(encoder)
cw.cleanup()
cw.cleanup = nil
}
cw.encoder.Reset(cw.ResponseWriter)
}

func (cw *compressResponseWriter) WriteHeader(code int) {
defer cw.ResponseWriter.WriteHeader(code)

if cw.wroteHeader {
return
}

defer cw.ResponseWriter.WriteHeader(code)
cw.wroteHeader = true

if cw.Header().Get("Content-Encoding") != "" || !cw.isCompressable() {
// Data has already been compressed or is not compressable.
if cw.Header().Get("Content-Encoding") != "" {
// Data has already been compressed.
return
}

// Enable encoding
cw.enableEncoder()
if cw.encoder == nil {
if !cw.isCompressable() {
// Data is not compressable.
return
}

cw.Header().Set("Content-Encoding", cw.encoding)
cw.Header().Add("Vary", "Accept-Encoding")

// The content-length after compression is unknown
cw.Header().Del("Content-Length")
}
var encoding string
cw.encoder, encoding, cw.cleanup = cw.selectEncoder()
if encoding != "" {
cw.Header().Set("Content-Encoding", encoding)
cw.Header().Add("Vary", "Accept-Encoding")

func (cw *compressResponseWriter) writer() io.Writer {
return cmp.Or[io.Writer](cw.encoder, cw.ResponseWriter)
// The content-length after compression is unknown
cw.Header().Del("Content-Length")
}
}

func (cw *compressResponseWriter) Write(p []byte) (int, error) {
Expand Down
159 changes: 0 additions & 159 deletions pkgs/httpcompress/httpCompress_test.go

This file was deleted.

0 comments on commit 45e03e2

Please sign in to comment.