pax_global_header00006660000000000000000000000064142607151550014520gustar00rootroot0000000000000052 comment=eba7ac8f9279b53dc43224e2d66647a77896b7f1 gzip-0.0.6/000077500000000000000000000000001426071515500124745ustar00rootroot00000000000000gzip-0.0.6/.github/000077500000000000000000000000001426071515500140345ustar00rootroot00000000000000gzip-0.0.6/.github/dependabot.yml000066400000000000000000000003031426071515500166600ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: github-actions directory: / schedule: interval: weekly - package-ecosystem: gomod directory: / schedule: interval: weekly gzip-0.0.6/.github/workflows/000077500000000000000000000000001426071515500160715ustar00rootroot00000000000000gzip-0.0.6/.github/workflows/codeql.yml000066400000000000000000000033121426071515500200620ustar00rootroot00000000000000# For most projects, this workflow file will not need changing; you simply need # to commit it to your repository. # # You may wish to alter this file to override the set of languages analyzed, # or to provide custom queries or build logic. # # ******** NOTE ******** # We have attempted to detect the languages in your repository. Please check # the `language` matrix defined below to confirm you have the correct set of # supported CodeQL languages. # name: "CodeQL" on: push: branches: [ master ] pull_request: # The branches below must be a subset of the branches above branches: [ master ] schedule: - cron: '41 23 * * 6' jobs: analyze: name: Analyze runs-on: ubuntu-latest permissions: actions: read contents: read security-events: write strategy: fail-fast: false matrix: language: [ 'go' ] # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] # Learn more about CodeQL language support at https://git.io/codeql-language-support steps: - name: Checkout repository uses: actions/checkout@v3 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. # By default, queries listed here will override any specified in a config file. # Prefix the list here with "+" to use these queries and those in the config file. # queries: ./path/to/local/query, your-org/your-repo/queries@main - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v2 gzip-0.0.6/.github/workflows/go.yml000066400000000000000000000031561426071515500172260ustar00rootroot00000000000000name: Run Tests on: push: branches: - master pull_request: branches: - master jobs: lint: runs-on: ubuntu-latest steps: - name: Setup go uses: actions/setup-go@v3 with: go-version: '^1.16' - name: Checkout repository uses: actions/checkout@v3 - name: Setup golangci-lint uses: golangci/golangci-lint-action@v3 with: version: v1.45.0 args: --verbose test: strategy: matrix: os: [ubuntu-latest] go: [1.14, 1.15, 1.16, 1.17, 1.18] include: - os: ubuntu-latest go-build: ~/.cache/go-build - os: macos-latest go-build: ~/Library/Caches/go-build name: ${{ matrix.os }} @ Go ${{ matrix.go }} runs-on: ${{ matrix.os }} env: GO111MODULE: on GOPROXY: https://proxy.golang.org steps: - name: Set up Go ${{ matrix.go }} uses: actions/setup-go@v3 with: go-version: ${{ matrix.go }} - name: Checkout Code uses: actions/checkout@v3 with: ref: ${{ github.ref }} - uses: actions/cache@v3 with: path: | ${{ matrix.go-build }} ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- - name: Run Tests run: | go test -v -covermode=atomic -coverprofile=coverage.out - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: flags: ${{ matrix.os }},go-${{ matrix.go }} gzip-0.0.6/.gitignore000066400000000000000000000000151426071515500144600ustar00rootroot00000000000000coverage.out gzip-0.0.6/.golangci.yml000066400000000000000000000013341426071515500150610ustar00rootroot00000000000000linters: enable-all: false disable-all: true fast: false enable: - bodyclose - deadcode - depguard - dogsled - dupl - errcheck - exportloopref - exhaustive - gochecknoinits - goconst - gocritic - gocyclo - gofmt - goimports - goprintffuncname - gosec - gosimple - govet - ineffassign - lll - misspell - nakedret - noctx - nolintlint - rowserrcheck - staticcheck - structcheck - stylecheck - typecheck - unconvert - unparam - unused - varcheck - whitespace - gofumpt run: timeout: 3m linters-settings: dupl: # tokens count to trigger issue, 150 by default threshold: 200 gzip-0.0.6/LICENSE000066400000000000000000000020521426071515500135000ustar00rootroot00000000000000MIT License Copyright (c) 2017 Gin-Gonic Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. gzip-0.0.6/README.md000066400000000000000000000054221426071515500137560ustar00rootroot00000000000000# GZIP gin's middleware [![Run Tests](https://github.com/gin-contrib/gzip/actions/workflows/go.yml/badge.svg)](https://github.com/gin-contrib/gzip/actions/workflows/go.yml) [![codecov](https://codecov.io/gh/gin-contrib/gzip/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/gzip) [![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/gzip)](https://goreportcard.com/report/github.com/gin-contrib/gzip) [![GoDoc](https://godoc.org/github.com/gin-contrib/gzip?status.svg)](https://godoc.org/github.com/gin-contrib/gzip) [![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin) Gin middleware to enable `GZIP` support. ## Usage Download and install it: ```sh go get github.com/gin-contrib/gzip ``` Import it in your code: ```go import "github.com/gin-contrib/gzip" ``` Canonical example: ```go package main import ( "fmt" "net/http" "time" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.Use(gzip.Gzip(gzip.DefaultCompression)) r.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) }) // Listen and Server in 0.0.0.0:8080 if err := r.Run(":8080"); err != nil { log.Fatal(err) } } ``` Customized Excluded Extensions ```go package main import ( "fmt" "net/http" "time" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedExtensions([]string{".pdf", ".mp4"}))) r.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) }) // Listen and Server in 0.0.0.0:8080 if err := r.Run(":8080"); err != nil { log.Fatal(err) } } ``` Customized Excluded Paths ```go package main import ( "fmt" "net/http" "time" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"}))) r.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) }) // Listen and Server in 0.0.0.0:8080 if err := r.Run(":8080"); err != nil { log.Fatal(err) } } ``` Customized Excluded Paths ```go package main import ( "fmt" "net/http" "time" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPathsRegexs([]string{".*"}))) r.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) }) // Listen and Server in 0.0.0.0:8080 if err := r.Run(":8080"); err != nil { log.Fatal(err) } } ``` gzip-0.0.6/example/000077500000000000000000000000001426071515500141275ustar00rootroot00000000000000gzip-0.0.6/example/example.go000066400000000000000000000006261426071515500161150ustar00rootroot00000000000000package main import ( "fmt" "log" "net/http" "time" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.Use(gzip.Gzip(gzip.DefaultCompression)) r.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) }) // Listen and Server in 0.0.0.0:8080 if err := r.Run(":8080"); err != nil { log.Fatal(err) } } gzip-0.0.6/go.mod000066400000000000000000000001761426071515500136060ustar00rootroot00000000000000module github.com/gin-contrib/gzip require ( github.com/gin-gonic/gin v1.8.1 github.com/stretchr/testify v1.8.0 ) go 1.13 gzip-0.0.6/go.sum000066400000000000000000000200441426071515500136270ustar00rootroot00000000000000github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8= github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0= github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM= github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU= github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gzip-0.0.6/gzip.go000066400000000000000000000015151426071515500137760ustar00rootroot00000000000000package gzip import ( "compress/gzip" "github.com/gin-gonic/gin" ) const ( BestCompression = gzip.BestCompression BestSpeed = gzip.BestSpeed DefaultCompression = gzip.DefaultCompression NoCompression = gzip.NoCompression ) func Gzip(level int, options ...Option) gin.HandlerFunc { return newGzipHandler(level, options...).Handle } type gzipWriter struct { gin.ResponseWriter writer *gzip.Writer } func (g *gzipWriter) WriteString(s string) (int, error) { g.Header().Del("Content-Length") return g.writer.Write([]byte(s)) } func (g *gzipWriter) Write(data []byte) (int, error) { g.Header().Del("Content-Length") return g.writer.Write(data) } // Fix: https://github.com/mholt/caddy/issues/38 func (g *gzipWriter) WriteHeader(code int) { g.Header().Del("Content-Length") g.ResponseWriter.WriteHeader(code) } gzip-0.0.6/gzip_test.go000066400000000000000000000161551426071515500150430ustar00rootroot00000000000000package gzip import ( "bytes" "compress/gzip" "context" "fmt" "io/ioutil" "net/http" "net/http/httptest" "net/http/httputil" "net/url" "strconv" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" ) const ( testResponse = "Gzip Test Response " testReverseResponse = "Gzip Test Reverse Response " ) type rServer struct{} func (s *rServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { fmt.Fprint(rw, testReverseResponse) } type closeNotifyingRecorder struct { *httptest.ResponseRecorder closed chan bool } func newCloseNotifyingRecorder() *closeNotifyingRecorder { return &closeNotifyingRecorder{ httptest.NewRecorder(), make(chan bool, 1), } } func (c *closeNotifyingRecorder) CloseNotify() <-chan bool { return c.closed } func newServer() *gin.Engine { // init reverse proxy server rServer := httptest.NewServer(new(rServer)) target, _ := url.Parse(rServer.URL) rp := httputil.NewSingleHostReverseProxy(target) router := gin.New() router.Use(Gzip(DefaultCompression)) router.GET("/", func(c *gin.Context) { c.Header("Content-Length", strconv.Itoa(len(testResponse))) c.String(200, testResponse) }) router.Any("/reverse", func(c *gin.Context) { rp.ServeHTTP(c.Writer, c.Request) }) return router } func TestGzip(t *testing.T) { req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil) req.Header.Add("Accept-Encoding", "gzip") w := httptest.NewRecorder() r := newServer() r.ServeHTTP(w, req) assert.Equal(t, w.Code, 200) assert.Equal(t, w.Header().Get("Content-Encoding"), "gzip") assert.Equal(t, w.Header().Get("Vary"), "Accept-Encoding") assert.NotEqual(t, w.Header().Get("Content-Length"), "0") assert.NotEqual(t, w.Body.Len(), 19) assert.Equal(t, fmt.Sprint(w.Body.Len()), w.Header().Get("Content-Length")) gr, err := gzip.NewReader(w.Body) assert.NoError(t, err) defer gr.Close() body, _ := ioutil.ReadAll(gr) assert.Equal(t, string(body), testResponse) } func TestGzipPNG(t *testing.T) { req, _ := http.NewRequestWithContext(context.Background(), "GET", "/image.png", nil) req.Header.Add("Accept-Encoding", "gzip") router := gin.New() router.Use(Gzip(DefaultCompression)) router.GET("/image.png", func(c *gin.Context) { c.String(200, "this is a PNG!") }) w := httptest.NewRecorder() router.ServeHTTP(w, req) assert.Equal(t, w.Code, 200) assert.Equal(t, w.Header().Get("Content-Encoding"), "") assert.Equal(t, w.Header().Get("Vary"), "") assert.Equal(t, w.Body.String(), "this is a PNG!") } func TestExcludedExtensions(t *testing.T) { req, _ := http.NewRequestWithContext(context.Background(), "GET", "/index.html", nil) req.Header.Add("Accept-Encoding", "gzip") router := gin.New() router.Use(Gzip(DefaultCompression, WithExcludedExtensions([]string{".html"}))) router.GET("/index.html", func(c *gin.Context) { c.String(200, "this is a HTML!") }) w := httptest.NewRecorder() router.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "", w.Header().Get("Content-Encoding")) assert.Equal(t, "", w.Header().Get("Vary")) assert.Equal(t, "this is a HTML!", w.Body.String()) assert.Equal(t, "", w.Header().Get("Content-Length")) } func TestExcludedPaths(t *testing.T) { req, _ := http.NewRequestWithContext(context.Background(), "GET", "/api/books", nil) req.Header.Add("Accept-Encoding", "gzip") router := gin.New() router.Use(Gzip(DefaultCompression, WithExcludedPaths([]string{"/api/"}))) router.GET("/api/books", func(c *gin.Context) { c.String(200, "this is books!") }) w := httptest.NewRecorder() router.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "", w.Header().Get("Content-Encoding")) assert.Equal(t, "", w.Header().Get("Vary")) assert.Equal(t, "this is books!", w.Body.String()) assert.Equal(t, "", w.Header().Get("Content-Length")) } func TestNoGzip(t *testing.T) { req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil) w := httptest.NewRecorder() r := newServer() r.ServeHTTP(w, req) assert.Equal(t, w.Code, 200) assert.Equal(t, w.Header().Get("Content-Encoding"), "") assert.Equal(t, w.Header().Get("Content-Length"), "19") assert.Equal(t, w.Body.String(), testResponse) } func TestGzipWithReverseProxy(t *testing.T) { req, _ := http.NewRequestWithContext(context.Background(), "GET", "/reverse", nil) req.Header.Add("Accept-Encoding", "gzip") w := newCloseNotifyingRecorder() r := newServer() r.ServeHTTP(w, req) assert.Equal(t, w.Code, 200) assert.Equal(t, w.Header().Get("Content-Encoding"), "gzip") assert.Equal(t, w.Header().Get("Vary"), "Accept-Encoding") assert.NotEqual(t, w.Header().Get("Content-Length"), "0") assert.NotEqual(t, w.Body.Len(), 19) assert.Equal(t, fmt.Sprint(w.Body.Len()), w.Header().Get("Content-Length")) gr, err := gzip.NewReader(w.Body) assert.NoError(t, err) defer gr.Close() body, _ := ioutil.ReadAll(gr) assert.Equal(t, string(body), testReverseResponse) } func TestDecompressGzip(t *testing.T) { buf := &bytes.Buffer{} gz, _ := gzip.NewWriterLevel(buf, gzip.DefaultCompression) if _, err := gz.Write([]byte(testResponse)); err != nil { gz.Close() t.Fatal(err) } gz.Close() req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf) req.Header.Add("Content-Encoding", "gzip") router := gin.New() router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle))) router.POST("/", func(c *gin.Context) { if v := c.Request.Header.Get("Content-Encoding"); v != "" { t.Errorf("unexpected `Content-Encoding`: %s header", v) } if v := c.Request.Header.Get("Content-Length"); v != "" { t.Errorf("unexpected `Content-Length`: %s header", v) } data, err := c.GetRawData() if err != nil { t.Fatal(err) } c.Data(200, "text/plain", data) }) w := httptest.NewRecorder() router.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "", w.Header().Get("Content-Encoding")) assert.Equal(t, "", w.Header().Get("Vary")) assert.Equal(t, testResponse, w.Body.String()) assert.Equal(t, "", w.Header().Get("Content-Length")) } func TestDecompressGzipWithEmptyBody(t *testing.T) { req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", nil) req.Header.Add("Content-Encoding", "gzip") router := gin.New() router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle))) router.POST("/", func(c *gin.Context) { c.String(200, "ok") }) w := httptest.NewRecorder() router.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "", w.Header().Get("Content-Encoding")) assert.Equal(t, "", w.Header().Get("Vary")) assert.Equal(t, "ok", w.Body.String()) assert.Equal(t, "", w.Header().Get("Content-Length")) } func TestDecompressGzipWithIncorrectData(t *testing.T) { req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", bytes.NewReader([]byte(testResponse))) req.Header.Add("Content-Encoding", "gzip") router := gin.New() router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle))) router.POST("/", func(c *gin.Context) { c.String(200, "ok") }) w := httptest.NewRecorder() router.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) } gzip-0.0.6/handler.go000066400000000000000000000031531426071515500144420ustar00rootroot00000000000000package gzip import ( "compress/gzip" "fmt" "io/ioutil" "net/http" "path/filepath" "strings" "sync" "github.com/gin-gonic/gin" ) type gzipHandler struct { *Options gzPool sync.Pool } func newGzipHandler(level int, options ...Option) *gzipHandler { handler := &gzipHandler{ Options: DefaultOptions, gzPool: sync.Pool{ New: func() interface{} { gz, err := gzip.NewWriterLevel(ioutil.Discard, level) if err != nil { panic(err) } return gz }, }, } for _, setter := range options { setter(handler.Options) } return handler } func (g *gzipHandler) Handle(c *gin.Context) { if fn := g.DecompressFn; fn != nil && c.Request.Header.Get("Content-Encoding") == "gzip" { fn(c) } if !g.shouldCompress(c.Request) { return } gz := g.gzPool.Get().(*gzip.Writer) defer g.gzPool.Put(gz) defer gz.Reset(ioutil.Discard) gz.Reset(c.Writer) c.Header("Content-Encoding", "gzip") c.Header("Vary", "Accept-Encoding") c.Writer = &gzipWriter{c.Writer, gz} defer func() { gz.Close() c.Header("Content-Length", fmt.Sprint(c.Writer.Size())) }() c.Next() } func (g *gzipHandler) shouldCompress(req *http.Request) bool { if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") || strings.Contains(req.Header.Get("Connection"), "Upgrade") || strings.Contains(req.Header.Get("Accept"), "text/event-stream") { return false } extension := filepath.Ext(req.URL.Path) if g.ExcludedExtensions.Contains(extension) { return false } if g.ExcludedPaths.Contains(req.URL.Path) { return false } if g.ExcludedPathesRegexs.Contains(req.URL.Path) { return false } return true } gzip-0.0.6/options.go000066400000000000000000000045351426071515500145250ustar00rootroot00000000000000package gzip import ( "compress/gzip" "net/http" "regexp" "strings" "github.com/gin-gonic/gin" ) var ( DefaultExcludedExtentions = NewExcludedExtensions([]string{ ".png", ".gif", ".jpeg", ".jpg", }) DefaultOptions = &Options{ ExcludedExtensions: DefaultExcludedExtentions, } ) type Options struct { ExcludedExtensions ExcludedExtensions ExcludedPaths ExcludedPaths ExcludedPathesRegexs ExcludedPathesRegexs DecompressFn func(c *gin.Context) } type Option func(*Options) func WithExcludedExtensions(args []string) Option { return func(o *Options) { o.ExcludedExtensions = NewExcludedExtensions(args) } } func WithExcludedPaths(args []string) Option { return func(o *Options) { o.ExcludedPaths = NewExcludedPaths(args) } } func WithExcludedPathsRegexs(args []string) Option { return func(o *Options) { o.ExcludedPathesRegexs = NewExcludedPathesRegexs(args) } } func WithDecompressFn(decompressFn func(c *gin.Context)) Option { return func(o *Options) { o.DecompressFn = decompressFn } } // Using map for better lookup performance type ExcludedExtensions map[string]bool func NewExcludedExtensions(extensions []string) ExcludedExtensions { res := make(ExcludedExtensions) for _, e := range extensions { res[e] = true } return res } func (e ExcludedExtensions) Contains(target string) bool { _, ok := e[target] return ok } type ExcludedPaths []string func NewExcludedPaths(paths []string) ExcludedPaths { return ExcludedPaths(paths) } func (e ExcludedPaths) Contains(requestURI string) bool { for _, path := range e { if strings.HasPrefix(requestURI, path) { return true } } return false } type ExcludedPathesRegexs []*regexp.Regexp func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs { result := make([]*regexp.Regexp, len(regexs)) for i, reg := range regexs { result[i] = regexp.MustCompile(reg) } return result } func (e ExcludedPathesRegexs) Contains(requestURI string) bool { for _, reg := range e { if reg.MatchString(requestURI) { return true } } return false } func DefaultDecompressHandle(c *gin.Context) { if c.Request.Body == nil { return } r, err := gzip.NewReader(c.Request.Body) if err != nil { _ = c.AbortWithError(http.StatusBadRequest, err) return } c.Request.Header.Del("Content-Encoding") c.Request.Header.Del("Content-Length") c.Request.Body = r }