Skip to content

Commit 03719d1

Browse files
committed
fix: http MIME encoded word filename parse
1 parent f3d9873 commit 03719d1

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

internal/protocol/http/fetcher.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,14 @@ func (f *Fetcher) Resolve(req *base.Request) error {
147147
_, params, _ := mime.ParseMediaType(contentDisposition)
148148
filename := params["filename"]
149149
if filename != "" {
150-
file.Name = filename
150+
// Check if the filename is MIME encoded-word
151+
if strings.HasPrefix(filename, "=?") {
152+
decoder := new(mime.WordDecoder)
153+
filename = strings.Replace(filename, "UTF8", "UTF-8", 1)
154+
file.Name, _ = decoder.Decode(filename)
155+
} else {
156+
file.Name = filename
157+
}
151158
}
152159
}
153160
// get file filePath by URL

internal/protocol/http/fetcher_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ func TestFetcher_Resolve(t *testing.T) {
3737
},
3838
},
3939
}, t)
40+
testResolve(test.StartTestCustomServer, "encoded-word", &base.Resource{
41+
Size: test.BuildSize,
42+
Range: false,
43+
Files: []*base.FileInfo{
44+
{
45+
Name: "测试.zip",
46+
Size: test.BuildSize,
47+
},
48+
},
49+
}, t)
4050
testResolve(test.StartTestCustomServer, test.BuildName, &base.Resource{
4151
Size: 0,
4252
Range: false,

internal/test/httptest.go

+11
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ func StartTestCustomServer() net.Listener {
8383
defer file.Close()
8484
io.Copy(writer, file)
8585
})
86+
mux.HandleFunc("/encoded-word", func(writer http.ResponseWriter, request *http.Request) {
87+
writer.Header().Set("Content-Disposition", "attachment; filename=\"=?UTF8?B?5rWL6K+VLnppcA==?=\"")
88+
writer.Header().Set("Content-Type", "application/octet-stream")
89+
writer.Header().Set("Content-Length", fmt.Sprintf("%d", BuildSize))
90+
file, err := os.Open(BuildFile)
91+
if err != nil {
92+
panic(err)
93+
}
94+
defer file.Close()
95+
io.Copy(writer, file)
96+
})
8697
return mux
8798
})
8899
}

0 commit comments

Comments
 (0)