github.com/aacfactory/fns@v1.2.85/transports/middlewares/compress/kind.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package compress
    19  
    20  import (
    21  	"github.com/aacfactory/fns/commons/bytex"
    22  	"github.com/aacfactory/fns/transports"
    23  )
    24  
    25  const (
    26  	AnyName     = "*"
    27  	DefaultName = "compress"
    28  	GzipName    = "gzip"
    29  	DeflateName = "deflate"
    30  	BrotliName  = "br"
    31  )
    32  
    33  const (
    34  	No Kind = iota
    35  	Any
    36  	Default
    37  	Gzip
    38  	Deflate
    39  	Brotli
    40  )
    41  
    42  type Kind int
    43  
    44  func (kind Kind) String() string {
    45  	switch kind {
    46  	case Default:
    47  		return DefaultName
    48  	case Deflate:
    49  		return DeflateName
    50  	case Gzip:
    51  		return GzipName
    52  	case Brotli:
    53  		return BrotliName
    54  	case Any:
    55  		return AnyName
    56  	default:
    57  		return ""
    58  	}
    59  }
    60  
    61  func getKind(r transports.Request) Kind {
    62  	accepts := transports.GetAcceptEncodings(r.Header())
    63  	acceptsLen := len(accepts)
    64  	if acceptsLen == 0 {
    65  		return No
    66  	}
    67  	for i := acceptsLen - 1; i >= 0; i-- {
    68  		accept := accepts[i]
    69  		switch bytex.ToString(accept.Name) {
    70  		case DefaultName:
    71  			return Default
    72  		case GzipName:
    73  			return Gzip
    74  		case DeflateName:
    75  			return Deflate
    76  		case BrotliName:
    77  			return Brotli
    78  		case AnyName:
    79  			return Any
    80  		default:
    81  			break
    82  		}
    83  	}
    84  	return No
    85  }