mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-25 11:22:16 +00:00
Move modules/gzip to gitea.com/macaron/gzip (#9058)
* Move modules/gzip to gitea.com/macaron/gzip * Fix vendor
This commit is contained in:
parent
ba4e8f221b
commit
9ff6312627
54 changed files with 2972 additions and 5163 deletions
9
vendor/gitea.com/macaron/gzip/go.mod
generated
vendored
Normal file
9
vendor/gitea.com/macaron/gzip/go.mod
generated
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
module gitea.com/macaron/gzip
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
gitea.com/macaron/macaron v1.3.3-0.20190821202302-9646c0587edb
|
||||
github.com/klauspost/compress v1.9.2
|
||||
github.com/stretchr/testify v1.4.0
|
||||
)
|
42
vendor/gitea.com/macaron/gzip/go.sum
generated
vendored
Normal file
42
vendor/gitea.com/macaron/gzip/go.sum
generated
vendored
Normal file
|
@ -0,0 +1,42 @@
|
|||
gitea.com/macaron/inject v0.0.0-20190803172902-8375ba841591 h1:UbCTjPcLrNxR9LzKDjQBMT2zoxZuEnca1pZCpgeMuhQ=
|
||||
gitea.com/macaron/inject v0.0.0-20190803172902-8375ba841591/go.mod h1:h6E4kLao1Yko6DOU6QDnQPcuoNzvbZqzj2mtPcEn1aM=
|
||||
gitea.com/macaron/macaron v1.3.3-0.20190821202302-9646c0587edb h1:amL0md6orTj1tXY16ANzVU9FmzQB+W7aJwp8pVDbrmA=
|
||||
gitea.com/macaron/macaron v1.3.3-0.20190821202302-9646c0587edb/go.mod h1:0coI+mSPSwbsyAbOuFllVS38awuk9mevhLD52l50Gjs=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/klauspost/compress v1.9.2 h1:LfVyl+ZlLlLDeQ/d2AqfGIIH4qEDu0Ed2S5GyhCWIWY=
|
||||
github.com/klauspost/compress v1.9.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
||||
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/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 h1:Jpy1PXuP99tXNrhbq2BaPz9B+jNAvH1JPQQpG/9GCXY=
|
||||
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
|
||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8=
|
||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e h1:GSGeB9EAKY2spCABz6xOX5DbxZEXolK+nBSvmsQwRjM=
|
||||
github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
|
||||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.44.0 h1:YRJzTUp0kSYWUVFF5XAbDFfyiqwsl0Vb9R8TVP5eRi0=
|
||||
gopkg.in/ini.v1 v1.44.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
358
vendor/gitea.com/macaron/gzip/gzip.go
generated
vendored
Normal file
358
vendor/gitea.com/macaron/gzip/gzip.go
generated
vendored
Normal file
|
@ -0,0 +1,358 @@
|
|||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gzip
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gitea.com/macaron/macaron"
|
||||
"github.com/klauspost/compress/gzip"
|
||||
)
|
||||
|
||||
const (
|
||||
acceptEncodingHeader = "Accept-Encoding"
|
||||
contentEncodingHeader = "Content-Encoding"
|
||||
contentLengthHeader = "Content-Length"
|
||||
contentTypeHeader = "Content-Type"
|
||||
rangeHeader = "Range"
|
||||
varyHeader = "Vary"
|
||||
)
|
||||
|
||||
const (
|
||||
// MinSize is the minimum size of content we will compress
|
||||
MinSize = 1400
|
||||
)
|
||||
|
||||
// noopClosers are io.Writers with a shim to prevent early closure
|
||||
type noopCloser struct {
|
||||
io.Writer
|
||||
}
|
||||
|
||||
func (noopCloser) Close() error { return nil }
|
||||
|
||||
// WriterPool is a gzip writer pool to reduce workload on creation of
|
||||
// gzip writers
|
||||
type WriterPool struct {
|
||||
pool sync.Pool
|
||||
compressionLevel int
|
||||
}
|
||||
|
||||
// NewWriterPool creates a new pool
|
||||
func NewWriterPool(compressionLevel int) *WriterPool {
|
||||
return &WriterPool{pool: sync.Pool{
|
||||
// New will return nil, we'll manage the creation of new
|
||||
// writers in the middleware
|
||||
New: func() interface{} { return nil },
|
||||
},
|
||||
compressionLevel: compressionLevel}
|
||||
}
|
||||
|
||||
// Get a writer from the pool - or create one if not available
|
||||
func (wp *WriterPool) Get(rw macaron.ResponseWriter) *gzip.Writer {
|
||||
ret := wp.pool.Get()
|
||||
if ret == nil {
|
||||
ret, _ = gzip.NewWriterLevel(rw, wp.compressionLevel)
|
||||
} else {
|
||||
ret.(*gzip.Writer).Reset(rw)
|
||||
}
|
||||
return ret.(*gzip.Writer)
|
||||
}
|
||||
|
||||
// Put returns a writer to the pool
|
||||
func (wp *WriterPool) Put(w *gzip.Writer) {
|
||||
wp.pool.Put(w)
|
||||
}
|
||||
|
||||
var writerPool WriterPool
|
||||
|
||||
// Options represents the configuration for the gzip middleware
|
||||
type Options struct {
|
||||
CompressionLevel int
|
||||
}
|
||||
|
||||
func validateCompressionLevel(level int) bool {
|
||||
return level == gzip.DefaultCompression ||
|
||||
level == gzip.ConstantCompression ||
|
||||
(level >= gzip.BestSpeed && level <= gzip.BestCompression)
|
||||
}
|
||||
|
||||
func validate(options []Options) Options {
|
||||
// Default to level 4 compression (Best results seem to be between 4 and 6)
|
||||
opt := Options{CompressionLevel: 4}
|
||||
if len(options) > 0 {
|
||||
opt = options[0]
|
||||
}
|
||||
if !validateCompressionLevel(opt.CompressionLevel) {
|
||||
opt.CompressionLevel = 4
|
||||
}
|
||||
return opt
|
||||
}
|
||||
|
||||
// Middleware creates a macaron.Handler to proxy the response
|
||||
func Middleware(options ...Options) macaron.Handler {
|
||||
opt := validate(options)
|
||||
writerPool = *NewWriterPool(opt.CompressionLevel)
|
||||
regex := regexp.MustCompile(`bytes=(\d+)\-.*`)
|
||||
|
||||
return func(ctx *macaron.Context) {
|
||||
// If the client won't accept gzip or x-gzip don't compress
|
||||
if !strings.Contains(ctx.Req.Header.Get(acceptEncodingHeader), "gzip") &&
|
||||
!strings.Contains(ctx.Req.Header.Get(acceptEncodingHeader), "x-gzip") {
|
||||
return
|
||||
}
|
||||
|
||||
// If the client is asking for a specific range of bytes - don't compress
|
||||
if rangeHdr := ctx.Req.Header.Get(rangeHeader); rangeHdr != "" {
|
||||
|
||||
match := regex.FindStringSubmatch(rangeHdr)
|
||||
if len(match) > 1 {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// OK we should proxy the response writer
|
||||
// We are still not necessarily going to compress...
|
||||
proxyWriter := &ProxyResponseWriter{
|
||||
internal: ctx.Resp,
|
||||
}
|
||||
defer proxyWriter.Close()
|
||||
|
||||
ctx.Resp = proxyWriter
|
||||
ctx.MapTo(proxyWriter, (*http.ResponseWriter)(nil))
|
||||
|
||||
// Check if render middleware has been registered,
|
||||
// if yes, we need to modify ResponseWriter for it as well.
|
||||
if _, ok := ctx.Render.(*macaron.DummyRender); !ok {
|
||||
ctx.Render.SetResponseWriter(proxyWriter)
|
||||
}
|
||||
|
||||
ctx.Next()
|
||||
ctx.Resp = proxyWriter.internal
|
||||
}
|
||||
}
|
||||
|
||||
// ProxyResponseWriter is a wrapped macaron ResponseWriter that may compress its contents
|
||||
type ProxyResponseWriter struct {
|
||||
writer io.WriteCloser
|
||||
internal macaron.ResponseWriter
|
||||
stopped bool
|
||||
|
||||
code int
|
||||
buf []byte
|
||||
}
|
||||
|
||||
// Header returns the header map
|
||||
func (proxy *ProxyResponseWriter) Header() http.Header {
|
||||
return proxy.internal.Header()
|
||||
}
|
||||
|
||||
// Status returns the status code of the response or 0 if the response has not been written.
|
||||
func (proxy *ProxyResponseWriter) Status() int {
|
||||
if proxy.code != 0 {
|
||||
return proxy.code
|
||||
}
|
||||
return proxy.internal.Status()
|
||||
}
|
||||
|
||||
// Written returns whether or not the ResponseWriter has been written.
|
||||
func (proxy *ProxyResponseWriter) Written() bool {
|
||||
if proxy.code != 0 {
|
||||
return true
|
||||
}
|
||||
return proxy.internal.Written()
|
||||
}
|
||||
|
||||
// Size returns the size of the response body.
|
||||
func (proxy *ProxyResponseWriter) Size() int {
|
||||
return proxy.internal.Size()
|
||||
}
|
||||
|
||||
// Before allows for a function to be called before the ResponseWriter has been written to. This is
|
||||
// useful for setting headers or any other operations that must happen before a response has been written.
|
||||
func (proxy *ProxyResponseWriter) Before(before macaron.BeforeFunc) {
|
||||
proxy.internal.Before(before)
|
||||
}
|
||||
|
||||
// Write appends data to the proxied gzip writer.
|
||||
func (proxy *ProxyResponseWriter) Write(b []byte) (int, error) {
|
||||
// if writer is initialized, use the writer
|
||||
if proxy.writer != nil {
|
||||
return proxy.writer.Write(b)
|
||||
}
|
||||
|
||||
proxy.buf = append(proxy.buf, b...)
|
||||
|
||||
var (
|
||||
contentLength, _ = strconv.Atoi(proxy.Header().Get(contentLengthHeader))
|
||||
contentType = proxy.Header().Get(contentTypeHeader)
|
||||
contentEncoding = proxy.Header().Get(contentEncodingHeader)
|
||||
)
|
||||
|
||||
// OK if an encoding hasn't been chosen, and content length > 1400
|
||||
// and content type isn't a compressed type
|
||||
if contentEncoding == "" &&
|
||||
(contentLength == 0 || contentLength >= MinSize) &&
|
||||
(contentType == "" || !compressedContentType(contentType)) {
|
||||
// If current buffer is less than the min size and a Content-Length isn't set, then wait
|
||||
if len(proxy.buf) < MinSize && contentLength == 0 {
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
// If the Content-Length is larger than minSize or the current buffer is larger than minSize, then continue.
|
||||
if contentLength >= MinSize || len(proxy.buf) >= MinSize {
|
||||
// if we don't know the content type, infer it
|
||||
if contentType == "" {
|
||||
contentType = http.DetectContentType(proxy.buf)
|
||||
proxy.Header().Set(contentTypeHeader, contentType)
|
||||
}
|
||||
// If the Content-Type is not compressed - Compress!
|
||||
if !compressedContentType(contentType) {
|
||||
if err := proxy.startGzip(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(b), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we got here, we should not GZIP this response.
|
||||
if err := proxy.startPlain(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (proxy *ProxyResponseWriter) startGzip() error {
|
||||
// Set the content-encoding and vary headers.
|
||||
proxy.Header().Set(contentEncodingHeader, "gzip")
|
||||
proxy.Header().Set(varyHeader, acceptEncodingHeader)
|
||||
|
||||
// if the Content-Length is already set, then calls to Write on gzip
|
||||
// will fail to set the Content-Length header since its already set
|
||||
// See: https://github.com/golang/go/issues/14975.
|
||||
proxy.Header().Del(contentLengthHeader)
|
||||
|
||||
// Write the header to gzip response.
|
||||
if proxy.code != 0 {
|
||||
proxy.internal.WriteHeader(proxy.code)
|
||||
// Ensure that no other WriteHeader's happen
|
||||
proxy.code = 0
|
||||
}
|
||||
|
||||
// Initialize and flush the buffer into the gzip response if there are any bytes.
|
||||
// If there aren't any, we shouldn't initialize it yet because on Close it will
|
||||
// write the gzip header even if nothing was ever written.
|
||||
if len(proxy.buf) > 0 {
|
||||
// Initialize the GZIP response.
|
||||
proxy.writer = writerPool.Get(proxy.internal)
|
||||
|
||||
return proxy.writeBuf()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (proxy *ProxyResponseWriter) startPlain() error {
|
||||
if proxy.code != 0 {
|
||||
proxy.internal.WriteHeader(proxy.code)
|
||||
proxy.code = 0
|
||||
}
|
||||
proxy.stopped = true
|
||||
proxy.writer = noopCloser{proxy.internal}
|
||||
return proxy.writeBuf()
|
||||
}
|
||||
|
||||
func (proxy *ProxyResponseWriter) writeBuf() error {
|
||||
if proxy.buf == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
n, err := proxy.writer.Write(proxy.buf)
|
||||
|
||||
// This should never happen (per io.Writer docs), but if the write didn't
|
||||
// accept the entire buffer but returned no specific error, we have no clue
|
||||
// what's going on, so abort just to be safe.
|
||||
if err == nil && n < len(proxy.buf) {
|
||||
err = io.ErrShortWrite
|
||||
}
|
||||
proxy.buf = nil
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteHeader will ensure that we have setup the writer before we write the header
|
||||
func (proxy *ProxyResponseWriter) WriteHeader(code int) {
|
||||
if proxy.code == 0 {
|
||||
proxy.code = code
|
||||
}
|
||||
}
|
||||
|
||||
// Close the writer
|
||||
func (proxy *ProxyResponseWriter) Close() error {
|
||||
if proxy.stopped {
|
||||
return nil
|
||||
}
|
||||
|
||||
if proxy.writer == nil {
|
||||
err := proxy.startPlain()
|
||||
if err != nil {
|
||||
return fmt.Errorf("GzipMiddleware: write to regular responseWriter at close gets error: %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
err := proxy.writer.Close()
|
||||
|
||||
if poolWriter, ok := proxy.writer.(*gzip.Writer); ok {
|
||||
writerPool.Put(poolWriter)
|
||||
}
|
||||
|
||||
proxy.writer = nil
|
||||
proxy.stopped = true
|
||||
return err
|
||||
}
|
||||
|
||||
// Flush the writer
|
||||
func (proxy *ProxyResponseWriter) Flush() {
|
||||
if proxy.writer == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if gw, ok := proxy.writer.(*gzip.Writer); ok {
|
||||
gw.Flush()
|
||||
}
|
||||
|
||||
proxy.internal.Flush()
|
||||
}
|
||||
|
||||
// Hijack implements http.Hijacker. If the underlying ResponseWriter is a
|
||||
// Hijacker, its Hijack method is returned. Otherwise an error is returned.
|
||||
func (proxy *ProxyResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
hijacker, ok := proxy.internal.(http.Hijacker)
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface")
|
||||
}
|
||||
return hijacker.Hijack()
|
||||
}
|
||||
|
||||
// verify Hijacker interface implementation
|
||||
var _ http.Hijacker = &ProxyResponseWriter{}
|
||||
|
||||
func compressedContentType(contentType string) bool {
|
||||
switch contentType {
|
||||
case "application/zip":
|
||||
return true
|
||||
case "application/x-gzip":
|
||||
return true
|
||||
case "application/gzip":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue