This commit is contained in:
zsfelfoldi 2015-06-02 19:21:55 +02:00
commit 8fd754ef9d
5 changed files with 69 additions and 8 deletions

View file

@ -321,8 +321,8 @@ func (self *Api) Upload(lpath, index string) (string, error) {
wg.Wait() wg.Wait()
} }
if err == nil { if err == nil {
//cmd := exec.Command("file", "--mime-type", "-b", entry.Path) cmd := exec.Command("file", "--mime-type", "-b", entry.Path)
cmd := exec.Command("mimetype", "-b", entry.Path) // cmd := exec.Command("mimetype", "-b", entry.Path)
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
err = cmd.Run() err = cmd.Run()

View file

@ -81,7 +81,6 @@ func TestApiDirUpload(t *testing.T) {
content, err := ioutil.ReadFile(path.Join(testDir, "test0", "index.html")) 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, 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")) content, err = ioutil.ReadFile(path.Join(testDir, "test0", "index.css"))
testGet(t, api, path.Join(bzzhash, "index.css"), content, "text/plain", 0, 132) 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) { func TestApiFileUpload(t *testing.T) {
api, err := testApi() api, err := testApi()
if err != nil { if err != nil {
@ -108,5 +123,5 @@ func TestApiFileUpload(t *testing.T) {
} }
content, err := ioutil.ReadFile(path.Join(testDir, "test0", "index.html")) 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)
} }

View file

@ -13,12 +13,14 @@ import (
type DocServer struct { type DocServer struct {
*http.Transport *http.Transport
DocRoot string DocRoot string
schemes []string
} }
func New(docRoot string) (self *DocServer) { func New(docRoot string) (self *DocServer) {
self = &DocServer{ self = &DocServer{
Transport: &http.Transport{}, Transport: &http.Transport{},
DocRoot: docRoot, DocRoot: docRoot,
schemes: []string{"file"},
} }
self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot))) self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot)))
return 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) { func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) {
// retrieve content // 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) { func (self *DocServer) Get(uri, path string) (content []byte, err error) {
// retrieve content // retrieve content
resp, err := self.Client().Get(uri) resp, err := self.Client().Get(uri)

View file

@ -2,6 +2,7 @@ package docserver
import ( import (
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"testing" "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")
}
}

View file

@ -88,7 +88,7 @@ func New(xeth *xeth.XEth, jsontx string, http *docserver.DocServer) (self *NatSp
} }
// also called by admin.contractInfo.get // 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 // retrieve contract hash from state
codehex := xeth.CodeAt(contractAddress) codehex := xeth.CodeAt(contractAddress)
codeb := xeth.CodeAtBytes(contractAddress) codeb := xeth.CodeAtBytes(contractAddress)
@ -102,17 +102,29 @@ func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, http *docserv
res := resolver.New(xeth) res := resolver.New(xeth)
// resolve host via HashReg/UrlHint Resolver // 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 { if err != nil {
return return
} }
// get content via http client and authenticate content using hash // 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 { if err != nil {
return return
} }
return return
} }