From 7462e4c9a900ac22cecaf57642ce958c1248852e Mon Sep 17 00:00:00 2001 From: Iaroslav Gridin Date: Tue, 29 Aug 2017 02:30:45 +0300 Subject: [PATCH] Set filename in Content-Disposition if filename=x is passed in URI query License: MIT Signed-off-by: Iaroslav Gridin --- core/corehttp/gateway_handler.go | 12 ++++++++++-- test/sharness/t0110-gateway.sh | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index a4677d09e980..91e20159ebc9 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + url "net/url" "os" gopath "path" "runtime/debug" @@ -132,7 +133,7 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request) } func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - + log.Errorf("see me?") urlPath := r.URL.Path escapedURLPath := r.URL.EscapedPath() @@ -267,7 +268,14 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr } if !dir { - name := gopath.Base(urlPath) + urlFilename := r.URL.Query().Get("filename") + var name string + if urlFilename != "" { + w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename*=UTF-8''%s", url.PathEscape(urlFilename))) + name = urlFilename + } else { + name = gopath.Base(urlPath) + } http.ServeContent(w, r, name, modtime, dr) return } diff --git a/test/sharness/t0110-gateway.sh b/test/sharness/t0110-gateway.sh index 484f830c527f..093c13bf5d19 100755 --- a/test/sharness/t0110-gateway.sh +++ b/test/sharness/t0110-gateway.sh @@ -31,6 +31,11 @@ test_expect_success "GET IPFS path succeeds" ' curl -sfo actual "http://127.0.0.1:$port/ipfs/$HASH" ' +test_expect_success "GET IPFS path with explicit filename succeeds with proper header" " + curl -fo actual -D actual_headers 'http://127.0.0.1:$port/ipfs/$HASH?filename=testтест' && + grep -F \"Content-Disposition: inline; filename*=UTF-8''test%D1%82%D0%B5%D1%81%D1%82\" actual_headers +" + test_expect_success "GET IPFS path output looks good" ' test_cmp expected actual && rm actual