-
-
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
- Loading branch information
Showing
13 changed files
with
216 additions
and
111 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,71 @@ | ||
package corehttp | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
ipns_pb "github.com/ipfs/go-ipns/pb" | ||
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 | ||
} | ||
|
||
rawRecord, err := i.api.Routing().Get(ctx, key) | ||
if err != nil { | ||
webError(w, err.Error(), err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
var record ipns_pb.IpnsEntry | ||
err = proto.Unmarshal(rawRecord, &record) | ||
if err != nil { | ||
webError(w, err.Error(), err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Set cache control headers based on the TTL set in the IPNS record. If the | ||
// TTL is not present, we use the Last-Modified tag. We are tracking IPNS | ||
// caching on: https://github.com/ipfs/kubo/issues/1818. | ||
// TODO: use addCacheControlHeaders once #1818 is fixed. | ||
w.Header().Set("Etag", getEtag(r, resolvedPath.Cid())) | ||
if record.Ttl != nil { | ||
seconds := int(time.Duration(*record.Ttl).Seconds()) | ||
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", seconds)) | ||
} else { | ||
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat)) | ||
} | ||
|
||
// 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(rawRecord) | ||
} |
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.