From 475fb8e35bf10712d269872b8ebf445455ccdb62 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 This commit was moved from ipfs/kubo@7b34b7f533bb1544472b3d40a33f3a403a7a29c8 --- gateway/core/corehttp/gateway_handler.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index bf164f9cb..caef0822d 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "net/url" "os" gopath "path" "runtime/debug" @@ -131,7 +132,6 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request) } func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - urlPath := r.URL.Path escapedURLPath := r.URL.EscapedPath() @@ -266,7 +266,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) + } i.serveFile(w, r, name, modtime, dr) return }