2016-08-29 22:18:00 +03:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-06-20 15:06:27 +03:00
|
|
|
"context"
|
2016-08-29 22:18:00 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-06-20 15:06:27 +03:00
|
|
|
"math/big"
|
2017-01-26 22:33:39 +03:00
|
|
|
"net/http"
|
2017-12-05 00:56:52 +03:00
|
|
|
"path"
|
2016-08-29 22:18:00 +03:00
|
|
|
"strings"
|
|
|
|
|
2017-04-12 03:06:02 +03:00
|
|
|
"bytes"
|
|
|
|
"mime"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
2017-05-13 03:02:25 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-06-20 15:06:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/contracts/ens"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-02-23 16:19:59 +03:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
2018-06-20 15:06:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/log"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/multihash"
|
2017-05-13 03:02:25 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
2018-06-20 15:06:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage/mru"
|
2016-08-29 22:18:00 +03:00
|
|
|
)
|
|
|
|
|
2018-02-23 16:19:59 +03:00
|
|
|
var (
|
|
|
|
apiResolveCount = metrics.NewRegisteredCounter("api.resolve.count", nil)
|
|
|
|
apiResolveFail = metrics.NewRegisteredCounter("api.resolve.fail", nil)
|
|
|
|
apiPutCount = metrics.NewRegisteredCounter("api.put.count", nil)
|
|
|
|
apiPutFail = metrics.NewRegisteredCounter("api.put.fail", nil)
|
|
|
|
apiGetCount = metrics.NewRegisteredCounter("api.get.count", nil)
|
|
|
|
apiGetNotFound = metrics.NewRegisteredCounter("api.get.notfound", nil)
|
2018-06-20 15:06:27 +03:00
|
|
|
apiGetHTTP300 = metrics.NewRegisteredCounter("api.get.http.300", nil)
|
2018-02-23 16:19:59 +03:00
|
|
|
apiModifyCount = metrics.NewRegisteredCounter("api.modify.count", nil)
|
|
|
|
apiModifyFail = metrics.NewRegisteredCounter("api.modify.fail", nil)
|
|
|
|
apiAddFileCount = metrics.NewRegisteredCounter("api.addfile.count", nil)
|
|
|
|
apiAddFileFail = metrics.NewRegisteredCounter("api.addfile.fail", nil)
|
|
|
|
apiRmFileCount = metrics.NewRegisteredCounter("api.removefile.count", nil)
|
|
|
|
apiRmFileFail = metrics.NewRegisteredCounter("api.removefile.fail", nil)
|
|
|
|
apiAppendFileCount = metrics.NewRegisteredCounter("api.appendfile.count", nil)
|
|
|
|
apiAppendFileFail = metrics.NewRegisteredCounter("api.appendfile.fail", nil)
|
2018-06-20 15:06:27 +03:00
|
|
|
apiGetInvalid = metrics.NewRegisteredCounter("api.get.invalid", nil)
|
2018-02-23 16:19:59 +03:00
|
|
|
)
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// Resolver interface resolve a domain name to a hash using ENS
|
2016-08-29 22:18:00 +03:00
|
|
|
type Resolver interface {
|
|
|
|
Resolve(string) (common.Hash, error)
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// ResolveValidator is used to validate the contained Resolver
|
|
|
|
type ResolveValidator interface {
|
|
|
|
Resolver
|
|
|
|
Owner(node [32]byte) (common.Address, error)
|
|
|
|
HeaderByNumber(context.Context, *big.Int) (*types.Header, error)
|
|
|
|
}
|
|
|
|
|
2017-12-19 01:07:48 +03:00
|
|
|
// NoResolverError is returned by MultiResolver.Resolve if no resolver
|
2017-12-01 12:32:14 +03:00
|
|
|
// can be found for the address.
|
2017-12-19 01:07:48 +03:00
|
|
|
type NoResolverError struct {
|
|
|
|
TLD string
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// NewNoResolverError creates a NoResolverError for the given top level domain
|
2017-12-19 01:07:48 +03:00
|
|
|
func NewNoResolverError(tld string) *NoResolverError {
|
|
|
|
return &NoResolverError{TLD: tld}
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// Error NoResolverError implements error
|
2017-12-19 01:07:48 +03:00
|
|
|
func (e *NoResolverError) Error() string {
|
|
|
|
if e.TLD == "" {
|
|
|
|
return "no ENS resolver"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("no ENS endpoint configured to resolve .%s TLD names", e.TLD)
|
|
|
|
}
|
2017-12-01 12:32:14 +03:00
|
|
|
|
|
|
|
// MultiResolver is used to resolve URL addresses based on their TLDs.
|
|
|
|
// Each TLD can have multiple resolvers, and the resoluton from the
|
|
|
|
// first one in the sequence will be returned.
|
|
|
|
type MultiResolver struct {
|
2018-06-20 15:06:27 +03:00
|
|
|
resolvers map[string][]ResolveValidator
|
|
|
|
nameHash func(string) common.Hash
|
2017-12-01 12:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MultiResolverOption sets options for MultiResolver and is used as
|
|
|
|
// arguments for its constructor.
|
|
|
|
type MultiResolverOption func(*MultiResolver)
|
|
|
|
|
|
|
|
// MultiResolverOptionWithResolver adds a Resolver to a list of resolvers
|
|
|
|
// for a specific TLD. If TLD is an empty string, the resolver will be added
|
|
|
|
// to the list of default resolver, the ones that will be used for resolution
|
|
|
|
// of addresses which do not have their TLD resolver specified.
|
2018-06-20 15:06:27 +03:00
|
|
|
func MultiResolverOptionWithResolver(r ResolveValidator, tld string) MultiResolverOption {
|
2017-12-01 12:32:14 +03:00
|
|
|
return func(m *MultiResolver) {
|
|
|
|
m.resolvers[tld] = append(m.resolvers[tld], r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// MultiResolverOptionWithNameHash is unused at the time of this writing
|
|
|
|
func MultiResolverOptionWithNameHash(nameHash func(string) common.Hash) MultiResolverOption {
|
|
|
|
return func(m *MultiResolver) {
|
|
|
|
m.nameHash = nameHash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:32:14 +03:00
|
|
|
// NewMultiResolver creates a new instance of MultiResolver.
|
|
|
|
func NewMultiResolver(opts ...MultiResolverOption) (m *MultiResolver) {
|
|
|
|
m = &MultiResolver{
|
2018-06-20 15:06:27 +03:00
|
|
|
resolvers: make(map[string][]ResolveValidator),
|
|
|
|
nameHash: ens.EnsNode,
|
2017-12-01 12:32:14 +03:00
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(m)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve resolves address by choosing a Resolver by TLD.
|
|
|
|
// If there are more default Resolvers, or for a specific TLD,
|
|
|
|
// the Hash from the the first one which does not return error
|
|
|
|
// will be returned.
|
2018-06-20 15:06:27 +03:00
|
|
|
func (m *MultiResolver) Resolve(addr string) (h common.Hash, err error) {
|
|
|
|
rs, err := m.getResolveValidator(addr)
|
|
|
|
if err != nil {
|
|
|
|
return h, err
|
2017-12-01 12:32:14 +03:00
|
|
|
}
|
|
|
|
for _, r := range rs {
|
|
|
|
h, err = r.Resolve(addr)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// ValidateOwner checks the ENS to validate that the owner of the given domain is the given eth address
|
|
|
|
func (m *MultiResolver) ValidateOwner(name string, address common.Address) (bool, error) {
|
|
|
|
rs, err := m.getResolveValidator(name)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
var addr common.Address
|
|
|
|
for _, r := range rs {
|
|
|
|
addr, err = r.Owner(m.nameHash(name))
|
|
|
|
// we hide the error if it is not for the last resolver we check
|
|
|
|
if err == nil {
|
|
|
|
return addr == address, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeaderByNumber uses the validator of the given domainname and retrieves the header for the given block number
|
|
|
|
func (m *MultiResolver) HeaderByNumber(ctx context.Context, name string, blockNr *big.Int) (*types.Header, error) {
|
|
|
|
rs, err := m.getResolveValidator(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, r := range rs {
|
|
|
|
var header *types.Header
|
|
|
|
header, err = r.HeaderByNumber(ctx, blockNr)
|
|
|
|
// we hide the error if it is not for the last resolver we check
|
|
|
|
if err == nil {
|
|
|
|
return header, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// getResolveValidator uses the hostname to retrieve the resolver associated with the top level domain
|
|
|
|
func (m *MultiResolver) getResolveValidator(name string) ([]ResolveValidator, error) {
|
|
|
|
rs := m.resolvers[""]
|
|
|
|
tld := path.Ext(name)
|
|
|
|
if tld != "" {
|
|
|
|
tld = tld[1:]
|
|
|
|
rstld, ok := m.resolvers[tld]
|
|
|
|
if ok {
|
|
|
|
return rstld, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(rs) == 0 {
|
|
|
|
return rs, NewNoResolverError(tld)
|
|
|
|
}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNameHash sets the hasher function that hashes the domain into a name hash that ENS uses
|
|
|
|
func (m *MultiResolver) SetNameHash(nameHash func(string) common.Hash) {
|
|
|
|
m.nameHash = nameHash
|
|
|
|
}
|
|
|
|
|
2016-08-29 22:18:00 +03:00
|
|
|
/*
|
2018-06-20 15:06:27 +03:00
|
|
|
API implements webserver/file system related content storage and retrieval
|
|
|
|
on top of the FileStore
|
|
|
|
it is the public interface of the FileStore which is included in the ethereum stack
|
2016-08-29 22:18:00 +03:00
|
|
|
*/
|
2018-06-20 15:06:27 +03:00
|
|
|
type API struct {
|
|
|
|
resource *mru.Handler
|
|
|
|
fileStore *storage.FileStore
|
|
|
|
dns Resolver
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// NewAPI the api constructor initialises a new API instance.
|
|
|
|
func NewAPI(fileStore *storage.FileStore, dns Resolver, resourceHandler *mru.Handler) (self *API) {
|
|
|
|
self = &API{
|
|
|
|
fileStore: fileStore,
|
|
|
|
dns: dns,
|
|
|
|
resource: resourceHandler,
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// Upload to be used only in TEST
|
|
|
|
func (a *API) Upload(uploadDir, index string, toEncrypt bool) (hash string, err error) {
|
|
|
|
fs := NewFileSystem(a)
|
|
|
|
hash, err = fs.Upload(uploadDir, index, toEncrypt)
|
2017-04-12 03:06:02 +03:00
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// Retrieve FileStore reader API
|
|
|
|
func (a *API) Retrieve(addr storage.Address) (reader storage.LazySectionReader, isEncrypted bool) {
|
|
|
|
return a.fileStore.Retrieve(addr)
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// Store wraps the Store API call of the embedded FileStore
|
|
|
|
func (a *API) Store(data io.Reader, size int64, toEncrypt bool) (addr storage.Address, wait func(), err error) {
|
|
|
|
log.Debug("api.store", "size", size)
|
|
|
|
return a.fileStore.Store(data, size, toEncrypt)
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// ErrResolve is returned when an URI cannot be resolved from ENS.
|
2016-08-29 22:18:00 +03:00
|
|
|
type ErrResolve error
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// Resolve resolves a URI to an Address using the MultiResolver.
|
|
|
|
func (a *API) Resolve(uri *URI) (storage.Address, error) {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiResolveCount.Inc(1)
|
2018-06-20 15:06:27 +03:00
|
|
|
log.Trace("resolving", "uri", uri.Addr)
|
2017-04-13 12:06:19 +03:00
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// if the URI is immutable, check if the address looks like a hash
|
|
|
|
if uri.Immutable() {
|
|
|
|
key := uri.Address()
|
|
|
|
if key == nil {
|
2017-05-13 03:02:25 +03:00
|
|
|
return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr)
|
2017-04-13 12:06:19 +03:00
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
return key, nil
|
2017-04-13 12:06:19 +03:00
|
|
|
}
|
2017-05-13 03:02:25 +03:00
|
|
|
|
|
|
|
// if DNS is not configured, check if the address is a hash
|
2018-06-20 15:06:27 +03:00
|
|
|
if a.dns == nil {
|
|
|
|
key := uri.Address()
|
|
|
|
if key == nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiResolveFail.Inc(1)
|
2017-05-13 03:02:25 +03:00
|
|
|
return nil, fmt.Errorf("no DNS to resolve name: %q", uri.Addr)
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
return key, nil
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
2017-05-13 03:02:25 +03:00
|
|
|
|
|
|
|
// try and resolve the address
|
2018-06-20 15:06:27 +03:00
|
|
|
resolved, err := a.dns.Resolve(uri.Addr)
|
2017-05-13 03:02:25 +03:00
|
|
|
if err == nil {
|
|
|
|
return resolved[:], nil
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
key := uri.Address()
|
|
|
|
if key == nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiResolveFail.Inc(1)
|
2017-05-13 03:02:25 +03:00
|
|
|
return nil, err
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
return key, nil
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// Put provides singleton manifest creation on top of FileStore store
|
|
|
|
func (a *API) Put(content, contentType string, toEncrypt bool) (k storage.Address, wait func(), err error) {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiPutCount.Inc(1)
|
2016-08-29 22:18:00 +03:00
|
|
|
r := strings.NewReader(content)
|
2018-06-20 15:06:27 +03:00
|
|
|
key, waitContent, err := a.fileStore.Store(r, int64(len(content)), toEncrypt)
|
2016-08-29 22:18:00 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiPutFail.Inc(1)
|
2018-06-20 15:06:27 +03:00
|
|
|
return nil, nil, err
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
manifest := fmt.Sprintf(`{"entries":[{"hash":"%v","contentType":"%s"}]}`, key, contentType)
|
|
|
|
r = strings.NewReader(manifest)
|
2018-06-20 15:06:27 +03:00
|
|
|
key, waitManifest, err := a.fileStore.Store(r, int64(len(manifest)), toEncrypt)
|
2016-08-29 22:18:00 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiPutFail.Inc(1)
|
2018-06-20 15:06:27 +03:00
|
|
|
return nil, nil, err
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
return key, func() {
|
|
|
|
waitContent()
|
|
|
|
waitManifest()
|
|
|
|
}, nil
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get uses iterative manifest retrieval and prefix matching
|
2018-06-20 15:06:27 +03:00
|
|
|
// to resolve basePath to content using FileStore retrieve
|
|
|
|
// it returns a section reader, mimeType, status, the key of the actual content and an error
|
|
|
|
func (a *API) Get(manifestAddr storage.Address, path string) (reader storage.LazySectionReader, mimeType string, status int, contentAddr storage.Address, err error) {
|
|
|
|
log.Debug("api.get", "key", manifestAddr, "path", path)
|
2018-02-23 16:19:59 +03:00
|
|
|
apiGetCount.Inc(1)
|
2018-06-20 15:06:27 +03:00
|
|
|
trie, err := loadManifest(a.fileStore, manifestAddr, nil)
|
2016-08-29 22:18:00 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiGetNotFound.Inc(1)
|
2017-09-08 21:29:09 +03:00
|
|
|
status = http.StatusNotFound
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Warn(fmt.Sprintf("loadManifestTrie error: %v", err))
|
2016-08-29 22:18:00 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
log.Debug("trie getting entry", "key", manifestAddr, "path", path)
|
2016-08-29 22:18:00 +03:00
|
|
|
entry, _ := trie.getEntry(path)
|
2017-01-26 22:33:39 +03:00
|
|
|
|
2016-08-29 22:18:00 +03:00
|
|
|
if entry != nil {
|
2018-06-20 15:06:27 +03:00
|
|
|
log.Debug("trie got entry", "key", manifestAddr, "path", path, "entry.Hash", entry.Hash)
|
|
|
|
// we need to do some extra work if this is a mutable resource manifest
|
|
|
|
if entry.ContentType == ResourceContentType {
|
|
|
|
|
|
|
|
// get the resource root chunk key
|
|
|
|
log.Trace("resource type", "key", manifestAddr, "hash", entry.Hash)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
rsrc, err := a.resource.Load(storage.Address(common.FromHex(entry.Hash)))
|
|
|
|
if err != nil {
|
|
|
|
apiGetNotFound.Inc(1)
|
|
|
|
status = http.StatusNotFound
|
|
|
|
log.Debug(fmt.Sprintf("get resource content error: %v", err))
|
|
|
|
return reader, mimeType, status, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// use this key to retrieve the latest update
|
|
|
|
rsrc, err = a.resource.LookupLatest(ctx, rsrc.NameHash(), true, &mru.LookupParams{})
|
|
|
|
if err != nil {
|
|
|
|
apiGetNotFound.Inc(1)
|
|
|
|
status = http.StatusNotFound
|
|
|
|
log.Debug(fmt.Sprintf("get resource content error: %v", err))
|
|
|
|
return reader, mimeType, status, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's multihash, we will transparently serve the content this multihash points to
|
|
|
|
// \TODO this resolve is rather expensive all in all, review to see if it can be achieved cheaper
|
|
|
|
if rsrc.Multihash {
|
|
|
|
|
|
|
|
// get the data of the update
|
|
|
|
_, rsrcData, err := a.resource.GetContent(rsrc.NameHash().Hex())
|
|
|
|
if err != nil {
|
|
|
|
apiGetNotFound.Inc(1)
|
|
|
|
status = http.StatusNotFound
|
|
|
|
log.Warn(fmt.Sprintf("get resource content error: %v", err))
|
|
|
|
return reader, mimeType, status, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate that data as multihash
|
|
|
|
decodedMultihash, err := multihash.FromMultihash(rsrcData)
|
|
|
|
if err != nil {
|
|
|
|
apiGetInvalid.Inc(1)
|
|
|
|
status = http.StatusUnprocessableEntity
|
|
|
|
log.Warn("invalid resource multihash", "err", err)
|
|
|
|
return reader, mimeType, status, nil, err
|
|
|
|
}
|
|
|
|
manifestAddr = storage.Address(decodedMultihash)
|
|
|
|
log.Trace("resource is multihash", "key", manifestAddr)
|
|
|
|
|
|
|
|
// get the manifest the multihash digest points to
|
|
|
|
trie, err := loadManifest(a.fileStore, manifestAddr, nil)
|
|
|
|
if err != nil {
|
|
|
|
apiGetNotFound.Inc(1)
|
|
|
|
status = http.StatusNotFound
|
|
|
|
log.Warn(fmt.Sprintf("loadManifestTrie (resource multihash) error: %v", err))
|
|
|
|
return reader, mimeType, status, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally, get the manifest entry
|
|
|
|
// it will always be the entry on path ""
|
|
|
|
entry, _ = trie.getEntry(path)
|
|
|
|
if entry == nil {
|
|
|
|
status = http.StatusNotFound
|
|
|
|
apiGetNotFound.Inc(1)
|
|
|
|
err = fmt.Errorf("manifest (resource multihash) entry for '%s' not found", path)
|
|
|
|
log.Trace("manifest (resource multihash) entry not found", "key", manifestAddr, "path", path)
|
|
|
|
return reader, mimeType, status, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// data is returned verbatim since it's not a multihash
|
|
|
|
return rsrc, "application/octet-stream", http.StatusOK, nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// regardless of resource update manifests or normal manifests we will converge at this point
|
|
|
|
// get the key the manifest entry points to and serve it if it's unambiguous
|
|
|
|
contentAddr = common.Hex2Bytes(entry.Hash)
|
2016-08-29 22:18:00 +03:00
|
|
|
status = entry.Status
|
2017-10-06 16:45:54 +03:00
|
|
|
if status == http.StatusMultipleChoices {
|
2018-06-20 15:06:27 +03:00
|
|
|
apiGetHTTP300.Inc(1)
|
|
|
|
return nil, entry.ContentType, status, contentAddr, err
|
2017-10-06 16:45:54 +03:00
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
mimeType = entry.ContentType
|
|
|
|
log.Debug("content lookup key", "key", contentAddr, "mimetype", mimeType)
|
|
|
|
reader, _ = a.fileStore.Retrieve(contentAddr)
|
2016-08-29 22:18:00 +03:00
|
|
|
} else {
|
2018-06-20 15:06:27 +03:00
|
|
|
// no entry found
|
2017-01-26 22:33:39 +03:00
|
|
|
status = http.StatusNotFound
|
2018-02-23 16:19:59 +03:00
|
|
|
apiGetNotFound.Inc(1)
|
2016-08-29 22:18:00 +03:00
|
|
|
err = fmt.Errorf("manifest entry for '%s' not found", path)
|
2018-06-20 15:06:27 +03:00
|
|
|
log.Trace("manifest entry not found", "key", contentAddr, "path", path)
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// Modify loads manifest and checks the content hash before recalculating and storing the manifest.
|
|
|
|
func (a *API) Modify(addr storage.Address, path, contentHash, contentType string) (storage.Address, error) {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiModifyCount.Inc(1)
|
2016-08-29 22:18:00 +03:00
|
|
|
quitC := make(chan bool)
|
2018-06-20 15:06:27 +03:00
|
|
|
trie, err := loadManifest(a.fileStore, addr, quitC)
|
2016-08-29 22:18:00 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiModifyFail.Inc(1)
|
2017-04-07 01:22:22 +03:00
|
|
|
return nil, err
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
if contentHash != "" {
|
2017-04-07 01:22:22 +03:00
|
|
|
entry := newManifestTrieEntry(&ManifestEntry{
|
2016-08-29 22:18:00 +03:00
|
|
|
Path: path,
|
|
|
|
ContentType: contentType,
|
2017-04-07 01:22:22 +03:00
|
|
|
}, nil)
|
|
|
|
entry.Hash = contentHash
|
2016-08-29 22:18:00 +03:00
|
|
|
trie.addEntry(entry, quitC)
|
|
|
|
} else {
|
|
|
|
trie.deleteEntry(path, quitC)
|
|
|
|
}
|
|
|
|
|
2017-04-07 01:22:22 +03:00
|
|
|
if err := trie.recalcAndStore(); err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiModifyFail.Inc(1)
|
2017-04-07 01:22:22 +03:00
|
|
|
return nil, err
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
return trie.ref, nil
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
2017-04-12 03:06:02 +03:00
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// AddFile creates a new manifest entry, adds it to swarm, then adds a file to swarm.
|
|
|
|
func (a *API) AddFile(mhash, path, fname string, content []byte, nameresolver bool) (storage.Address, string, error) {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAddFileCount.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
|
|
|
|
uri, err := Parse("bzz:/" + mhash)
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAddFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
mkey, err := a.Resolve(uri)
|
2017-04-12 03:06:02 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAddFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim the root dir we added
|
|
|
|
if path[:1] == "/" {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &ManifestEntry{
|
|
|
|
Path: filepath.Join(path, fname),
|
|
|
|
ContentType: mime.TypeByExtension(filepath.Ext(fname)),
|
|
|
|
Mode: 0700,
|
|
|
|
Size: int64(len(content)),
|
|
|
|
ModTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
mw, err := a.NewManifestWriter(mkey, nil)
|
2017-04-12 03:06:02 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAddFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
fkey, err := mw.AddEntry(bytes.NewReader(content), entry)
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAddFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMkey, err := mw.Store()
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAddFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return fkey, newMkey.String(), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// RemoveFile removes a file entry in a manifest.
|
|
|
|
func (a *API) RemoveFile(mhash, path, fname string, nameresolver bool) (string, error) {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiRmFileCount.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
|
|
|
|
uri, err := Parse("bzz:/" + mhash)
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiRmFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return "", err
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
mkey, err := a.Resolve(uri)
|
2017-04-12 03:06:02 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiRmFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim the root dir we added
|
|
|
|
if path[:1] == "/" {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
mw, err := a.NewManifestWriter(mkey, nil)
|
2017-04-12 03:06:02 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiRmFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mw.RemoveEntry(filepath.Join(path, fname))
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiRmFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMkey, err := mw.Store()
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiRmFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return newMkey.String(), nil
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// AppendFile removes old manifest, appends file entry to new manifest and adds it to Swarm.
|
|
|
|
func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content []byte, oldAddr storage.Address, offset int64, addSize int64, nameresolver bool) (storage.Address, string, error) {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAppendFileCount.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
|
|
|
|
buffSize := offset + addSize
|
|
|
|
if buffSize < existingSize {
|
|
|
|
buffSize = existingSize
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, buffSize)
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
oldReader, _ := a.Retrieve(oldAddr)
|
2017-04-12 03:06:02 +03:00
|
|
|
io.ReadAtLeast(oldReader, buf, int(offset))
|
|
|
|
|
|
|
|
newReader := bytes.NewReader(content)
|
|
|
|
io.ReadAtLeast(newReader, buf[offset:], int(addSize))
|
|
|
|
|
|
|
|
if buffSize < existingSize {
|
|
|
|
io.ReadAtLeast(oldReader, buf[addSize:], int(buffSize))
|
|
|
|
}
|
|
|
|
|
|
|
|
combinedReader := bytes.NewReader(buf)
|
|
|
|
totalSize := int64(len(buf))
|
|
|
|
|
|
|
|
// TODO(jmozah): to append using pyramid chunker when it is ready
|
2018-06-20 15:06:27 +03:00
|
|
|
//oldReader := a.Retrieve(oldKey)
|
2017-04-12 03:06:02 +03:00
|
|
|
//newReader := bytes.NewReader(content)
|
|
|
|
//combinedReader := io.MultiReader(oldReader, newReader)
|
|
|
|
|
|
|
|
uri, err := Parse("bzz:/" + mhash)
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAppendFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
mkey, err := a.Resolve(uri)
|
2017-04-12 03:06:02 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAppendFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim the root dir we added
|
|
|
|
if path[:1] == "/" {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
mw, err := a.NewManifestWriter(mkey, nil)
|
2017-04-12 03:06:02 +03:00
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAppendFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mw.RemoveEntry(filepath.Join(path, fname))
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAppendFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &ManifestEntry{
|
|
|
|
Path: filepath.Join(path, fname),
|
|
|
|
ContentType: mime.TypeByExtension(filepath.Ext(fname)),
|
|
|
|
Mode: 0700,
|
|
|
|
Size: totalSize,
|
|
|
|
ModTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
fkey, err := mw.AddEntry(io.Reader(combinedReader), entry)
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAppendFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMkey, err := mw.Store()
|
|
|
|
if err != nil {
|
2018-02-23 16:19:59 +03:00
|
|
|
apiAppendFileFail.Inc(1)
|
2017-04-12 03:06:02 +03:00
|
|
|
return nil, "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return fkey, newMkey.String(), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// BuildDirectoryTree used by swarmfs_unix
|
|
|
|
func (a *API) BuildDirectoryTree(mhash string, nameresolver bool) (addr storage.Address, manifestEntryMap map[string]*manifestTrieEntry, err error) {
|
2018-02-23 16:19:59 +03:00
|
|
|
|
2017-04-12 03:06:02 +03:00
|
|
|
uri, err := Parse("bzz:/" + mhash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
addr, err = a.Resolve(uri)
|
2017-04-12 03:06:02 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
quitC := make(chan bool)
|
2018-06-20 15:06:27 +03:00
|
|
|
rootTrie, err := loadManifest(a.fileStore, addr, quitC)
|
2017-04-12 03:06:02 +03:00
|
|
|
if err != nil {
|
2018-06-20 15:06:27 +03:00
|
|
|
return nil, nil, fmt.Errorf("can't load manifest %v: %v", addr.String(), err)
|
2017-04-12 03:06:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
manifestEntryMap = map[string]*manifestTrieEntry{}
|
|
|
|
err = rootTrie.listWithPrefix(uri.Path, quitC, func(entry *manifestTrieEntry, suffix string) {
|
|
|
|
manifestEntryMap[suffix] = entry
|
|
|
|
})
|
|
|
|
|
2017-08-08 20:34:35 +03:00
|
|
|
if err != nil {
|
2018-06-20 15:06:27 +03:00
|
|
|
return nil, nil, fmt.Errorf("list with prefix failed %v: %v", addr.String(), err)
|
|
|
|
}
|
|
|
|
return addr, manifestEntryMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceLookup Looks up mutable resource updates at specific periods and versions
|
|
|
|
func (a *API) ResourceLookup(ctx context.Context, addr storage.Address, period uint32, version uint32, maxLookup *mru.LookupParams) (string, []byte, error) {
|
|
|
|
var err error
|
|
|
|
rsrc, err := a.resource.Load(addr)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
if version != 0 {
|
|
|
|
if period == 0 {
|
|
|
|
return "", nil, mru.NewError(mru.ErrInvalidValue, "Period can't be 0")
|
|
|
|
}
|
|
|
|
_, err = a.resource.LookupVersion(ctx, rsrc.NameHash(), period, version, true, maxLookup)
|
|
|
|
} else if period != 0 {
|
|
|
|
_, err = a.resource.LookupHistorical(ctx, rsrc.NameHash(), period, true, maxLookup)
|
|
|
|
} else {
|
|
|
|
_, err = a.resource.LookupLatest(ctx, rsrc.NameHash(), true, maxLookup)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
var data []byte
|
|
|
|
_, data, err = a.resource.GetContent(rsrc.NameHash().Hex())
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
return rsrc.Name(), data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceCreate creates Resource and returns its key
|
|
|
|
func (a *API) ResourceCreate(ctx context.Context, name string, frequency uint64) (storage.Address, error) {
|
|
|
|
key, _, err := a.resource.New(ctx, name, frequency)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceUpdateMultihash updates a Mutable Resource and marks the update's content to be of multihash type, which will be recognized upon retrieval.
|
|
|
|
// It will fail if the data is not a valid multihash.
|
|
|
|
func (a *API) ResourceUpdateMultihash(ctx context.Context, name string, data []byte) (storage.Address, uint32, uint32, error) {
|
|
|
|
return a.resourceUpdate(ctx, name, data, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceUpdate updates a Mutable Resource with arbitrary data.
|
|
|
|
// Upon retrieval the update will be retrieved verbatim as bytes.
|
|
|
|
func (a *API) ResourceUpdate(ctx context.Context, name string, data []byte) (storage.Address, uint32, uint32, error) {
|
|
|
|
return a.resourceUpdate(ctx, name, data, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) resourceUpdate(ctx context.Context, name string, data []byte, multihash bool) (storage.Address, uint32, uint32, error) {
|
|
|
|
var addr storage.Address
|
|
|
|
var err error
|
|
|
|
if multihash {
|
|
|
|
addr, err = a.resource.UpdateMultihash(ctx, name, data)
|
|
|
|
} else {
|
|
|
|
addr, err = a.resource.Update(ctx, name, data)
|
2017-08-08 20:34:35 +03:00
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
period, _ := a.resource.GetLastPeriod(name)
|
|
|
|
version, _ := a.resource.GetVersion(name)
|
|
|
|
return addr, period, version, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceHashSize returned the size of the digest produced by the Mutable Resource hashing function
|
|
|
|
func (a *API) ResourceHashSize() int {
|
|
|
|
return a.resource.HashSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceIsValidated checks if the Mutable Resource has an active content validator.
|
|
|
|
func (a *API) ResourceIsValidated() bool {
|
|
|
|
return a.resource.IsValidated()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveResourceManifest retrieves the Mutable Resource manifest for the given address, and returns the address of the metadata chunk.
|
|
|
|
func (a *API) ResolveResourceManifest(addr storage.Address) (storage.Address, error) {
|
|
|
|
trie, err := loadManifest(a.fileStore, addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot load resource manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, _ := trie.getEntry("")
|
|
|
|
if entry.ContentType != ResourceContentType {
|
|
|
|
return nil, fmt.Errorf("not a resource manifest: %s", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage.Address(common.FromHex(entry.Hash)), nil
|
2017-04-12 03:06:02 +03:00
|
|
|
}
|