Skip to content

Commit be112c1

Browse files
Skip gzip for some well-known compressed file types (#30796)
Co-authored-by: silverwind <me@silverwind.io>
1 parent ce08a9f commit be112c1

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

modules/httplib/serve.go

+8
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ import (
1717
"time"
1818

1919
charsetModule "code.gitea.io/gitea/modules/charset"
20+
"code.gitea.io/gitea/modules/container"
2021
"code.gitea.io/gitea/modules/httpcache"
2122
"code.gitea.io/gitea/modules/log"
2223
"code.gitea.io/gitea/modules/setting"
2324
"code.gitea.io/gitea/modules/typesniffer"
2425
"code.gitea.io/gitea/modules/util"
26+
27+
"github.com/klauspost/compress/gzhttp"
2528
)
2629

2730
type ServeHeaderOptions struct {
@@ -38,6 +41,11 @@ type ServeHeaderOptions struct {
3841
func ServeSetHeaders(w http.ResponseWriter, opts *ServeHeaderOptions) {
3942
header := w.Header()
4043

44+
skipCompressionExts := container.SetOf(".gz", ".bz2", ".zip", ".xz", ".zst", ".deb", ".apk", ".jar", ".png", ".jpg", ".webp")
45+
if skipCompressionExts.Contains(strings.ToLower(path.Ext(opts.Filename))) {
46+
w.Header().Add(gzhttp.HeaderNoCompression, "1")
47+
}
48+
4149
contentType := typesniffer.ApplicationOctetStream
4250
if opts.ContentType != "" {
4351
if opts.ContentTypeCharset != "" {

routers/web/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import (
5454
"github.com/prometheus/client_golang/prometheus"
5555
)
5656

57-
const GzipMinSize = 1400 // min size to compress for the body size of response
57+
var GzipMinSize = 1400 // min size to compress for the body size of response
5858

5959
// optionsCorsHandler return a http handler which sets CORS options if enabled by config, it blocks non-CORS OPTIONS requests.
6060
func optionsCorsHandler() func(next http.Handler) http.Handler {
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"io"
8+
"net/http"
9+
"testing"
10+
11+
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/test"
13+
"code.gitea.io/gitea/routers"
14+
"code.gitea.io/gitea/routers/web"
15+
"code.gitea.io/gitea/tests"
16+
17+
"github.com/stretchr/testify/assert"
18+
)
19+
20+
func TestRepoDownloadArchive(t *testing.T) {
21+
defer tests.PrepareTestEnv(t)()
22+
defer test.MockVariableValue(&setting.EnableGzip, true)()
23+
defer test.MockVariableValue(&web.GzipMinSize, 10)()
24+
defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()
25+
26+
req := NewRequest(t, "GET", "/user2/repo1/archive/master.zip")
27+
req.Header.Set("Accept-Encoding", "gzip")
28+
resp := MakeRequest(t, req, http.StatusOK)
29+
bs, err := io.ReadAll(resp.Body)
30+
assert.NoError(t, err)
31+
assert.Empty(t, resp.Header().Get("Content-Encoding"))
32+
assert.Equal(t, 320, len(bs))
33+
}

0 commit comments

Comments
 (0)