Skip to content

Commit

Permalink
Set filename in Content-Disposition if filename=x is passed in URI query
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Iaroslav Gridin <voker57@gmail.com>
  • Loading branch information
Voker57 committed Aug 29, 2017
1 parent 48476b2 commit aa8a81b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
gopath "path"
"runtime/debug"
Expand Down Expand Up @@ -132,7 +133,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()

Expand Down Expand Up @@ -267,7 +267,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
}
Expand Down
5 changes: 5 additions & 0 deletions test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aa8a81b

Please sign in to comment.