From e8ce29d58d9092e37959526a047fefdcfa812d74 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 2 Jun 2015 14:48:56 +0100 Subject: [PATCH 1/4] change mimetype back to file: no mimetype command on MacOs --- bzz/api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bzz/api.go b/bzz/api.go index a8ef586d3f..f2a070d25a 100644 --- a/bzz/api.go +++ b/bzz/api.go @@ -266,8 +266,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() From 5292414f1147ccd5bd06df4b75a6c82b4ecf533b Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 2 Jun 2015 16:32:46 +0100 Subject: [PATCH 2/4] add tests for upload with fallback for root path --- bzz/api_test.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) 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) } From 618c5d70536e8d8751c6cdadd69cf474e6607886 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 2 Jun 2015 17:48:07 +0100 Subject: [PATCH 3/4] add HasScheme method to check for register scheme handler protocols + test --- common/docserver/docserver.go | 16 ++++++++++++++++ common/docserver/docserver_test.go | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/common/docserver/docserver.go b/common/docserver/docserver.go index 21fc980e0a..f719c71566 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 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") + } +} From a9aed92a4bfbd1a84f1e236d1736a179e99ed544 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 2 Jun 2015 17:56:38 +0100 Subject: [PATCH 4/4] make natspec/contractinfo fetching to try looking up content via bzz if docserver has the scheme registered, if fails, falls back to urlhint --- common/docserver/docserver.go | 2 ++ common/natspec/natspec.go | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/common/docserver/docserver.go b/common/docserver/docserver.go index f719c71566..e900b45f5a 100644 --- a/common/docserver/docserver.go +++ b/common/docserver/docserver.go @@ -71,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/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 }