From fba990a056c296adf3417b8b85a953b63e820be9 Mon Sep 17 00:00:00 2001 From: Levi Date: Thu, 13 Feb 2025 21:54:13 +0800 Subject: [PATCH] fix: http MIME encoded word filename parse (#931) --- internal/protocol/http/fetcher.go | 9 ++++++++- internal/protocol/http/fetcher_test.go | 10 ++++++++++ internal/test/httptest.go | 11 +++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/internal/protocol/http/fetcher.go b/internal/protocol/http/fetcher.go index bcbcc867..a4034ba2 100644 --- a/internal/protocol/http/fetcher.go +++ b/internal/protocol/http/fetcher.go @@ -147,7 +147,14 @@ func (f *Fetcher) Resolve(req *base.Request) error { _, params, _ := mime.ParseMediaType(contentDisposition) filename := params["filename"] if filename != "" { - file.Name = filename + // Check if the filename is MIME encoded-word + if strings.HasPrefix(filename, "=?") { + decoder := new(mime.WordDecoder) + filename = strings.Replace(filename, "UTF8", "UTF-8", 1) + file.Name, _ = decoder.Decode(filename) + } else { + file.Name = filename + } } } // get file filePath by URL diff --git a/internal/protocol/http/fetcher_test.go b/internal/protocol/http/fetcher_test.go index 995949e7..b88f2a38 100644 --- a/internal/protocol/http/fetcher_test.go +++ b/internal/protocol/http/fetcher_test.go @@ -37,6 +37,16 @@ func TestFetcher_Resolve(t *testing.T) { }, }, }, t) + testResolve(test.StartTestCustomServer, "encoded-word", &base.Resource{ + Size: test.BuildSize, + Range: false, + Files: []*base.FileInfo{ + { + Name: "测试.zip", + Size: test.BuildSize, + }, + }, + }, t) testResolve(test.StartTestCustomServer, test.BuildName, &base.Resource{ Size: 0, Range: false, diff --git a/internal/test/httptest.go b/internal/test/httptest.go index 77f183eb..86e841e4 100644 --- a/internal/test/httptest.go +++ b/internal/test/httptest.go @@ -83,6 +83,17 @@ func StartTestCustomServer() net.Listener { defer file.Close() io.Copy(writer, file) }) + mux.HandleFunc("/encoded-word", func(writer http.ResponseWriter, request *http.Request) { + writer.Header().Set("Content-Disposition", "attachment; filename=\"=?UTF8?B?5rWL6K+VLnppcA==?=\"") + writer.Header().Set("Content-Type", "application/octet-stream") + writer.Header().Set("Content-Length", fmt.Sprintf("%d", BuildSize)) + file, err := os.Open(BuildFile) + if err != nil { + panic(err) + } + defer file.Close() + io.Copy(writer, file) + }) return mux }) }