@cryptotaxi247 / kubo / commits / 7b34b7f53

Set filename in Content-Disposition if filename=x is passed in URI query

License: MIT Signed-off-by: Iaroslav Gridin <voker57@gmail.com>

Iaroslav Gridin committed Aug 29, 2017 at 02:30 UTC 7b34b7f533bb1544472b3d40a33f3a403a7a29c8
2 files changed +14 -2
core/corehttp/gateway_handler.go
+9 -2
@@ -6,6 +6,7 @@ import (
6 "fmt"
7 "io"
8 "net/http"
9 + "net/url"
10 "os"
11 gopath "path"
12 "runtime/debug"
@@ -131,7 +132,6 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request)
132 }
133
134 func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
134 -
135 urlPath := r.URL.Path
136 escapedURLPath := r.URL.EscapedPath()
137
@@ -266,7 +266,14 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
266 }
267
268 if !dir {
269 - name := gopath.Base(urlPath)
269 + urlFilename := r.URL.Query().Get("filename")
270 + var name string
271 + if urlFilename != "" {
272 + w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename*=UTF-8''%s", url.PathEscape(urlFilename)))
273 + name = urlFilename
274 + } else {
275 + name = gopath.Base(urlPath)
276 + }
277 i.serveFile(w, r, name, modtime, dr)
278 return
279 }
test/sharness/t0110-gateway.sh
+5
@@ -31,6 +31,11 @@ test_expect_success "GET IPFS path succeeds" '
31 curl -sfo actual "http://127.0.0.1:$port/ipfs/$HASH"
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
37 +"
38 +
39 test_expect_success "GET IPFS path output looks good" '
40 test_cmp expected actual &&
41 rm actual