feat(gateway): Content-Disposition for legacy clients
This adds ASCII-only filename for clients that do not implement RFC 5987 Closes #7648
Marcin Rataj committed
Sep 16, 2020 at 22:39 UTC
19ec5f4a51e9c53c8d6b2df7d8472058d342b4c6
2 files changed
+10
-6
core/corehttp/gateway_handler.go
+5
-1
@@ -34,6 +34,8 @@ const (
34
ipnsPathPrefix = "/ipns/"
35
)
36
37
+var onlyAscii = regexp.MustCompile("[[:^ascii:]]")
38
+
39
// gatewayHandler is a HTTP handler that serves IPFS objects (accessible by default at /ipfs/<path>)
40
// (it serves requests like GET /ipfs/QmVRzPKPzNtSrEzBFm2UZfxmPAgnaLke4DMcerbsGGSaFe/link)
41
type gatewayHandler struct {
@@ -265,7 +267,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
267
if r.URL.Query().Get("download") == "true" {
268
disposition = "attachment"
269
}
268
- w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename*=UTF-8''%s", disposition, url.PathEscape(urlFilename)))
270
+ utf8Name := url.PathEscape(urlFilename)
271
+ asciiName := url.PathEscape(onlyAscii.ReplaceAllLiteralString(urlFilename, "_"))
272
+ w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"; filename*=UTF-8''%s", disposition, asciiName, utf8Name))
273
name = urlFilename
274
} else {
275
name = getFilename(urlPath)
test/sharness/t0110-gateway.sh
+5
-5
@@ -32,13 +32,13 @@ test_expect_success "GET IPFS path succeeds" '
32
'
33
34
test_expect_success "GET IPFS path with explicit ?filename succeeds with proper header" "
35
- curl -fo actual -D actual_headers 'http://127.0.0.1:$port/ipfs/$HASH?filename=testтест' &&
36
- grep -F \"Content-Disposition: inline; filename*=UTF-8''test%D1%82%D0%B5%D1%81%D1%82\" actual_headers
35
+ curl -fo actual -D actual_headers 'http://127.0.0.1:$port/ipfs/$HASH?filename=testтест.pdf' &&
36
+ grep -F 'Content-Disposition: inline; filename=\"test____.pdf\"; filename*=UTF-8'\'\''test%D1%82%D0%B5%D1%81%D1%82.pdf' actual_headers
37
"
38
39
-test_expect_success "GET IPFS path with explicit ?filename and download=true succeeds with proper header" "
40
- curl -fo actual -D actual_headers 'http://127.0.0.1:$port/ipfs/$HASH?filename=testтест&download=true' &&
41
- grep -F \"Content-Disposition: attachment; filename*=UTF-8''test%D1%82%D0%B5%D1%81%D1%82\" actual_headers
39
+test_expect_success "GET IPFS path with explicit ?filename and &download=true succeeds with proper header" "
40
+ curl -fo actual -D actual_headers 'http://127.0.0.1:$port/ipfs/$HASH?filename=testтест.mp4&download=true' &&
41
+ grep -F 'Content-Disposition: attachment; filename=\"test____.mp4\"; filename*=UTF-8'\'\''test%D1%82%D0%B5%D1%81%D1%82.mp4' actual_headers
42
"
43
44
# https://github.com/ipfs/go-ipfs/issues/4025#issuecomment-342250616