mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-09 18:10:29 +00:00
fix: use PathUnescape for filename URL decoding (#1214)
This commit is contained in:
1 parent
71c9e6947a
commit
6756d82ffa
5 files changed
+98
-6
No files matched your search
@@ -171,9 +171,9 @@ 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
|
||||
// Url decode - use PathUnescape for paths to handle %2B correctly
|
||||
if file.Name != "" {
|
||||
file.Name, _ = url.QueryUnescape(file.Name)
|
||||
file.Name, _ = url.PathUnescape(file.Name)
|
||||
}
|
||||
}
|
||||
// unknown file filePath
|
||||
@@ -656,7 +656,8 @@ func parseFilenameExtended(cd string) string {
|
||||
if len(parts) == 2 {
|
||||
// parts[0] is charset (e.g., "UTF-8")
|
||||
// parts[1] is percent-encoded value
|
||||
decoded, err := url.QueryUnescape(parts[1])
|
||||
// Use PathUnescape to handle %2B correctly (should decode to +, not space)
|
||||
decoded, err := url.PathUnescape(parts[1])
|
||||
if err == nil {
|
||||
return decoded
|
||||
}
|
||||
@@ -665,7 +666,8 @@ func parseFilenameExtended(cd string) string {
|
||||
// Try with single quote delimiter as well (some servers use this)
|
||||
parts = strings.SplitN(value, "'", 3)
|
||||
if len(parts) >= 3 {
|
||||
decoded, err := url.QueryUnescape(parts[2])
|
||||
// Use PathUnescape to handle %2B correctly (should decode to +, not space)
|
||||
decoded, err := url.PathUnescape(parts[2])
|
||||
if err == nil {
|
||||
return decoded
|
||||
}
|
||||
@@ -687,8 +689,8 @@ func decodeFilenameParam(filename string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// Try URL decoding
|
||||
decoded := util.TryUrlQueryUnescape(filename)
|
||||
// Try URL decoding - use PathUnescape for filenames to handle %2B correctly
|
||||
decoded := util.TryUrlPathUnescape(filename)
|
||||
|
||||
// Check if the result is valid UTF-8. If not, try GBK decoding.
|
||||
// This handles the case where Chinese Windows servers send GBK-encoded filenames
|
||||
|
||||
@@ -113,6 +113,32 @@ func TestFetcher_Resolve(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}, t)
|
||||
// Test filename with plus signs (e.g., C++ Primer)
|
||||
// Before fix: %2B decoded to space -> "C++ Primer" became "C Primer"
|
||||
// After fix: %2B correctly decoded to + -> "C++ Primer Plus.mobi"
|
||||
testResolve(test.StartTestCustomServer, "plus-sign-encoded", &base.Resource{
|
||||
Size: test.BuildSize,
|
||||
Range: false,
|
||||
Files: []*base.FileInfo{
|
||||
{
|
||||
Name: "C++ Primer Plus.mobi",
|
||||
Size: test.BuildSize,
|
||||
},
|
||||
},
|
||||
}, t)
|
||||
// Test filename with plus sign in URL path
|
||||
// Before fix: %2B decoded to space
|
||||
// After fix: %2B correctly decoded to +
|
||||
testResolve(test.StartTestCustomServer, "C%2B%2B%20Primer.txt", &base.Resource{
|
||||
Size: 0,
|
||||
Range: false,
|
||||
Files: []*base.FileInfo{
|
||||
{
|
||||
Name: "C++ Primer.txt",
|
||||
Size: 0,
|
||||
},
|
||||
},
|
||||
}, t)
|
||||
|
||||
// Test URL without file path - should use domain/host as filename
|
||||
listener := test.StartTestRootServer()
|
||||
|
||||
Reference in new issue
Block a user