diff --git a/bzz/api.go b/bzz/api.go index 019104776f..7065f76775 100644 --- a/bzz/api.go +++ b/bzz/api.go @@ -321,8 +321,8 @@ func (self *Api) Upload(lpath, index string) (string, error) { wg.Wait() } if err == nil { - //cmd := exec.Command("file", "--mime-type", "-b", entry.Path) - cmd := exec.Command("mimetype", "-b", entry.Path) + cmd := exec.Command("file", "--mime-type", "-b", entry.Path) + // cmd := exec.Command("mimetype", "-b", entry.Path) var out bytes.Buffer cmd.Stdout = &out err = cmd.Run() diff --git a/bzz/api_test.go b/bzz/api_test.go index 93f9b54224..7eea9abb51 100644 --- a/bzz/api_test.go +++ b/bzz/api_test.go @@ -81,7 +81,6 @@ func TestApiDirUpload(t *testing.T) { content, err := ioutil.ReadFile(path.Join(testDir, "test0", "index.html")) testGet(t, api, path.Join(bzzhash, "index.html"), content, "text/html", 0, 202) - testGet(t, api, bzzhash, content, "text/html", 0, 202) content, err = ioutil.ReadFile(path.Join(testDir, "test0", "index.css")) testGet(t, api, path.Join(bzzhash, "index.css"), content, "text/plain", 0, 132) @@ -95,6 +94,22 @@ func TestApiDirUpload(t *testing.T) { } } +func TestApiDirUploadWithRootFile(t *testing.T) { + api, err := testApi() + if err != nil { + t.Errorf("unexpected error: %v", err) + return + } + bzzhash, err := api.Upload(path.Join(testDir, "test0"), "index.html") + if err != nil { + t.Errorf("unexpected error: %v", err) + return + } + + content, err := ioutil.ReadFile(path.Join(testDir, "test0", "index.html")) + testGet(t, api, bzzhash, content, "text/html", 0, 202) +} + func TestApiFileUpload(t *testing.T) { api, err := testApi() if err != nil { @@ -108,5 +123,5 @@ func TestApiFileUpload(t *testing.T) { } content, err := ioutil.ReadFile(path.Join(testDir, "test0", "index.html")) - testGet(t, api, path.Join(bzzhash), content, "text/html", 0, 202) + testGet(t, api, bzzhash, content, "text/html", 0, 202) } diff --git a/common/docserver/docserver.go b/common/docserver/docserver.go index 21fc980e0a..e900b45f5a 100644 --- a/common/docserver/docserver.go +++ b/common/docserver/docserver.go @@ -13,12 +13,14 @@ import ( type DocServer struct { *http.Transport DocRoot string + schemes []string } func New(docRoot string) (self *DocServer) { self = &DocServer{ Transport: &http.Transport{}, DocRoot: docRoot, + schemes: []string{"file"}, } self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot))) return @@ -34,6 +36,20 @@ func (self *DocServer) Client() *http.Client { } } +func (self *DocServer) RegisterScheme(scheme string, rt http.RoundTripper) { + self.schemes = append(self.schemes, scheme) + self.RegisterProtocol(scheme, rt) +} + +func (self *DocServer) HasScheme(scheme string) bool { + for _, s := range self.schemes { + if s == scheme { + return true + } + } + return false +} + func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) { // retrieve content @@ -55,6 +71,8 @@ func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []b } +// Get(uri, path) downloads the document at uri, if path is non-empty it +// is interpreted as a filepath to which the contents are saved func (self *DocServer) Get(uri, path string) (content []byte, err error) { // retrieve content resp, err := self.Client().Get(uri) diff --git a/common/docserver/docserver_test.go b/common/docserver/docserver_test.go index e7656bb2d3..09b16864a7 100644 --- a/common/docserver/docserver_test.go +++ b/common/docserver/docserver_test.go @@ -2,6 +2,7 @@ package docserver import ( "io/ioutil" + "net/http" "os" "testing" @@ -36,3 +37,18 @@ func TestGetAuthContent(t *testing.T) { } } + +type rt struct{} + +func (rt) RoundTrip(req *http.Request) (resp *http.Response, err error) { return } + +func TestRegisterScheme(t *testing.T) { + ds := New("/tmp/") + if ds.HasScheme("scheme") { + t.Errorf("expected scheme not to be registered") + } + ds.RegisterScheme("scheme", rt{}) + if !ds.HasScheme("scheme") { + t.Errorf("expected scheme to be registered") + } +} diff --git a/common/natspec/natspec.go b/common/natspec/natspec.go index 7e5f053c70..dcd1fba477 100644 --- a/common/natspec/natspec.go +++ b/common/natspec/natspec.go @@ -88,7 +88,7 @@ func New(xeth *xeth.XEth, jsontx string, http *docserver.DocServer) (self *NatSp } // also called by admin.contractInfo.get -func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, http *docserver.DocServer) (content []byte, err error) { +func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, ds *docserver.DocServer) (content []byte, err error) { // retrieve contract hash from state codehex := xeth.CodeAt(contractAddress) codeb := xeth.CodeAtBytes(contractAddress) @@ -102,17 +102,29 @@ func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, http *docserv res := resolver.New(xeth) // resolve host via HashReg/UrlHint Resolver - uri, hash, err := res.KeyToUrl(codehash) + hash, err := res.KeyToContentHash(codehash) + if err != nil { + return + } + if ds.HasScheme("bzz") { + content, err = ds.Get("bzz://"+hash.Hex(), "") + if err == nil { // non-fatal + return + } + err = nil + //falling back to urlhint + } + + uri, err := res.ContentHashToUrl(hash) if err != nil { return } // get content via http client and authenticate content using hash - content, err = http.GetAuthContent(uri, hash) + content, err = ds.GetAuthContent(uri, hash) if err != nil { return } - return }