From 9c5aebaf3664333baf3fe981b840428b20b80bd5 Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 18 Feb 2025 19:37:38 +0800 Subject: [PATCH] fix: path url encode filename parse (#939) --- internal/protocol/http/fetcher.go | 4 ++++ internal/protocol/http/fetcher_test.go | 10 ++++++++++ internal/test/httptest.go | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/internal/protocol/http/fetcher.go b/internal/protocol/http/fetcher.go index a4034ba2..4cd0a890 100644 --- a/internal/protocol/http/fetcher.go +++ b/internal/protocol/http/fetcher.go @@ -160,6 +160,10 @@ func (f *Fetcher) Resolve(req *base.Request) error { // get file filePath by URL if file.Name == "" { file.Name = path.Base(httpReq.URL.Path) + // Url decode + if file.Name != "" { + file.Name, _ = url.QueryUnescape(file.Name) + } } // unknown file filePath if file.Name == "" || file.Name == "/" || file.Name == "." { diff --git a/internal/protocol/http/fetcher_test.go b/internal/protocol/http/fetcher_test.go index b88f2a38..e8673c55 100644 --- a/internal/protocol/http/fetcher_test.go +++ b/internal/protocol/http/fetcher_test.go @@ -47,6 +47,16 @@ func TestFetcher_Resolve(t *testing.T) { }, }, }, t) + testResolve(test.StartTestCustomServer, "%E6%B5%8B%E8%AF%95.zip", &base.Resource{ + Size: 0, + Range: false, + Files: []*base.FileInfo{ + { + Name: "测试.zip", + Size: 0, + }, + }, + }, 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 86e841e4..85696748 100644 --- a/internal/test/httptest.go +++ b/internal/test/httptest.go @@ -94,6 +94,14 @@ func StartTestCustomServer() net.Listener { defer file.Close() io.Copy(writer, file) }) + mux.HandleFunc("/%E6%B5%8B%E8%AF%95.zip", func(writer http.ResponseWriter, request *http.Request) { + file, err := os.Open(BuildFile) + if err != nil { + panic(err) + } + defer file.Close() + io.Copy(writer, file) + }) return mux }) }