From cd5fd456672c1d086a33f9acd9f90c16e00fcb67 Mon Sep 17 00:00:00 2001 From: Levi Date: Sat, 5 Apr 2025 22:37:19 +0800 Subject: [PATCH] fix: resolve http non encode attachment filename (#975) --- internal/controller/controller.go | 6 ++++-- internal/protocol/http/fetcher.go | 14 ++++++++++++++ internal/protocol/http/fetcher_test.go | 10 ++++++++++ internal/test/httptest.go | 11 +++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/internal/controller/controller.go b/internal/controller/controller.go index f2c0d80d..dbaf2e6f 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -24,8 +24,10 @@ type DefaultFileController struct { func NewController() *Controller { return &Controller{ - GetConfig: func(v any) {}, - GetProxy: func(requestProxy *base.RequestProxy) func(*http.Request) (*url.URL, error) { return nil }, + GetConfig: func(v any) {}, + GetProxy: func(requestProxy *base.RequestProxy) func(*http.Request) (*url.URL, error) { + return requestProxy.ToHandler() + }, FileController: &DefaultFileController{}, } } diff --git a/internal/protocol/http/fetcher.go b/internal/protocol/http/fetcher.go index d464534e..ae480da4 100644 --- a/internal/protocol/http/fetcher.go +++ b/internal/protocol/http/fetcher.go @@ -174,6 +174,12 @@ func (f *Fetcher) Resolve(req *base.Request) error { } else { file.Name = filename } + } else { + substr := "attachment; filename=" + index := strings.Index(contentDisposition, substr) + if index != -1 { + file.Name = contentDisposition[index+len(substr):] + } } } // get file filePath by URL @@ -610,6 +616,14 @@ func (f *Fetcher) buildClient() *http.Client { } } +func decodeMangledString(mangled string) string { + rawBytes := make([]byte, 0, len(mangled)*3) + for _, r := range mangled { + rawBytes = append(rawBytes, byte(r)) + } + return string(rawBytes) +} + type fetcherData struct { Connections []*connection } diff --git a/internal/protocol/http/fetcher_test.go b/internal/protocol/http/fetcher_test.go index ea40f344..c89a83af 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, "no-encode", &base.Resource{ + Size: test.BuildSize, + Range: false, + Files: []*base.FileInfo{ + { + Name: "测试.zip", + Size: test.BuildSize, + }, + }, + }, t) testResolve(test.StartTestCustomServer, "%E6%B5%8B%E8%AF%95.zip", &base.Resource{ Size: 0, Range: false, diff --git a/internal/test/httptest.go b/internal/test/httptest.go index 54dec46e..9910b5bc 100644 --- a/internal/test/httptest.go +++ b/internal/test/httptest.go @@ -102,6 +102,17 @@ func StartTestCustomServer() net.Listener { defer file.Close() io.Copy(writer, file) }) + mux.HandleFunc("/no-encode", func(writer http.ResponseWriter, request *http.Request) { + writer.Header().Set("Content-Disposition", "attachment; filename=测试.zip") + 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 }) }