Skip to content

Commit

Permalink
swarm: bzz-list, bzz-raw and bzz-immutable schemes (ethereum#15667)
Browse files Browse the repository at this point in the history
* swarm/api: url scheme bzz-list for getting list of files from manifest

Replace query parameter list=true for listing all files contained
in a swarm manifest with a new URL scheme bzz-list.

* swarm: replaace bzzr and bzzi schemes with bzz-raw and bzz-immutable

New URI Shemes are added and old ones are deprecated, but not removed.
Old Schemes bzzr and bzzi are functional for backward compatibility.

* swarm/api: completely remove bzzr and bzzi schemes

Remove old schemes in favour of bzz-raw and
bzz-immutable.

* swarm/api: revert "completely remove bzzr and bzzi schemes"

Keep bzzr and bzzi schemes for backward compatibility. At least
until 0.3 swarm release.
  • Loading branch information
janos authored and mariameda committed Aug 19, 2018
1 parent b0084d6 commit 3932109
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 99 deletions.
54 changes: 22 additions & 32 deletions swarm/api/http/server.go
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2016 The go-nilu Authors
// This file is part of the go-nilu library.
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-nilu library is free software: you can redistribute it and/or modify
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-nilu library is distributed in the hope that it will be useful,
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-nilu library. If not, see <http://www.gnu.org/licenses/>.
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

/*
A simple http server interface to Swarm
Expand All @@ -35,10 +35,10 @@ import (
"strings"
"time"

"github.com/NiluPlatform/go-nilu/common"
"github.com/NiluPlatform/go-nilu/log"
"github.com/NiluPlatform/go-nilu/swarm/api"
"github.com/NiluPlatform/go-nilu/swarm/storage"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/swarm/api"
"github.com/ethereum/go-ethereum/swarm/storage"
"github.com/rs/cors"
)

Expand Down Expand Up @@ -290,12 +290,9 @@ func (s *Server) HandleDelete(w http.ResponseWriter, r *Request) {
fmt.Fprint(w, newKey)
}

// HandleGet handles a GET request to
// - bzz-raw://<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) {
// HandleGetRaw handles a GET request to bzz-raw://<key> and responds with
// the raw content stored at the given storage key
func (s *Server) HandleGetRaw(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))
Expand Down Expand Up @@ -348,22 +345,15 @@ func (s *Server) HandleGet(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"
if typ := r.URL.Query().Get("content_type"); typ != "" {
contentType = typ
}
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)
// allow the request to overwrite the content type using a query
// parameter
contentType := "application/octet-stream"
if typ := r.URL.Query().Get("content_type"); typ != "" {
contentType = typ
}
w.Header().Set("Content-Type", contentType)

http.ServeContent(w, &r.Request, "", time.Now(), reader)
}

// HandleGetFiles handles a GET request to bzz:/<manifest> with an Accept
Expand Down Expand Up @@ -629,8 +619,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.HandleDelete(w, req)

case "GET":
if uri.Raw() || uri.Hash() || uri.DeprecatedRaw() {
s.HandleGet(w, req)
if uri.Raw() || uri.DeprecatedRaw() {
s.HandleGetRaw(w, req)
return
}

Expand Down
56 changes: 11 additions & 45 deletions swarm/api/http/server_test.go
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2017 The go-nilu Authors
// This file is part of the go-nilu library.
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-nilu library is free software: you can redistribute it and/or modify
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-nilu library is distributed in the hope that it will be useful,
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-nilu library. If not, see <http://www.gnu.org/licenses/>.
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package http_test

Expand All @@ -26,14 +26,14 @@ import (
"sync"
"testing"

"github.com/NiluPlatform/go-nilu/common"
"github.com/NiluPlatform/go-nilu/swarm/api"
swarm "github.com/NiluPlatform/go-nilu/swarm/api/client"
"github.com/NiluPlatform/go-nilu/swarm/storage"
"github.com/NiluPlatform/go-nilu/swarm/testutil"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/swarm/api"
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
"github.com/ethereum/go-ethereum/swarm/storage"
"github.com/ethereum/go-ethereum/swarm/testutil"
)

func TestBzzGetPath(t *testing.T) {
func TestBzzrGetPath(t *testing.T) {

var err error

Expand Down Expand Up @@ -104,38 +104,6 @@ func TestBzzGetPath(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 err != nil {
t.Fatalf("Read request body: %v", err)
}

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))
}
}
}

for _, c := range []struct {
path string
json string
Expand Down Expand Up @@ -229,15 +197,13 @@ func TestBzzGetPath(t *testing.T) {
srv.URL + "/bzz-immutable:/nonhash",
srv.URL + "/bzz-raw:/nonhash",
srv.URL + "/bzz-list:/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;",
"error resolving nonhash: no DNS to resolve name: &#34;nonhash&#34;",
}

for i, url := range nonhashtests {
Expand Down
10 changes: 2 additions & 8 deletions swarm/api/uri.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ 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
Expand All @@ -58,7 +56,7 @@ type URI struct {
// * <scheme>://<addr>
// * <scheme>://<addr>/<path>
//
// with scheme one of bzz, bzz-raw, bzz-immutable, bzz-list or bzz-hash
// with scheme one of bzz, bzz-raw, bzz-immutable or bzz-list
// or deprecated ones bzzr and bzzi
func Parse(rawuri string) (*URI, error) {
u, err := url.Parse(rawuri)
Expand All @@ -69,7 +67,7 @@ func Parse(rawuri string) (*URI, error) {

// check the scheme is valid
switch uri.Scheme {
case "bzz", "bzz-raw", "bzz-immutable", "bzz-list", "bzz-hash", "bzzr", "bzzi":
case "bzz", "bzz-raw", "bzz-immutable", "bzz-list", "bzzr", "bzzi":
default:
return nil, fmt.Errorf("unknown scheme %q", u.Scheme)
}
Expand Down Expand Up @@ -112,10 +110,6 @@ func (u *URI) DeprecatedImmutable() 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
}
14 changes: 0 additions & 14 deletions swarm/api/uri_test.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestParseURI(t *testing.T) {
expectRaw bool
expectImmutable bool
expectList bool
expectHash bool
expectDeprecatedRaw bool
expectDeprecatedImmutable bool
}
Expand Down Expand Up @@ -99,16 +98,6 @@ 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,
},
{
uri: "bzz-list:",
expectURI: &URI{Scheme: "bzz-list"},
Expand Down Expand Up @@ -163,9 +152,6 @@ func TestParseURI(t *testing.T) {
if actual.List() != x.expectList {
t.Fatalf("expected %s list to be %t, got %t", x.uri, x.expectList, actual.List())
}
if actual.Hash() != x.expectHash {
t.Fatalf("expected %s hash to be %t, got %t", x.uri, x.expectHash, actual.Hash())
}
if actual.DeprecatedRaw() != x.expectDeprecatedRaw {
t.Fatalf("expected %s deprecated raw to be %t, got %t", x.uri, x.expectDeprecatedRaw, actual.DeprecatedRaw())
}
Expand Down

0 comments on commit 3932109

Please sign in to comment.