"net/http"
"testing"
+ "code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert"
)
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
+}
--- /dev/null
+x\ 1\9d\8eK
+Â0\14E\1dg\15\99\v%\9f×\17\ 3":uä\16\92ô\8a\95¦J|\82îÞê\12\1c\1e¸çpËÖQ´ó~%\rÐ9Ù\84\81à\90\1c\91\1f\ 2\17\14G6GÎ \94\18ͦw(ê\9e\1aæE4}*\81íÙ{Ç)`\19YÆ\86\83\ fl\8ceêMî\97\84JO¹Ü\9a>\8eµ¾õ©%\88Þ^¿ÐÝ¿°\17L\98!]ÆN[v\14#\13E½6Î\18U~/\ 5ÿúê0\fZð\90îU'õ\ 1gpJ5
\ No newline at end of file
-205ac761f3326a7ebe416e8673760016450b5cec
+1032bbf17fbc0d9c95bb5418dabe8f8c99278700
st := typesniffer.DetectContentType(buf)
+ mappedMimeType := ""
+ if setting.MimeTypeMap.Enabled {
+ fileExtension := strings.ToLower(filepath.Ext(name))
+ mappedMimeType = setting.MimeTypeMap.Map[fileExtension]
+ }
if st.IsText() || ctx.QueryBool("render") {
cs, err := charset.DetectEncoding(buf)
if err != nil {
log.Error("Detect raw file %s charset failed: %v, using by default utf-8", name, err)
cs = "utf-8"
}
- 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 != "" {
+ ctx.Resp.Header().Set("Content-Type", mappedMimeType)
+ }
if (st.IsImage() || st.IsPDF()) && (setting.UI.SVG.Enabled || !st.IsSvgImage()) {
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name))
if st.IsSvgImage() {
}
} else {
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
- if setting.MimeTypeMap.Enabled {
- fileExtension := strings.ToLower(filepath.Ext(name))
- if mimetype, ok := setting.MimeTypeMap.Map[fileExtension]; ok {
- ctx.Resp.Header().Set("Content-Type", mimetype)
- }
- }
}
}