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

feat(routing/http/server): improve ux of /ipns #596

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The following emojis are used to highlight certain changes:
### Added

* `routing/http/server` now adds `Cache-Control` HTTP header to GET requests: 15 seconds for empty responses, or 5 minutes for responses with providers.
* `routing/http/server` the `/ipns` endpoint is more friendly to users opening URL in web browsers: returns `Content-Disposition` header and defaults to `application/vnd.ipfs.ipns-record` response when `Accept` is missing.

### Changed

Expand Down
16 changes: 14 additions & 2 deletions routing/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multibase"

logging "github.com/ipfs/go-log/v2"
)
Expand Down Expand Up @@ -370,8 +371,14 @@
}

func (s *server) GetIPNS(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept"), mediaTypeIPNSRecord) {
writeErr(w, "GetIPNS", http.StatusNotAcceptable, errors.New("content type in 'Accept' header is missing or not supported"))
acceptHdrValue := r.Header.Get("Accept")
// When 'Accept' header is missing, default to 'application/vnd.ipfs.ipns-record'
// (improved UX, similar to how we default to JSON response for /providers and /peers)
if len(acceptHdrValue) == 0 {
acceptHdrValue = mediaTypeIPNSRecord
}

Check warning on line 379 in routing/http/server/server.go

View check run for this annotation

Codecov / codecov/patch

routing/http/server/server.go#L378-L379

Added lines #L378 - L379 were not covered by tests
hacdias marked this conversation as resolved.
Show resolved Hide resolved
if !strings.Contains(acceptHdrValue, mediaTypeIPNSRecord) {
writeErr(w, "GetIPNS", http.StatusNotAcceptable, errors.New("content type in 'Accept' header is not supported, retry with 'application/vnd.ipfs.ipns-record'"))

Check warning on line 381 in routing/http/server/server.go

View check run for this annotation

Codecov / codecov/patch

routing/http/server/server.go#L381

Added line #L381 was not covered by tests
return
}

Expand Down Expand Up @@ -420,6 +427,11 @@

w.Header().Set("Etag", fmt.Sprintf(`"%x"`, xxhash.Sum64(rawRecord)))
w.Header().Set("Content-Type", mediaTypeIPNSRecord)

// Content-Disposition is not required, but improves UX by assigning a meaningful filename when opening URL in a web browser
if filename, err := cid.StringOfBase(multibase.Base36); err == nil {
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.ipns-record\"", filename))
}
w.Header().Add("Vary", "Accept")
w.Write(rawRecord)
}
Expand Down
2 changes: 2 additions & 0 deletions routing/http/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ func TestIPNS(t *testing.T) {

requireCloseToNow(t, resp.Header.Get("Last-Modified"))

require.Contains(t, resp.Header.Get("Content-Disposition"), `attachment; filename="`+name1.String()+`.ipns-record"`)

require.Contains(t, resp.Header.Get("Cache-Control"), "public, max-age=42")

// expected "stale" values are int(time.Until(eol).Seconds())
Expand Down
Loading