Skip to content

Commit fda35f4

Browse files
authored
fix: parse filename from response header (#399)
1 parent 957c061 commit fda35f4

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

pkg/net/http.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,7 @@ func (h *HTTPDownloader) DownloadFile() error {
156156
}
157157
}
158158

159-
if disposition, ok := resp.Header["Content-Disposition"]; ok && len(disposition) >= 1 {
160-
h.suggestedFilename = strings.TrimPrefix(disposition[0], `filename="`)
161-
h.suggestedFilename = strings.TrimSuffix(h.suggestedFilename, `"`)
162-
if h.suggestedFilename == filepath {
163-
h.suggestedFilename = ""
164-
}
165-
}
159+
h.suggestedFilename = ParseSuggestedFilename(resp.Header, filepath)
166160

167161
// pre-hook before get started to download file
168162
if h.PreStart != nil && !h.PreStart(resp) {
@@ -336,3 +330,17 @@ func DetectSizeWithRoundTripper(targetURL, output string, showProgress, noProxy,
336330
}
337331
return
338332
}
333+
334+
// ParseSuggestedFilename parse the filename from resp header
335+
func ParseSuggestedFilename(header http.Header, filepath string) (filename string) {
336+
if disposition, ok := header["Content-Disposition"]; ok && len(disposition) >= 1 {
337+
if index := strings.LastIndex(disposition[0], `filename="`); index != -1 {
338+
filename = disposition[0][index+len(`filename="`):]
339+
filename = strings.TrimSuffix(filename, `"`)
340+
if filename == filepath {
341+
filename = ""
342+
}
343+
}
344+
}
345+
return
346+
}

pkg/net/http_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,55 @@ func TestMultiThreadDownloader(t *testing.T) {
402402
})
403403
}
404404
}
405+
406+
func Test_getSuggestedFilename(t *testing.T) {
407+
type args struct {
408+
header http.Header
409+
filepath string
410+
}
411+
tests := []struct {
412+
name string
413+
args args
414+
wantFilename string
415+
}{
416+
{
417+
name: "test1",
418+
args: args{
419+
header: http.Header{"Content-Disposition": []string{`attachment; filename="harbor-helm-1.3.18.tar.gz"`}},
420+
filepath: "harbor.tar.gz",
421+
},
422+
wantFilename: "harbor-helm-1.3.18.tar.gz",
423+
},
424+
{
425+
name: "test2",
426+
args: args{
427+
header: http.Header{"Content-Disposition": []string{`attachment; filename="harbor-helm-1.3.18.tar.gz"`}},
428+
filepath: "harbor-helm-1.3.18.tar.gz",
429+
},
430+
wantFilename: "",
431+
},
432+
{
433+
name: "test3",
434+
args: args{
435+
header: http.Header{"Content-Disposition": []string{`filename="harbor-helm-1.3.18.tar.gz"`}},
436+
filepath: "harbor.tar.gz",
437+
},
438+
wantFilename: "harbor-helm-1.3.18.tar.gz",
439+
},
440+
{
441+
name: "test4",
442+
args: args{
443+
header: http.Header{"Content-Disposition": []string{`filename="harbor-helm-1.3.18.tar.gz"`}},
444+
filepath: "harbor-helm-1.3.18.tar.gz",
445+
},
446+
wantFilename: "",
447+
},
448+
}
449+
for _, tt := range tests {
450+
t.Run(tt.name, func(t *testing.T) {
451+
if gotFilename := net.ParseSuggestedFilename(tt.args.header, tt.args.filepath); gotFilename != tt.wantFilename {
452+
t.Errorf("getSuggestedFilename() = %v, want %v", gotFilename, tt.wantFilename)
453+
}
454+
})
455+
}
456+
}

0 commit comments

Comments
 (0)