]> source.dussan.org Git - gitea.git/commitdiff
Skip gzip for some well-known compressed file types (#30796) (#30813)
authorGiteabot <teabot@gitea.io>
Thu, 2 May 2024 06:50:24 +0000 (14:50 +0800)
committerGitHub <noreply@github.com>
Thu, 2 May 2024 06:50:24 +0000 (14:50 +0800)
Backport #30796 by wxiaoguang

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
modules/httplib/serve.go
routers/web/web.go
tests/integration/repo_archive_test.go [new file with mode: 0644]

index a193ed901cf99de794e083a3dbf29d10404507b4..6e147d76f5142323b2d9be3f9f1db4156a0e6ae6 100644 (file)
@@ -17,11 +17,14 @@ import (
        "time"
 
        charsetModule "code.gitea.io/gitea/modules/charset"
+       "code.gitea.io/gitea/modules/container"
        "code.gitea.io/gitea/modules/httpcache"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/setting"
        "code.gitea.io/gitea/modules/typesniffer"
        "code.gitea.io/gitea/modules/util"
+
+       "github.com/klauspost/compress/gzhttp"
 )
 
 type ServeHeaderOptions struct {
@@ -38,6 +41,11 @@ type ServeHeaderOptions struct {
 func ServeSetHeaders(w http.ResponseWriter, opts *ServeHeaderOptions) {
        header := w.Header()
 
+       skipCompressionExts := container.SetOf(".gz", ".bz2", ".zip", ".xz", ".zst", ".deb", ".apk", ".jar", ".png", ".jpg", ".webp")
+       if skipCompressionExts.Contains(strings.ToLower(path.Ext(opts.Filename))) {
+               w.Header().Add(gzhttp.HeaderNoCompression, "1")
+       }
+
        contentType := typesniffer.ApplicationOctetStream
        if opts.ContentType != "" {
                if opts.ContentTypeCharset != "" {
index 9a6687059b8f2d132de14a3ebc4549b029145544..91ab378d97c52257844ac13a4ce481f01b87afe8 100644 (file)
@@ -54,7 +54,7 @@ import (
        "github.com/prometheus/client_golang/prometheus"
 )
 
-const GzipMinSize = 1400 // min size to compress for the body size of response
+var GzipMinSize = 1400 // min size to compress for the body size of response
 
 // optionsCorsHandler return a http handler which sets CORS options if enabled by config, it blocks non-CORS OPTIONS requests.
 func optionsCorsHandler() func(next http.Handler) http.Handler {
diff --git a/tests/integration/repo_archive_test.go b/tests/integration/repo_archive_test.go
new file mode 100644 (file)
index 0000000..664b04b
--- /dev/null
@@ -0,0 +1,33 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package integration
+
+import (
+       "io"
+       "net/http"
+       "testing"
+
+       "code.gitea.io/gitea/modules/setting"
+       "code.gitea.io/gitea/modules/test"
+       "code.gitea.io/gitea/routers"
+       "code.gitea.io/gitea/routers/web"
+       "code.gitea.io/gitea/tests"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestRepoDownloadArchive(t *testing.T) {
+       defer tests.PrepareTestEnv(t)()
+       defer test.MockVariableValue(&setting.EnableGzip, true)()
+       defer test.MockVariableValue(&web.GzipMinSize, 10)()
+       defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()
+
+       req := NewRequest(t, "GET", "/user2/repo1/archive/master.zip")
+       req.Header.Set("Accept-Encoding", "gzip")
+       resp := MakeRequest(t, req, http.StatusOK)
+       bs, err := io.ReadAll(resp.Body)
+       assert.NoError(t, err)
+       assert.Empty(t, resp.Header().Get("Content-Encoding"))
+       assert.Equal(t, 320, len(bs))
+}