-
-
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.
- Loading branch information
Showing
9 changed files
with
143 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package coreapi | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"errors" | ||
|
||
"github.com/ipfs/go-path" | ||
peer "github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/routing" | ||
) | ||
|
||
type RoutingAPI CoreAPI | ||
|
||
func (r *RoutingAPI) Get(ctx context.Context, key string) ([]byte, error) { | ||
dhtKey, err := ensureIPNSKey(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx, cancel := context.WithCancel(ctx) | ||
ctx, events := routing.RegisterForQueryEvents(ctx) | ||
|
||
var getErr error | ||
|
||
go func() { | ||
defer cancel() | ||
var val []byte | ||
val, getErr = r.routing.GetValue(ctx, dhtKey) | ||
if getErr != nil { | ||
routing.PublishQueryEvent(ctx, &routing.QueryEvent{ | ||
Type: routing.QueryError, | ||
Extra: getErr.Error(), | ||
}) | ||
} else { | ||
routing.PublishQueryEvent(ctx, &routing.QueryEvent{ | ||
Type: routing.Value, | ||
Extra: base64.StdEncoding.EncodeToString(val), | ||
}) | ||
} | ||
}() | ||
|
||
for e := range events { | ||
if e.Type == routing.Value { | ||
return []byte(e.Extra), nil | ||
} | ||
} | ||
|
||
if err := ctx.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil, errors.New("key not found") | ||
} | ||
|
||
// Shamelessly (mostly) copied from commands/routing.go | ||
func ensureIPNSKey(s string) (string, error) { | ||
parts := path.SplitList(s) | ||
if len(parts) != 3 || | ||
parts[0] != "" || | ||
parts[1] != "ipns" { | ||
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,50 @@ | ||
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, 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 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