From 81ff8a47de6e7ccff0c16d23a1d8f346668269f3 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Wed, 13 Dec 2017 17:53:22 +0100 Subject: [PATCH] swarm/api: revert "completely remove bzzr and bzzi schemes" Keep bzzr and bzzi schemes for backward compatibility. At least until 0.3 swarm release. --- swarm/api/api.go | 2 +- swarm/api/http/server.go | 8 ++++---- swarm/api/uri.go | 16 +++++++++++++++- swarm/api/uri_test.go | 40 ++++++++++++++++++++++++++++++++++------ 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/swarm/api/api.go b/swarm/api/api.go index 79de29a1cf..8c4bca2ec0 100644 --- a/swarm/api/api.go +++ b/swarm/api/api.go @@ -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 isHash := hashMatcher.MatchString(uri.Addr) - if uri.Immutable() { + if uri.Immutable() || uri.DeprecatedImmutable() { if !isHash { return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr) } diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go index 3a689b9bf8..3872cbc4f0 100644 --- a/swarm/api/http/server.go +++ b/swarm/api/http/server.go @@ -592,7 +592,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": - if uri.Raw() { + if uri.Raw() || uri.DeprecatedRaw() { s.HandlePostRaw(w, req) } else { 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 // strictly a traditional PUT request which replaces content // 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) return } else { @@ -612,14 +612,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } case "DELETE": - if uri.Raw() { + if uri.Raw() || uri.DeprecatedRaw() { ShowError(w, r, fmt.Sprintf("No DELETE to %s allowed.", uri), http.StatusBadRequest) return } s.HandleDelete(w, req) case "GET": - if uri.Raw() { + if uri.Raw() || uri.DeprecatedRaw() { s.HandleGetRaw(w, req) return } diff --git a/swarm/api/uri.go b/swarm/api/uri.go index ce64ef0882..af1dc74453 100644 --- a/swarm/api/uri.go +++ b/swarm/api/uri.go @@ -31,6 +31,11 @@ type URI struct { // * 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 + // * bzzi - immutable URI of an entry in a swarm manifest + // (address is not resolved) Scheme string // Addr is either a hexadecimal storage key or it an address which @@ -52,6 +57,7 @@ type URI struct { // * :/// // // 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) if err != nil { @@ -61,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": + case "bzz", "bzz-raw", "bzz-immutable", "bzz-list", "bzzr", "bzzi": default: return nil, fmt.Errorf("unknown scheme %q", u.Scheme) } @@ -96,6 +102,14 @@ func (u *URI) List() bool { 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 { return u.Scheme + ":/" + u.Addr + "/" + u.Path } diff --git a/swarm/api/uri_test.go b/swarm/api/uri_test.go index b4211a28f3..babb2834e9 100644 --- a/swarm/api/uri_test.go +++ b/swarm/api/uri_test.go @@ -23,12 +23,14 @@ import ( func TestParseURI(t *testing.T) { type test struct { - uri string - expectURI *URI - expectErr bool - expectRaw bool - expectImmutable bool - expectList bool + uri string + expectURI *URI + expectErr bool + expectRaw bool + expectImmutable bool + expectList bool + expectDeprecatedRaw bool + expectDeprecatedImmutable bool } tests := []test{ { @@ -106,6 +108,26 @@ func TestParseURI(t *testing.T) { expectURI: &URI{Scheme: "bzz-list"}, 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 { actual, err := Parse(x.uri) @@ -130,5 +152,11 @@ 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.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()) + } } }