storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/rpc/compression_selector.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Copyright 2012 The Gorilla Authors. All rights reserved.
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE.ORIG file.
     5  //
     6  // Copyright 2020 MinIO, Inc. All rights reserved.
     7  // forked from https://github.com/gorilla/rpc/v2
     8  // modified to be used with MinIO under Apache
     9  // 2.0 license that can be found in the LICENSE file.
    10  package rpc
    11  
    12  import (
    13  	"io"
    14  	"net/http"
    15  	"strings"
    16  	"unicode"
    17  
    18  	"github.com/klauspost/compress/flate"
    19  	"github.com/klauspost/compress/gzip"
    20  )
    21  
    22  // gzipWriter writes and closes the gzip writer.
    23  type gzipWriter struct {
    24  	w *gzip.Writer
    25  }
    26  
    27  func (gw *gzipWriter) Write(p []byte) (n int, err error) {
    28  	defer gw.w.Close()
    29  	return gw.w.Write(p)
    30  }
    31  
    32  // gzipEncoder implements the gzip compressed http encoder.
    33  type gzipEncoder struct {
    34  }
    35  
    36  func (enc *gzipEncoder) Encode(w http.ResponseWriter) io.Writer {
    37  	w.Header().Set("Content-Encoding", "gzip")
    38  	return &gzipWriter{gzip.NewWriter(w)}
    39  }
    40  
    41  // flateWriter writes and closes the flate writer.
    42  type flateWriter struct {
    43  	w *flate.Writer
    44  }
    45  
    46  func (fw *flateWriter) Write(p []byte) (n int, err error) {
    47  	defer fw.w.Close()
    48  	return fw.w.Write(p)
    49  }
    50  
    51  // flateEncoder implements the flate compressed http encoder.
    52  type flateEncoder struct {
    53  }
    54  
    55  func (enc *flateEncoder) Encode(w http.ResponseWriter) io.Writer {
    56  	fw, err := flate.NewWriter(w, flate.DefaultCompression)
    57  	if err != nil {
    58  		return w
    59  	}
    60  	w.Header().Set("Content-Encoding", "deflate")
    61  	return &flateWriter{fw}
    62  }
    63  
    64  // CompressionSelector generates the compressed http encoder.
    65  type CompressionSelector struct {
    66  }
    67  
    68  // Select method selects the correct compression encoder based on http HEADER.
    69  func (*CompressionSelector) Select(r *http.Request) Encoder {
    70  	encHeader := r.Header.Get("Accept-Encoding")
    71  	encTypes := strings.FieldsFunc(encHeader, func(r rune) bool {
    72  		return unicode.IsSpace(r) || r == ','
    73  	})
    74  
    75  	for _, enc := range encTypes {
    76  		switch enc {
    77  		case "gzip":
    78  			return &gzipEncoder{}
    79  		case "deflate":
    80  			return &flateEncoder{}
    81  		}
    82  	}
    83  
    84  	return DefaultEncoder
    85  }