Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: honour url filename when downloading as CAR/BLOCK #9028

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion core/corehttp/gateway_handler_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ func (i *gatewayHandler) serveRawBlock(ctx context.Context, w http.ResponseWrite
content := bytes.NewReader(block)

// Set Content-Disposition
name := blockCid.String() + ".bin"
var name string
if urlFilename := r.URL.Query().Get("filename"); urlFilename != "" {
name = urlFilename
} else {
name = blockCid.String() + ".bin"
}
setContentDispositionHeader(w, name, "attachment")

// Set remaining headers
Expand Down
7 changes: 6 additions & 1 deletion core/corehttp/gateway_handler_car.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ func (i *gatewayHandler) serveCAR(ctx context.Context, w http.ResponseWriter, r
rootCid := resolvedPath.Cid()

// Set Content-Disposition
name := rootCid.String() + ".car"
var name string
if urlFilename := r.URL.Query().Get("filename"); urlFilename != "" {
name = urlFilename
} else {
name = rootCid.String() + ".car"
}
setContentDispositionHeader(w, name, "attachment")

// Weak Etag W/ because we can't guarantee byte-for-byte identical responses
Expand Down
6 changes: 6 additions & 0 deletions test/sharness/t0117-gateway-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ test_expect_success "Create text fixtures" '
grep "< X-Content-Type-Options: nosniff" curl_output
'

test_expect_success "GET for application/vnd.ipld.raw with query filename includes Content-Disposition with custom filename" '
curl -svX GET -H "Accept: application/vnd.ipld.raw" "http://127.0.0.1:$GWAY_PORT/ipfs/$ROOT_DIR_CID/dir/ascii.txt?filename=foobar.bin" >/dev/null 2>curl_output_filename &&
cat curl_output_filename &&
grep "< Content-Disposition: attachment\; filename=\"foobar.bin\"" curl_output_filename
'

# Cache control HTTP headers
# (basic checks, detailed behavior is tested in t0116-gateway-cache.sh)

Expand Down
6 changes: 6 additions & 0 deletions test/sharness/t0118-gateway-car.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ test_launch_ipfs_daemon_without_network
grep "< Accept-Ranges: none" curl_output
'

test_expect_success "GET for application/vnd.ipld.car with query filename includes Content-Disposition with custom filename" '
curl -svX GET -H "Accept: application/vnd.ipld.car" "http://127.0.0.1:$GWAY_PORT/ipfs/$ROOT_DIR_CID/subdir/ascii.txt?filename=foobar.car" > curl_output_filename 2>&1 &&
cat curl_output_filename &&
grep "< Content-Disposition: attachment\; filename=\"foobar.car\"" curl_output_filename
'

# Cache control HTTP headers

test_expect_success "GET response for application/vnd.ipld.car includes a weak Etag" '
Expand Down