-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gateway): IPNS record response format (IPIP-351)
- Loading branch information
Showing
13 changed files
with
199 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package coreapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/ipfs/go-path" | ||
coreiface "github.com/ipfs/interface-go-ipfs-core" | ||
peer "github.com/libp2p/go-libp2p/core/peer" | ||
) | ||
|
||
type RoutingAPI CoreAPI | ||
|
||
func (r *RoutingAPI) Get(ctx context.Context, key string) ([]byte, error) { | ||
if !r.nd.IsOnline { | ||
return nil, coreiface.ErrOffline | ||
} | ||
|
||
dhtKey, err := normalizeKey(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return r.routing.GetValue(ctx, dhtKey) | ||
} | ||
|
||
func (r *RoutingAPI) Put(ctx context.Context, key string, value []byte) error { | ||
if !r.nd.IsOnline { | ||
return coreiface.ErrOffline | ||
} | ||
|
||
dhtKey, err := normalizeKey(key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return r.routing.PutValue(ctx, dhtKey, value) | ||
} | ||
|
||
func normalizeKey(s string) (string, error) { | ||
parts := path.SplitList(s) | ||
if len(parts) != 3 || | ||
parts[0] != "" || | ||
!(parts[1] == "ipns" || parts[1] == "pk") { | ||
return "", errors.New("invalid key") | ||
} | ||
|
||
k, err := peer.Decode(parts[2]) | ||
if err != nil { | ||
return "", err | ||
} | ||
return path.Join(append(parts[:2], string(k))), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package corehttp | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
path "github.com/ipfs/go-path" | ||
ipath "github.com/ipfs/interface-go-ipfs-core/path" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (i *gatewayHandler) serveIpnsRecord(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time, logger *zap.SugaredLogger) { | ||
if contentPath.Namespace() != "ipns" { | ||
err := fmt.Errorf("%s is not an IPNS link", contentPath.String()) | ||
webError(w, err.Error(), err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
key := contentPath.String() | ||
key = strings.TrimSuffix(key, "/") | ||
if strings.Count(key, "/") > 2 { | ||
err := errors.New("cannot find ipns key for subpath") | ||
webError(w, err.Error(), err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
record, err := i.api.Routing().Get(ctx, key) | ||
if err != nil { | ||
webError(w, err.Error(), err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Set cache control headers. See the linked issue for improvements on | ||
// IPNS caching based on keys' TTL. | ||
// https://github.com/ipfs/kubo/issues/1818#issuecomment-1015849462 | ||
_ = addCacheControlHeaders(w, r, contentPath, resolvedPath.Cid()) | ||
|
||
// Set Content-Disposition | ||
var name string | ||
if urlFilename := r.URL.Query().Get("filename"); urlFilename != "" { | ||
name = urlFilename | ||
} else { | ||
name = path.SplitList(key)[2] + ".ipns-record" | ||
} | ||
setContentDispositionHeader(w, name, "attachment") | ||
|
||
w.Header().Set("Content-Type", "application/vnd.ipfs.ipns-record") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
|
||
_, _ = w.Write(record) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.