From 3932109fb433294aa1b086d29fbea2e64a139f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jano=C5=A1=20Gulja=C5=A1?= Date: Tue, 19 Dec 2017 09:49:30 +0100 Subject: [PATCH] swarm: bzz-list, bzz-raw and bzz-immutable schemes (#15667) * 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. --- swarm/api/http/server.go | 54 ++++++++++++++------------------- swarm/api/http/server_test.go | 56 +++++++---------------------------- swarm/api/uri.go | 10 ++----- swarm/api/uri_test.go | 14 --------- 4 files changed, 35 insertions(+), 99 deletions(-) mode change 100755 => 100644 swarm/api/http/server.go mode change 100755 => 100644 swarm/api/http/server_test.go mode change 100755 => 100644 swarm/api/uri.go mode change 100755 => 100644 swarm/api/uri_test.go diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go old mode 100755 new mode 100644 index 2ef65f9586f9..3872cbc4f018 --- a/swarm/api/http/server.go +++ b/swarm/api/http/server.go @@ -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 . +// along with the go-ethereum library. If not, see . /* A simple http server interface to Swarm @@ -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" ) @@ -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:// and responds with the raw content stored at the -// given storage key -// - bzz-hash:// 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:// 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)) @@ -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:/ with an Accept @@ -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 } diff --git a/swarm/api/http/server_test.go b/swarm/api/http/server_test.go old mode 100755 new mode 100644 index d725429a96e1..dbbfa3b071c8 --- a/swarm/api/http/server_test.go +++ b/swarm/api/http/server_test.go @@ -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 . +// along with the go-ethereum library. If not, see . package http_test @@ -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 @@ -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 @@ -229,7 +197,6 @@ 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{ @@ -237,7 +204,6 @@ func TestBzzGetPath(t *testing.T) { "error resolving nonhash: immutable address not a content hash: "nonhash"", "error resolving nonhash: no DNS to resolve name: "nonhash"", "error resolving nonhash: no DNS to resolve name: "nonhash"", - "error resolving nonhash: no DNS to resolve name: "nonhash"", } for i, url := range nonhashtests { diff --git a/swarm/api/uri.go b/swarm/api/uri.go old mode 100755 new mode 100644 index d8aafedf4138..af1dc7445320 --- a/swarm/api/uri.go +++ b/swarm/api/uri.go @@ -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 @@ -58,7 +56,7 @@ type URI struct { // * :// // * :/// // -// 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) @@ -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) } @@ -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 } diff --git a/swarm/api/uri_test.go b/swarm/api/uri_test.go old mode 100755 new mode 100644 index 137b4505d414..babb2834e931 --- a/swarm/api/uri_test.go +++ b/swarm/api/uri_test.go @@ -29,7 +29,6 @@ func TestParseURI(t *testing.T) { expectRaw bool expectImmutable bool expectList bool - expectHash bool expectDeprecatedRaw bool expectDeprecatedImmutable bool } @@ -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"}, @@ -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()) }