From 2b011e71c7555900f46a71a6b74c7fb738fb7fd7 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 1 May 2024 12:32:39 +0800 Subject: [PATCH 1/3] fix --- modules/httplib/serve.go | 8 +++++++ routers/web/web.go | 2 +- tests/integration/repo_archive_test.go | 33 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/integration/repo_archive_test.go diff --git a/modules/httplib/serve.go b/modules/httplib/serve.go index a193ed901cf99..afda09c77d6dc 100644 --- a/modules/httplib/serve.go +++ b/modules/httplib/serve.go @@ -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") + if skipCompressionExts.Contains(path.Ext(opts.Filename)) { + w.Header().Add(gzhttp.HeaderNoCompression, "1") + } + contentType := typesniffer.ApplicationOctetStream if opts.ContentType != "" { if opts.ContentTypeCharset != "" { diff --git a/routers/web/web.go b/routers/web/web.go index 9a6687059b8f2..91ab378d97c52 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -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 index 0000000000000..664b04baf755c --- /dev/null +++ b/tests/integration/repo_archive_test.go @@ -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)) +} From fbed6e2ae2c4ae3968998cf9f9e626a338f717d8 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 2 May 2024 08:35:47 +0800 Subject: [PATCH 2/3] Update modules/httplib/serve.go Co-authored-by: silverwind --- modules/httplib/serve.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/httplib/serve.go b/modules/httplib/serve.go index afda09c77d6dc..95f47da9e4353 100644 --- a/modules/httplib/serve.go +++ b/modules/httplib/serve.go @@ -41,7 +41,7 @@ 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") + skipCompressionExts := container.SetOf(".gz", ".bz2", ".zip", ".xz", ".zst", ".deb", ".apk", ".jar", ".png", ".jpg", ".webp") if skipCompressionExts.Contains(path.Ext(opts.Filename)) { w.Header().Add(gzhttp.HeaderNoCompression, "1") } From d012049e61e38b4cb411649b44653fc9e7b7a8a8 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 2 May 2024 08:37:05 +0800 Subject: [PATCH 3/3] use lowercase --- modules/httplib/serve.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/httplib/serve.go b/modules/httplib/serve.go index 95f47da9e4353..6e147d76f5142 100644 --- a/modules/httplib/serve.go +++ b/modules/httplib/serve.go @@ -42,7 +42,7 @@ 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(path.Ext(opts.Filename)) { + if skipCompressionExts.Contains(strings.ToLower(path.Ext(opts.Filename))) { w.Header().Add(gzhttp.HeaderNoCompression, "1") }