api.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "fmt"
  19. "io"
  20. "net/http"
  21. "regexp"
  22. "strings"
  23. "sync"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/logger"
  26. "github.com/ethereum/go-ethereum/logger/glog"
  27. "github.com/ethereum/go-ethereum/swarm/storage"
  28. )
  29. var (
  30. hashMatcher = regexp.MustCompile("^[0-9A-Fa-f]{64}")
  31. slashes = regexp.MustCompile("/+")
  32. domainAndVersion = regexp.MustCompile("[@:;,]+")
  33. )
  34. type Resolver interface {
  35. Resolve(string) (common.Hash, error)
  36. }
  37. /*
  38. Api implements webserver/file system related content storage and retrieval
  39. on top of the dpa
  40. it is the public interface of the dpa which is included in the ethereum stack
  41. */
  42. type Api struct {
  43. dpa *storage.DPA
  44. dns Resolver
  45. }
  46. //the api constructor initialises
  47. func NewApi(dpa *storage.DPA, dns Resolver) (self *Api) {
  48. self = &Api{
  49. dpa: dpa,
  50. dns: dns,
  51. }
  52. return
  53. }
  54. // DPA reader API
  55. func (self *Api) Retrieve(key storage.Key) storage.LazySectionReader {
  56. return self.dpa.Retrieve(key)
  57. }
  58. func (self *Api) Store(data io.Reader, size int64, wg *sync.WaitGroup) (key storage.Key, err error) {
  59. return self.dpa.Store(data, size, wg, nil)
  60. }
  61. type ErrResolve error
  62. // DNS Resolver
  63. func (self *Api) Resolve(hostPort string, nameresolver bool) (storage.Key, error) {
  64. glog.V(logger.Detail).Infof("Resolving : %v", hostPort)
  65. if hashMatcher.MatchString(hostPort) || self.dns == nil {
  66. glog.V(logger.Detail).Infof("host is a contentHash: '%v'", hostPort)
  67. return storage.Key(common.Hex2Bytes(hostPort)), nil
  68. }
  69. if !nameresolver {
  70. return nil, fmt.Errorf("'%s' is not a content hash value.", hostPort)
  71. }
  72. contentHash, err := self.dns.Resolve(hostPort)
  73. if err != nil {
  74. err = ErrResolve(err)
  75. glog.V(logger.Warn).Infof("DNS error : %v", err)
  76. }
  77. glog.V(logger.Detail).Infof("host lookup: %v -> %v", err)
  78. return contentHash[:], err
  79. }
  80. func Parse(uri string) (hostPort, path string) {
  81. if uri == "" {
  82. return
  83. }
  84. parts := slashes.Split(uri, 3)
  85. var i int
  86. if len(parts) == 0 {
  87. return
  88. }
  89. // beginning with slash is now optional
  90. for len(parts[i]) == 0 {
  91. i++
  92. }
  93. hostPort = parts[i]
  94. for i < len(parts)-1 {
  95. i++
  96. if len(path) > 0 {
  97. path = path + "/" + parts[i]
  98. } else {
  99. path = parts[i]
  100. }
  101. }
  102. glog.V(logger.Debug).Infof("host: '%s', path '%s' requested.", hostPort, path)
  103. return
  104. }
  105. func (self *Api) parseAndResolve(uri string, nameresolver bool) (key storage.Key, hostPort, path string, err error) {
  106. hostPort, path = Parse(uri)
  107. //resolving host and port
  108. contentHash, err := self.Resolve(hostPort, nameresolver)
  109. glog.V(logger.Debug).Infof("Resolved '%s' to contentHash: '%s', path: '%s'", uri, contentHash, path)
  110. return contentHash[:], hostPort, path, err
  111. }
  112. // Put provides singleton manifest creation on top of dpa store
  113. func (self *Api) Put(content, contentType string) (string, error) {
  114. r := strings.NewReader(content)
  115. wg := &sync.WaitGroup{}
  116. key, err := self.dpa.Store(r, int64(len(content)), wg, nil)
  117. if err != nil {
  118. return "", err
  119. }
  120. manifest := fmt.Sprintf(`{"entries":[{"hash":"%v","contentType":"%s"}]}`, key, contentType)
  121. r = strings.NewReader(manifest)
  122. key, err = self.dpa.Store(r, int64(len(manifest)), wg, nil)
  123. if err != nil {
  124. return "", err
  125. }
  126. wg.Wait()
  127. return key.String(), nil
  128. }
  129. // Get uses iterative manifest retrieval and prefix matching
  130. // to resolve path to content using dpa retrieve
  131. // it returns a section reader, mimeType, status and an error
  132. func (self *Api) Get(uri string, nameresolver bool) (reader storage.LazySectionReader, mimeType string, status int, err error) {
  133. key, _, path, err := self.parseAndResolve(uri, nameresolver)
  134. if err != nil {
  135. return nil, "", 500, fmt.Errorf("can't resolve: %v", err)
  136. }
  137. quitC := make(chan bool)
  138. trie, err := loadManifest(self.dpa, key, quitC)
  139. if err != nil {
  140. glog.V(logger.Warn).Infof("loadManifestTrie error: %v", err)
  141. return
  142. }
  143. glog.V(logger.Detail).Infof("getEntry(%s)", path)
  144. entry, _ := trie.getEntry(path)
  145. if entry != nil {
  146. key = common.Hex2Bytes(entry.Hash)
  147. status = entry.Status
  148. mimeType = entry.ContentType
  149. glog.V(logger.Detail).Infof("content lookup key: '%v' (%v)", key, mimeType)
  150. reader = self.dpa.Retrieve(key)
  151. } else {
  152. status = http.StatusNotFound
  153. err = fmt.Errorf("manifest entry for '%s' not found", path)
  154. glog.V(logger.Warn).Infof("%v", err)
  155. }
  156. return
  157. }
  158. func (self *Api) Modify(uri, contentHash, contentType string, nameresolver bool) (newRootHash string, err error) {
  159. root, _, path, err := self.parseAndResolve(uri, nameresolver)
  160. if err != nil {
  161. return "", fmt.Errorf("can't resolve: %v", err)
  162. }
  163. quitC := make(chan bool)
  164. trie, err := loadManifest(self.dpa, root, quitC)
  165. if err != nil {
  166. return
  167. }
  168. if contentHash != "" {
  169. entry := &manifestTrieEntry{
  170. Path: path,
  171. Hash: contentHash,
  172. ContentType: contentType,
  173. }
  174. trie.addEntry(entry, quitC)
  175. } else {
  176. trie.deleteEntry(path, quitC)
  177. }
  178. err = trie.recalcAndStore()
  179. if err != nil {
  180. return
  181. }
  182. return trie.hash.String(), nil
  183. }