From 616cabc86af8c72b2d4691dc648044e4622705cf Mon Sep 17 00:00:00 2001 From: Jimmy Praet Date: Tue, 29 Jun 2021 23:08:57 +0200 Subject: [PATCH 1/2] Support custom mime type mapping for text files --- integrations/download_test.go | 28 ++++++++++++++++++ .../10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 | 2 ++ .../26/f842bcad37fa40a1bb34cbb5ee219ee35d863d | Bin 0 -> 75 bytes .../ba/1aed4e2ea2443d76cec241b96be4ec990852ec | Bin 0 -> 117 bytes .../user2/repo2.git/refs/heads/master | 2 +- routers/common/repo.go | 21 ++++++++----- 6 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 create mode 100644 integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863d create mode 100644 integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ec diff --git a/integrations/download_test.go b/integrations/download_test.go index 305155e9ace48..38de75f476a96 100644 --- a/integrations/download_test.go +++ b/integrations/download_test.go @@ -8,6 +8,7 @@ import ( "net/http" "testing" + "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) @@ -62,3 +63,30 @@ func TestDownloadByIDMediaForSVGUsesSecureHeaders(t *testing.T) { assert.Equal(t, "image/svg+xml", resp.HeaderMap.Get("Content-Type")) assert.Equal(t, "nosniff", resp.HeaderMap.Get("X-Content-Type-Options")) } + +func TestDownloadRawTextFileWithoutMimeTypeMapping(t *testing.T) { + defer prepareTestEnv(t)() + + session := loginUser(t, "user2") + + req := NewRequest(t, "GET", "/user2/repo2/raw/branch/master/test.xml") + resp := session.MakeRequest(t, req, http.StatusOK) + + assert.Equal(t, "text/plain; charset=utf-8", resp.HeaderMap.Get("Content-Type")) +} + +func TestDownloadRawTextFileWithMimeTypeMapping(t *testing.T) { + defer prepareTestEnv(t)() + setting.MimeTypeMap.Map[".xml"] = "text/xml" + setting.MimeTypeMap.Enabled = true + + session := loginUser(t, "user2") + + req := NewRequest(t, "GET", "/user2/repo2/raw/branch/master/test.xml") + resp := session.MakeRequest(t, req, http.StatusOK) + + assert.Equal(t, "text/xml; charset=utf-8", resp.HeaderMap.Get("Content-Type")) + + delete(setting.MimeTypeMap.Map, ".xml") + setting.MimeTypeMap.Enabled = false +} diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 b/integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 new file mode 100644 index 0000000000000..736e40878edf4 --- /dev/null +++ b/integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 @@ -0,0 +1,2 @@ +xK +0Eg %":u􊕦J|p˭Q~% 9لG6G ͦw(E4}*{)`YƆleMJOܚ>%^ݿL!]N[v#E6U~/0 ZU'gpJ5 \ No newline at end of file diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863d b/integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863d new file mode 100644 index 0000000000000000000000000000000000000000..c3e7e778c5bc1e5a44f0ff429b4aa801c0938199 GIT binary patch literal 75 zcmV-R0JQ&j0ZYosPf{>5VX(2U$jwnGOD!tS%+Iq`GSo9rQb^59&QHnAOSe@D4RO=8 hP_nnq6l9f8-1_{7XU4N7C{`8AG`nn literal 0 HcmV?d00001 diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ec b/integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ec new file mode 100644 index 0000000000000000000000000000000000000000..add9a3af0d4c37c916e85c963b20a314b8fe3294 GIT binary patch literal 117 zcmV-*0E+*30V^p=O;s>7FlR6{FfcPQQSivmP1VayVR+T_r@efUH~XP%EAKClN_3cu z?N&pTJ^uzGbB&l)+hgNx13LEwg3F=Md?`@Hr!A(C8@ Date: Wed, 30 Jun 2021 09:10:36 +0200 Subject: [PATCH 2/2] Apply suggested change to routers/common/repo.go Co-authored-by: KN4CK3R --- routers/common/repo.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/routers/common/repo.go b/routers/common/repo.go index 72b880571cd12..8d33fb07fbbd5 100644 --- a/routers/common/repo.go +++ b/routers/common/repo.go @@ -75,11 +75,10 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader) log.Error("Detect raw file %s charset failed: %v, using by default utf-8", name, err) cs = "utf-8" } - if mappedMimeType != "" { - ctx.Resp.Header().Set("Content-Type", mappedMimeType+"; charset="+strings.ToLower(cs)) - } else { - ctx.Resp.Header().Set("Content-Type", "text/plain; charset="+strings.ToLower(cs)) + if mappedMimeType == "" { + mappedMimeType = "text/plain" } + ctx.Resp.Header().Set("Content-Type", mappedMimeType+"; charset="+strings.ToLower(cs)) } else { ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition") if mappedMimeType != "" {