Skip to content
Myr

feat attachment filename support utf8 (#3071)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by metal A-wing · committed by GitHub · parent 90330e2 · Browse files · 4 files+35 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+35 added−2 removed

  • other:+35 added−2 removed

4 files+35 added−2 removed

modified:context_test.go+14 added
13 unmodified lines
14 "mime/multipart"15 "net"16 "net/http"17 "net/http/httptest"18 "net/url"19 "os"20 "reflect"21 "strings"22 "sync"
1010 unmodified lines
1033 assert.Contains(t, w.Body.String(), "func New() *Engine {")1034 assert.Equal(t, fmt.Sprintf("attachment; filename=\"%s\"", newFilename), w.Header().Get("Content-Disposition"))1035}10361037func TestContextRenderUTF8Attachment(t *testing.T) {1038 w := httptest.NewRecorder()1039 c, _ := CreateTestContext(w)1040 newFilename := "new🧡_filename.go"10411042 c.Request, _ = http.NewRequest("GET", "/", nil)1043 c.FileAttachment("./gin.go", newFilename)10441045 assert.Equal(t, 200, w.Code)1046 assert.Contains(t, w.Body.String(), "func New() *Engine {")1047 assert.Equal(t, `attachment; filename*=UTF-8''`+url.QueryEscape(newFilename), w.Header().Get("Content-Disposition"))1048}10491050// TestContextRenderYAML tests that the response is serialized as YAML1051// and Content-Type is set to application/x-yaml1052func TestContextRenderYAML(t *testing.T) {1053 w := httptest.NewRecorder()
1134 unmodified lines
modified:context.go+5 added-2 removed
4 unmodified lines
5package gin67import (8 "errors"9 "fmt"9 "io"10 "io/ioutil"11 "log"12 "math"
1003 unmodified lines
10161017// FileAttachment writes the specified file into the body stream in an efficient way1018// On the client side, the file will typically be downloaded with the given filename1019func (c *Context) FileAttachment(filepath, filename string) {1020 if isASCII(filename) {1021 c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+filename+`"`)1022 } else {1021 c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))1023 c.Writer.Header().Set("Content-Disposition", `attachment; filename*=UTF-8''`+url.QueryEscape(filename))1024 }1025 http.ServeFile(c.Writer, c.Request, filepath)1026}10271028// SSEvent writes a Server-Sent Event into the body stream.
146 unmodified lines
modified:utils_test.go+5 added
141 unmodified lines
142 var x xml.StartElement143 e := h.MarshalXML(enc, x)144 assert.Error(t, e)145}146147func TestIsASCII(t *testing.T) {148 assert.Equal(t, isASCII("test"), true)149 assert.Equal(t, isASCII("🧡💛💚💙💜"), false)150}
modified:utils.go+11 added
10 unmodified lines
11 "path"12 "reflect"13 "runtime"14 "strings"15 "unicode"16)1718// BindKey indicates a default bind key.19const BindKey = "_gin-gonic/gin/bindkey"
131 unmodified lines
151 default:152 panic("too many parameters")153 }154}155156// https://stackoverflow.com/questions/53069040/checking-a-string-contains-only-ascii-characters157func isASCII(s string) bool {158 for i := 0; i < len(s); i++ {159 if s[i] > unicode.MaxASCII {160 return false161 }162 }163 return true164}