mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 20:56:42 +00:00
Merge branch 'bzz' of https://github.com/ethersphere/go-ethereum into bzz
This commit is contained in:
commit
8fd754ef9d
5 changed files with 69 additions and 8 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue