From 43bfee0d4872932c1da185e8367e21795bc4ceb5 Mon Sep 17 00:00:00 2001 From: Vladimir Vissoultchev Date: Tue, 28 Jul 2015 19:50:35 +0300 Subject: Raw text file view returns correct charset in content-type header if not utf-8 --- routers/repo/download.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'routers') diff --git a/routers/repo/download.go b/routers/repo/download.go index b1c5fbc8..8e9efba3 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -26,10 +26,17 @@ func ServeBlob(ctx *middleware.Context, blob *git.Blob) error { } _, isTextFile := base.IsTextFile(buf) - _, isImageFile := base.IsImageFile(buf) - if !isTextFile && !isImageFile { - ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+path.Base(ctx.Repo.TreeName)) - ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary") + if isTextFile { + charset, _ := base.DetectEncoding(buf) + if charset != "utf-8" { + ctx.Resp.Header().Set("Content-Type", "text/plain; charset="+charset) + } + } else { + _, isImageFile := base.IsImageFile(buf) + if !isImageFile { + ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+path.Base(ctx.Repo.TreeName)) + ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary") + } } ctx.Resp.Write(buf) _, err = io.Copy(ctx.Resp, dataRc) -- cgit v1.2.3 From 2cc050e21ed35d95d824cc22d129fc22ee1318a4 Mon Sep 17 00:00:00 2001 From: Vladimir Vissoultchev Date: Wed, 29 Jul 2015 17:58:03 +0300 Subject: Fix UTF-8 in upper-case, use ansi charset for all non UTF-8 encodings --- modules/base/template.go | 4 ++-- modules/setting/setting.go | 2 +- routers/repo/download.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'routers') diff --git a/modules/base/template.go b/modules/base/template.go index f0a2e032..2a81a34d 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -55,7 +55,7 @@ func ShortSha(sha1 string) string { func DetectEncoding(content []byte) (string, error) { detector := chardet.NewTextDetector() result, err := detector.DetectBest(content) - if result.Charset == "ISO-8859-1" { + if result.Charset != "UTF-8" && len(setting.AnsiCharset) > 0 { return setting.AnsiCharset, err } return result.Charset, err @@ -67,7 +67,7 @@ func ToUtf8WithErr(content []byte) (error, string) { return err, "" } - if charsetLabel == "utf8" { + if charsetLabel == "UTF-8" { return nil, string(content) } diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 896e60ad..f826a3a4 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -313,7 +313,7 @@ func NewConfigContext() { RepoRootPath = path.Clean(RepoRootPath) } ScriptType = sec.Key("SCRIPT_TYPE").MustString("bash") - AnsiCharset = sec.Key("ANSI_CHARSET").MustString("ISO-8859-1") + AnsiCharset = sec.Key("ANSI_CHARSET").MustString("") // UI settings. IssuePagingNum = Cfg.Section("ui").Key("ISSUE_PAGING_NUM").MustInt(10) diff --git a/routers/repo/download.go b/routers/repo/download.go index 8e9efba3..c71f8d29 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -28,7 +28,7 @@ func ServeBlob(ctx *middleware.Context, blob *git.Blob) error { _, isTextFile := base.IsTextFile(buf) if isTextFile { charset, _ := base.DetectEncoding(buf) - if charset != "utf-8" { + if charset != "UTF-8" { ctx.Resp.Header().Set("Content-Type", "text/plain; charset="+charset) } } else { -- cgit v1.2.3