Modified API to allow for immutable storage.

This commit is contained in:
Daniel A. Nagy 2016-02-04 16:39:00 +01:00
parent 5189b7d6bc
commit befdf849ef
4 changed files with 20 additions and 17 deletions

View file

@ -52,11 +52,15 @@ func (self *Api) Store(data storage.SectionReader, wg *sync.WaitGroup) (key stor
}
// DNS Resolver
func (self *Api) Resolve(hostPort string) (contentHash storage.Key, err error) {
func (self *Api) Resolve(hostPort string, nameresolver bool) (contentHash storage.Key, err error) {
if hashMatcher.MatchString(hostPort) || self.dns == nil {
glog.V(logger.Debug).Infof("[BZZ] host is a contentHash: '%v'", contentHash)
return storage.Key(common.Hex2Bytes(hostPort)), nil
}
if !nameresolver {
err = fmt.Errorf("'%s' is not a content hash value.", hostPort)
return
}
contentHash, err = self.dns.Resolve(hostPort)
if err != nil {
err = ErrResolve(err)
@ -91,10 +95,10 @@ func parse(uri string) (hostPort, path string) {
return
}
func (self *Api) parseAndResolve(uri string) (contentHash storage.Key, hostPort, path string, err error) {
func (self *Api) parseAndResolve(uri string, nameresolver bool) (contentHash storage.Key, hostPort, path string, err error) {
hostPort, path = parse(uri)
//resolving host and port
contentHash, err = self.Resolve(hostPort)
contentHash, err = self.Resolve(hostPort, nameresolver)
return
}
@ -119,9 +123,9 @@ func (self *Api) Put(content, contentType string) (string, error) {
// Get uses iterative manifest retrieval and prefix matching
// to resolve path to content using dpa retrieve
// it returns a section reader, mimeType, status and an error
func (self *Api) Get(uri string) (reader storage.SectionReader, mimeType string, status int, err error) {
func (self *Api) Get(uri string, nameresolver bool) (reader storage.SectionReader, mimeType string, status int, err error) {
key, _, path, err := self.parseAndResolve(uri)
key, _, path, err := self.parseAndResolve(uri, nameresolver)
trie, err := loadManifest(self.dpa, key)
if err != nil {
@ -144,8 +148,8 @@ func (self *Api) Get(uri string) (reader storage.SectionReader, mimeType string,
return
}
func (self *Api) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) {
root := common.Hex2Bytes(rootHash)
func (self *Api) Modify(uri, contentHash, contentType string, nameresolver bool) (newRootHash string, err error) {
root, _, path, err := self.parseAndResolve(uri, nameresolver)
trie, err := loadManifest(self.dpa, root)
if err != nil {
return

View file

@ -167,7 +167,7 @@ func (self *FileSystem) Download(bzzpath, localpath string) error {
}
//resolving host and port
key, _, path, err := self.api.parseAndResolve(bzzpath)
key, _, path, err := self.api.parseAndResolve(bzzpath, true)
if err != nil {
return err
}

View file

@ -64,8 +64,7 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
// }
glog.V(logger.Debug).Infof("[BZZ] Swarm: HTTP request URL: '%s', Host: '%s', Path: '%s', Referer: '%s', Accept: '%s'", r.RequestURI, requestURL.Host, requestURL.Path, r.Referer(), r.Header.Get("Accept"))
uri := requestURL.Path
// TODO: var raw, nameresolver bool
var raw bool
var raw, nameresolver bool
var proto string
// HTTP-based URL protocol handler
@ -88,7 +87,7 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
}
}
raw = proto[1:5] == "bzzr"
// TODO: nameresolver = proto[1:5] != "bzzi"
nameresolver = proto[1:5] != "bzzi"
glog.V(logger.Debug).Infof(
"[BZZ] Swarm: %s request over protocol %s '%s' received.",
@ -125,7 +124,7 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
mime := r.Header.Get("Content-Type")
// TODO proper root hash separation
glog.V(logger.Debug).Infof("[BZZ] Modify '%s' to store %v as '%s'.", path, key.Log(), mime)
newKey, err := a.Modify(path[:64], path[65:], common.Bytes2Hex(key), mime)
newKey, err := a.Modify(path, common.Bytes2Hex(key), mime, nameresolver)
if err == nil {
glog.V(logger.Debug).Infof("[BZZ] Swarm replaced manifest by '%s'", newKey)
w.Header().Set("Content-Type", "text/plain")
@ -143,7 +142,7 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
} else {
path = api.RegularSlashes(path)
glog.V(logger.Debug).Infof("[BZZ] Delete '%s'.", path)
newKey, err := a.Modify(path[:64], path[65:], "", "")
newKey, err := a.Modify(path, "", "", nameresolver)
if err == nil {
glog.V(logger.Debug).Infof("[BZZ] Swarm replaced manifest by '%s'", newKey)
w.Header().Set("Content-Type", "text/plain")
@ -157,7 +156,7 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
path = trailingSlashes.ReplaceAllString(path, "")
if raw {
// resolving host
key, err := a.Resolve(path)
key, err := a.Resolve(path, nameresolver)
if err != nil {
glog.V(logger.Error).Infof("[BZZ] Swarm: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
@ -184,7 +183,7 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
glog.V(logger.Debug).Infof("[BZZ] Swarm: Structured GET request '%s' received.", uri)
reader, mimeType, status, err := a.Get(path)
reader, mimeType, status, err := a.Get(path, nameresolver)
if err != nil {
if _, ok := err.(api.ErrResolve); ok {
glog.V(logger.Debug).Infof("[BZZ] Swarm: %v", err)

View file

@ -37,7 +37,7 @@ func (self *Storage) Put(content, contentType string) (string, error) {
// the actual size of which is given in len(resp.Content), while the expected
// size is resp.Size
func (self *Storage) Get(bzzpath string) (*Response, error) {
reader, mimeType, status, err := self.api.Get(bzzpath)
reader, mimeType, status, err := self.api.Get(bzzpath, true)
if err != nil {
return nil, err
}
@ -52,5 +52,5 @@ func (self *Storage) Get(bzzpath string) (*Response, error) {
}
func (self *Storage) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) {
return self.api.Modify(rootHash, path, contentHash, contentType)
return self.api.Modify(rootHash+"/"+path, contentHash, contentType, true)
}