swarm/api: url scheme bzz-hash to get hashes of swarm content (#15238)

Update URI to support bzz-hash scheme and handle such HTTP requests by
responding with hash of the content as a text/plain response.
This commit is contained in:
Janos Guljas 2017-12-13 12:32:13 +01:00
parent 3654aeaa4f
commit 700ac3b03e
4 changed files with 77 additions and 16 deletions

View file

@ -290,9 +290,12 @@ func (s *Server) HandleDelete(w http.ResponseWriter, r *Request) {
fmt.Fprint(w, newKey)
}
// HandleGetRaw handles a GET request to bzzr://<key> and responds with
// the raw content stored at the given storage key
func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) {
// HandleGet handles a GET request to
// - bzzr://<key> and responds with the raw content stored at the
// given storage key
// - bzz-hash://<key> and responds with the hash of the content stored
// at the given storage key as a text/plain response
func (s *Server) HandleGet(w http.ResponseWriter, r *Request) {
key, err := s.api.Resolve(r.uri)
if err != nil {
s.Error(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err))
@ -345,6 +348,8 @@ func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) {
return
}
switch {
case r.uri.Raw():
// allow the request to overwrite the content type using a query
// parameter
contentType := "application/octet-stream"
@ -354,6 +359,11 @@ func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) {
w.Header().Set("Content-Type", contentType)
http.ServeContent(w, &r.Request, "", time.Now(), reader)
case r.uri.Hash():
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, key)
}
}
// HandleGetFiles handles a GET request to bzz:/<manifest> with an Accept
@ -616,8 +626,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.HandleDelete(w, req)
case "GET":
if uri.Raw() {
s.HandleGetRaw(w, req)
if uri.Raw() || uri.Hash() {
s.HandleGet(w, req)
return
}

View file

@ -33,7 +33,7 @@ import (
"github.com/ethereum/go-ethereum/swarm/testutil"
)
func TestBzzrGetPath(t *testing.T) {
func TestBzzGetPath(t *testing.T) {
var err error
@ -104,16 +104,47 @@ func TestBzzrGetPath(t *testing.T) {
}
}
for k, v := range testrequests {
var resp *http.Response
var respbody []byte
url := srv.URL + "/bzz-hash:/"
if k[:] != "" {
url += common.ToHex(key[0])[2:] + "/" + k[1:]
}
resp, err = http.Get(url)
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer resp.Body.Close()
respbody, err = ioutil.ReadAll(resp.Body)
if string(respbody) != key[v].String() {
isexpectedfailrequest := false
for _, r := range expectedfailrequests {
if k[:] == r {
isexpectedfailrequest = true
}
}
if !isexpectedfailrequest {
t.Fatalf("Response body does not match, expected: %v, got %v", key[v], string(respbody))
}
}
}
nonhashtests := []string{
srv.URL + "/bzz:/name",
srv.URL + "/bzzi:/nonhash",
srv.URL + "/bzzr:/nonhash",
srv.URL + "/bzz-hash:/nonhash",
}
nonhashresponses := []string{
"error resolving name: no DNS to resolve name: &#34;name&#34;",
"error resolving nonhash: immutable address not a content hash: &#34;nonhash&#34;",
"error resolving nonhash: no DNS to resolve name: &#34;nonhash&#34;",
"error resolving nonhash: no DNS to resolve name: &#34;nonhash&#34;",
}
for i, url := range nonhashtests {

View file

@ -30,6 +30,8 @@ type URI struct {
// * bzzr - raw swarm content
// * bzzi - immutable URI of an entry in a swarm manifest
// (address is not resolved)
// * bzz-hash - hash of swarm content
//
Scheme string
// Addr is either a hexadecimal storage key or it an address which
@ -50,7 +52,7 @@ type URI struct {
// * <scheme>://<addr>
// * <scheme>://<addr>/<path>
//
// with scheme one of bzz, bzzr or bzzi
// with scheme one of bzz, bzzr, bzzi or bzz-hash
func Parse(rawuri string) (*URI, error) {
u, err := url.Parse(rawuri)
if err != nil {
@ -60,7 +62,7 @@ func Parse(rawuri string) (*URI, error) {
// check the scheme is valid
switch uri.Scheme {
case "bzz", "bzzi", "bzzr":
case "bzz", "bzzi", "bzzr", "bzz-hash":
default:
return nil, fmt.Errorf("unknown scheme %q", u.Scheme)
}
@ -91,6 +93,10 @@ func (u *URI) Immutable() bool {
return u.Scheme == "bzzi"
}
func (u *URI) Hash() bool {
return u.Scheme == "bzz-hash"
}
func (u *URI) String() string {
return u.Scheme + ":/" + u.Addr + "/" + u.Path
}

View file

@ -28,6 +28,7 @@ func TestParseURI(t *testing.T) {
expectErr bool
expectRaw bool
expectImmutable bool
expectHash bool
}
tests := []test{
{
@ -95,6 +96,16 @@ func TestParseURI(t *testing.T) {
uri: "bzz://abc123/path/to/entry",
expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
},
{
uri: "bzz-hash:",
expectURI: &URI{Scheme: "bzz-hash"},
expectHash: true,
},
{
uri: "bzz-hash:/",
expectURI: &URI{Scheme: "bzz-hash"},
expectHash: true,
},
}
for _, x := range tests {
actual, err := Parse(x.uri)
@ -116,5 +127,8 @@ func TestParseURI(t *testing.T) {
if actual.Immutable() != x.expectImmutable {
t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable())
}
if actual.Hash() != x.expectHash {
t.Fatalf("expected %s hash to be %t, got %t", x.uri, x.expectHash, actual.Hash())
}
}
}