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.
This commit is contained in:
Janos Guljas 2017-12-13 13:28:39 +01:00
parent cfd8420177
commit 52693520cc
9 changed files with 81 additions and 39 deletions

View file

@ -83,7 +83,7 @@ func (self *Api) Resolve(uri *URI) (storage.Key, error) {
// if the URI is immutable, check if the address is a hash // if the URI is immutable, check if the address is a hash
isHash := hashMatcher.MatchString(uri.Addr) isHash := hashMatcher.MatchString(uri.Addr)
if uri.Immutable() { if uri.Immutable() || uri.DeprecatedImmutable() {
if !isHash { if !isHash {
return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr) return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr)
} }

View file

@ -216,7 +216,7 @@ func TestAPIResolve(t *testing.T) {
api := &Api{dns: x.dns} api := &Api{dns: x.dns}
uri := &URI{Addr: x.addr, Scheme: "bzz"} uri := &URI{Addr: x.addr, Scheme: "bzz"}
if x.immutable { if x.immutable {
uri.Scheme = "bzzi" uri.Scheme = "bzz-immutable"
} }
res, err := api.Resolve(uri) res, err := api.Resolve(uri)
if err == nil { if err == nil {

View file

@ -57,7 +57,7 @@ func (c *Client) UploadRaw(r io.Reader, size int64) (string, error) {
if size <= 0 { if size <= 0 {
return "", errors.New("data size must be greater than zero") return "", errors.New("data size must be greater than zero")
} }
req, err := http.NewRequest("POST", c.Gateway+"/bzzr:/", r) req, err := http.NewRequest("POST", c.Gateway+"/bzz-raw:/", r)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -79,7 +79,7 @@ func (c *Client) UploadRaw(r io.Reader, size int64) (string, error) {
// DownloadRaw downloads raw data from swarm // DownloadRaw downloads raw data from swarm
func (c *Client) DownloadRaw(hash string) (io.ReadCloser, error) { func (c *Client) DownloadRaw(hash string) (io.ReadCloser, error) {
uri := c.Gateway + "/bzzr:/" + hash uri := c.Gateway + "/bzz-raw:/" + hash
res, err := http.DefaultClient.Get(uri) res, err := http.DefaultClient.Get(uri)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -35,8 +35,8 @@ import (
client := httpclient.New() client := httpclient.New()
// for (private) swarm proxy running locally // for (private) swarm proxy running locally
client.RegisterScheme("bzz", &http.RoundTripper{Port: port}) client.RegisterScheme("bzz", &http.RoundTripper{Port: port})
client.RegisterScheme("bzzi", &http.RoundTripper{Port: port}) client.RegisterScheme("bzz-immutable", &http.RoundTripper{Port: port})
client.RegisterScheme("bzzr", &http.RoundTripper{Port: port}) client.RegisterScheme("bzz-raw", &http.RoundTripper{Port: port})
The port you give the Roundtripper is the port the swarm proxy is listening on. The port you give the Roundtripper is the port the swarm proxy is listening on.
If Host is left empty, localhost is assumed. If Host is left empty, localhost is assumed.

View file

@ -86,7 +86,7 @@ type Request struct {
uri *api.URI uri *api.URI
} }
// HandlePostRaw handles a POST request to a raw bzzr:/ URI, stores the request // HandlePostRaw handles a POST request to a raw bzz-raw:/ URI, stores the request
// body in swarm and returns the resulting storage key as a text/plain response // body in swarm and returns the resulting storage key as a text/plain response
func (s *Server) HandlePostRaw(w http.ResponseWriter, r *Request) { func (s *Server) HandlePostRaw(w http.ResponseWriter, r *Request) {
if r.uri.Path != "" { if r.uri.Path != "" {
@ -290,7 +290,7 @@ func (s *Server) HandleDelete(w http.ResponseWriter, r *Request) {
fmt.Fprint(w, newKey) fmt.Fprint(w, newKey)
} }
// HandleGetRaw handles a GET request to bzzr://<key> and responds with // HandleGetRaw handles a GET request to bzz-raw://<key> and responds with
// the raw content stored at the given storage key // the raw content stored at the given storage key
func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) { func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) {
key, err := s.api.Resolve(r.uri) key, err := s.api.Resolve(r.uri)
@ -592,7 +592,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case "POST": case "POST":
if uri.Raw() { if uri.Raw() || uri.DeprecatedRaw() {
s.HandlePostRaw(w, req) s.HandlePostRaw(w, req)
} else { } else {
s.HandlePostFiles(w, req) s.HandlePostFiles(w, req)
@ -604,7 +604,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// new manifest leaving the existing one intact, so it isn't // new manifest leaving the existing one intact, so it isn't
// strictly a traditional PUT request which replaces content // strictly a traditional PUT request which replaces content
// at a URI, and POST is more ubiquitous) // at a URI, and POST is more ubiquitous)
if uri.Raw() { if uri.Raw() || uri.DeprecatedRaw() {
ShowError(w, r, fmt.Sprintf("No PUT to %s allowed.", uri), http.StatusBadRequest) ShowError(w, r, fmt.Sprintf("No PUT to %s allowed.", uri), http.StatusBadRequest)
return return
} else { } else {
@ -612,14 +612,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
case "DELETE": case "DELETE":
if uri.Raw() { if uri.Raw() || uri.DeprecatedRaw() {
ShowError(w, r, fmt.Sprintf("No DELETE to %s allowed.", uri), http.StatusBadRequest) ShowError(w, r, fmt.Sprintf("No DELETE to %s allowed.", uri), http.StatusBadRequest)
return return
} }
s.HandleDelete(w, req) s.HandleDelete(w, req)
case "GET": case "GET":
if uri.Raw() { if uri.Raw() || uri.DeprecatedRaw() {
s.HandleGetRaw(w, req) s.HandleGetRaw(w, req)
return return
} }

View file

@ -70,7 +70,7 @@ func TestBzzrGetPath(t *testing.T) {
wg.Wait() wg.Wait()
} }
_, err = http.Get(srv.URL + "/bzzr:/" + common.ToHex(key[0])[2:] + "/a") _, err = http.Get(srv.URL + "/bzz-raw:/" + common.ToHex(key[0])[2:] + "/a")
if err != nil { if err != nil {
t.Fatalf("Failed to connect to proxy: %v", err) t.Fatalf("Failed to connect to proxy: %v", err)
} }
@ -79,7 +79,7 @@ func TestBzzrGetPath(t *testing.T) {
var resp *http.Response var resp *http.Response
var respbody []byte var respbody []byte
url := srv.URL + "/bzzr:/" url := srv.URL + "/bzz-raw:/"
if k[:] != "" { if k[:] != "" {
url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain" url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
} }
@ -194,8 +194,8 @@ func TestBzzrGetPath(t *testing.T) {
nonhashtests := []string{ nonhashtests := []string{
srv.URL + "/bzz:/name", srv.URL + "/bzz:/name",
srv.URL + "/bzzi:/nonhash", srv.URL + "/bzz-immutable:/nonhash",
srv.URL + "/bzzr:/nonhash", srv.URL + "/bzz-raw:/nonhash",
srv.URL + "/bzz-list:/nonhash", srv.URL + "/bzz-list:/nonhash",
} }

View file

@ -27,10 +27,15 @@ type URI struct {
// Scheme has one of the following values: // Scheme has one of the following values:
// //
// * bzz - an entry in a swarm manifest // * bzz - an entry in a swarm manifest
// * bzz-raw - raw swarm content
// * bzz-immutable - immutable URI of an entry in a swarm manifest
// (address is not resolved)
// * bzz-list - list of all files contained in a swarm manifest
//
// Deprecated Schemes:
// * bzzr - raw swarm content // * bzzr - raw swarm content
// * bzzi - immutable URI of an entry in a swarm manifest // * bzzi - immutable URI of an entry in a swarm manifest
// (address is not resolved) // (address is not resolved)
// * bzz-list - list of all files contained in a swarm manifest
Scheme string Scheme string
// Addr is either a hexadecimal storage key or it an address which // Addr is either a hexadecimal storage key or it an address which
@ -51,7 +56,8 @@ type URI struct {
// * <scheme>://<addr> // * <scheme>://<addr>
// * <scheme>://<addr>/<path> // * <scheme>://<addr>/<path>
// //
// with scheme one of bzz, bzzr, bzzi or bzz-list // with scheme one of bzz, bzz-raw, bzz-immutable or bzz-list
// or deprecated ones bzzr and bzzi
func Parse(rawuri string) (*URI, error) { func Parse(rawuri string) (*URI, error) {
u, err := url.Parse(rawuri) u, err := url.Parse(rawuri)
if err != nil { if err != nil {
@ -61,7 +67,7 @@ func Parse(rawuri string) (*URI, error) {
// check the scheme is valid // check the scheme is valid
switch uri.Scheme { switch uri.Scheme {
case "bzz", "bzzi", "bzzr", "bzz-list": case "bzz", "bzz-raw", "bzz-immutable", "bzz-list", "bzzr", "bzzi":
default: default:
return nil, fmt.Errorf("unknown scheme %q", u.Scheme) return nil, fmt.Errorf("unknown scheme %q", u.Scheme)
} }
@ -85,17 +91,25 @@ func Parse(rawuri string) (*URI, error) {
} }
func (u *URI) Raw() bool { func (u *URI) Raw() bool {
return u.Scheme == "bzzr" return u.Scheme == "bzz-raw"
} }
func (u *URI) Immutable() bool { func (u *URI) Immutable() bool {
return u.Scheme == "bzzi" return u.Scheme == "bzz-immutable"
} }
func (u *URI) List() bool { func (u *URI) List() bool {
return u.Scheme == "bzz-list" return u.Scheme == "bzz-list"
} }
func (u *URI) DeprecatedRaw() bool {
return u.Scheme == "bzzr"
}
func (u *URI) DeprecatedImmutable() bool {
return u.Scheme == "bzzi"
}
func (u *URI) String() string { func (u *URI) String() string {
return u.Scheme + ":/" + u.Addr + "/" + u.Path return u.Scheme + ":/" + u.Addr + "/" + u.Path
} }

View file

@ -29,6 +29,8 @@ func TestParseURI(t *testing.T) {
expectRaw bool expectRaw bool
expectImmutable bool expectImmutable bool
expectList bool expectList bool
expectDeprecatedRaw bool
expectDeprecatedImmutable bool
} }
tests := []test{ tests := []test{
{ {
@ -48,13 +50,13 @@ func TestParseURI(t *testing.T) {
expectURI: &URI{Scheme: "bzz"}, expectURI: &URI{Scheme: "bzz"},
}, },
{ {
uri: "bzzi:", uri: "bzz-immutable:",
expectURI: &URI{Scheme: "bzzi"}, expectURI: &URI{Scheme: "bzz-immutable"},
expectImmutable: true, expectImmutable: true,
}, },
{ {
uri: "bzzr:", uri: "bzz-raw:",
expectURI: &URI{Scheme: "bzzr"}, expectURI: &URI{Scheme: "bzz-raw"},
expectRaw: true, expectRaw: true,
}, },
{ {
@ -70,18 +72,18 @@ func TestParseURI(t *testing.T) {
expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"}, expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
}, },
{ {
uri: "bzzr:/", uri: "bzz-raw:/",
expectURI: &URI{Scheme: "bzzr"}, expectURI: &URI{Scheme: "bzz-raw"},
expectRaw: true, expectRaw: true,
}, },
{ {
uri: "bzzr:/abc123", uri: "bzz-raw:/abc123",
expectURI: &URI{Scheme: "bzzr", Addr: "abc123"}, expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123"},
expectRaw: true, expectRaw: true,
}, },
{ {
uri: "bzzr:/abc123/path/to/entry", uri: "bzz-raw:/abc123/path/to/entry",
expectURI: &URI{Scheme: "bzzr", Addr: "abc123", Path: "path/to/entry"}, expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123", Path: "path/to/entry"},
expectRaw: true, expectRaw: true,
}, },
{ {
@ -106,6 +108,26 @@ func TestParseURI(t *testing.T) {
expectURI: &URI{Scheme: "bzz-list"}, expectURI: &URI{Scheme: "bzz-list"},
expectList: true, expectList: true,
}, },
{
uri: "bzzr:",
expectURI: &URI{Scheme: "bzzr"},
expectDeprecatedRaw: true,
},
{
uri: "bzzr:/",
expectURI: &URI{Scheme: "bzzr"},
expectDeprecatedRaw: true,
},
{
uri: "bzzi:",
expectURI: &URI{Scheme: "bzzi"},
expectDeprecatedImmutable: true,
},
{
uri: "bzzi:/",
expectURI: &URI{Scheme: "bzzi"},
expectDeprecatedImmutable: true,
},
} }
for _, x := range tests { for _, x := range tests {
actual, err := Parse(x.uri) actual, err := Parse(x.uri)
@ -130,5 +152,11 @@ func TestParseURI(t *testing.T) {
if actual.List() != x.expectList { if actual.List() != x.expectList {
t.Fatalf("expected %s list to be %t, got %t", x.uri, x.expectList, actual.List()) t.Fatalf("expected %s list to be %t, got %t", x.uri, x.expectList, actual.List())
} }
if actual.DeprecatedRaw() != x.expectDeprecatedRaw {
t.Fatalf("expected %s deprecated raw to be %t, got %t", x.uri, x.expectDeprecatedRaw, actual.DeprecatedRaw())
}
if actual.DeprecatedImmutable() != x.expectDeprecatedImmutable {
t.Fatalf("expected %s deprecated immutable to be %t, got %t", x.uri, x.expectDeprecatedImmutable, actual.DeprecatedImmutable())
}
} }
} }

View file

@ -46,7 +46,7 @@ main() {
} }
do_random_upload() { do_random_upload() {
curl -fsSL -X POST --data-binary "$(random_data)" "http://${addr}/bzzr:/" curl -fsSL -X POST --data-binary "$(random_data)" "http://${addr}/bzz-raw:/"
} }
random_data() { random_data() {