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
Provenance
- Commit
8860527- Tree
c1803bd- Author
metal A-wing <freedom.awing.777@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
90330e2
4 files changed+35 added−2 removed
modified:context_test.go+14 added
13 unmodified lines14 "mime/multipart"15 "net"16 "net/http"17 "net/http/httptest"18+ "net/url"19 "os"20 "reflect"21 "strings"22 "sync"1010 unmodified lines1033 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 }1036 1037+func TestContextRenderUTF8Attachment(t *testing.T) {1038+ w := httptest.NewRecorder()1039+ c, _ := CreateTestContext(w)1040+ newFilename := "new🧡_filename.go"1041+1042+ c.Request, _ = http.NewRequest("GET", "/", nil)1043+ c.FileAttachment("./gin.go", newFilename)1044+1045+ 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+}1049+1050 // TestContextRenderYAML tests that the response is serialized as YAML1051 // and Content-Type is set to application/x-yaml1052 func TestContextRenderYAML(t *testing.T) {1053 w := httptest.NewRecorder()1134 unmodified linesmodified:context.go+5 added-2 removed
4 unmodified lines5 package gin6 7 import (8 "errors"9- "fmt"9 "io"10 "io/ioutil"11 "log"12 "math"1003 unmodified lines1016 1017 // 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 filename1019 func (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 }1027 1028 // SSEvent writes a Server-Sent Event into the body stream.146 unmodified linesmodified:utils_test.go+5 added
141 unmodified lines142 var x xml.StartElement143 e := h.MarshalXML(enc, x)144 assert.Error(t, e)145 }146+147+func 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 lines11 "path"12 "reflect"13 "runtime"14 "strings"15+ "unicode"16 )17 18 // BindKey indicates a default bind key.19 const BindKey = "_gin-gonic/gin/bindkey"131 unmodified lines151 default:152 panic("too many parameters")153 }154 }155+156+// https://stackoverflow.com/questions/53069040/checking-a-string-contains-only-ascii-characters157+func 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+}