mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
Revert swarm.hash query parameter related changes
Revert changes to the original bzzh:// schema solution.
This commit is contained in:
parent
e9f38d726e
commit
b18003d99a
4 changed files with 60 additions and 75 deletions
|
|
@ -290,51 +290,16 @@ 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
|
// HandleGet handles a GET request to
|
||||||
// the raw content stored at the given storage key
|
// - bzzr://<key> and responds with the raw content stored at the
|
||||||
func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) {
|
// given storage key
|
||||||
_, reader, ok := s.handleGet(w, r)
|
// - bzzh://<key> and responds with the hash of the content stored
|
||||||
if !ok {
|
// at the given storage key as a text/plain response
|
||||||
return
|
func (s *Server) HandleGet(w http.ResponseWriter, r *Request) {
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleGetHash handles a GET request to bzz://<key> with query parameter
|
|
||||||
// hash=true, and responds with the hash of the content stored
|
|
||||||
// at the given storage key as a application/bzz-hash response
|
|
||||||
func (s *Server) HandleGetHash(w http.ResponseWriter, r *Request) {
|
|
||||||
key, _, ok := s.handleGet(w, r)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/bzz-hash")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
fmt.Fprint(w, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleGet is a handler that is used in HandleGetRaw and HandleGetHash methods
|
|
||||||
// to provide storage Key and LazySectionReader for the requested path.
|
|
||||||
//
|
|
||||||
// This method accepts http.ResponseWriter to respond errors and in case of
|
|
||||||
// errors, the third returned value will be false, indicating that the request
|
|
||||||
// is not valid, error is written to the response and nothing more should be
|
|
||||||
// written.
|
|
||||||
func (s *Server) handleGet(w http.ResponseWriter, r *Request) (key storage.Key, reader storage.LazySectionReader, ok bool) {
|
|
||||||
key, err := s.api.Resolve(r.uri)
|
key, err := s.api.Resolve(r.uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Error(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err))
|
s.Error(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err))
|
||||||
return nil, nil, false
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// if path is set, interpret <key> as a manifest and return the
|
// if path is set, interpret <key> as a manifest and return the
|
||||||
|
|
@ -343,7 +308,7 @@ func (s *Server) handleGet(w http.ResponseWriter, r *Request) (key storage.Key,
|
||||||
walker, err := s.api.NewManifestWalker(key, nil)
|
walker, err := s.api.NewManifestWalker(key, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.BadRequest(w, r, fmt.Sprintf("%s is not a manifest", key))
|
s.BadRequest(w, r, fmt.Sprintf("%s is not a manifest", key))
|
||||||
return nil, nil, false
|
return
|
||||||
}
|
}
|
||||||
var entry *api.ManifestEntry
|
var entry *api.ManifestEntry
|
||||||
walker.Walk(func(e *api.ManifestEntry) error {
|
walker.Walk(func(e *api.ManifestEntry) error {
|
||||||
|
|
@ -371,18 +336,34 @@ func (s *Server) handleGet(w http.ResponseWriter, r *Request) (key storage.Key,
|
||||||
})
|
})
|
||||||
if entry == nil {
|
if entry == nil {
|
||||||
s.NotFound(w, r, fmt.Errorf("Manifest entry could not be loaded"))
|
s.NotFound(w, r, fmt.Errorf("Manifest entry could not be loaded"))
|
||||||
return nil, nil, false
|
return
|
||||||
}
|
}
|
||||||
key = storage.Key(common.Hex2Bytes(entry.Hash))
|
key = storage.Key(common.Hex2Bytes(entry.Hash))
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the root chunk exists by retrieving the file's size
|
// check the root chunk exists by retrieving the file's size
|
||||||
reader = s.api.Retrieve(key)
|
reader := s.api.Retrieve(key)
|
||||||
if _, err := reader.Size(nil); err != nil {
|
if _, err := reader.Size(nil); err != nil {
|
||||||
s.NotFound(w, r, fmt.Errorf("Root chunk not found %s: %s", key, err))
|
s.NotFound(w, r, fmt.Errorf("Root chunk not found %s: %s", key, err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return key, reader, true
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleGetFiles handles a GET request to bzz:/<manifest> with an Accept
|
// HandleGetFiles handles a GET request to bzz:/<manifest> with an Accept
|
||||||
|
|
@ -645,8 +626,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
s.HandleDelete(w, req)
|
s.HandleDelete(w, req)
|
||||||
|
|
||||||
case "GET":
|
case "GET":
|
||||||
if uri.Raw() {
|
if uri.Raw() || uri.Hash() {
|
||||||
s.HandleGetRaw(w, req)
|
s.HandleGet(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -655,24 +636,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
getList := r.URL.Query().Get("list") == "true"
|
if r.URL.Query().Get("list") == "true" {
|
||||||
getHash := r.URL.Query().Get("swarm.hash") == "true"
|
|
||||||
|
|
||||||
if getList && getHash {
|
|
||||||
s.BadRequest(w, req, "query parameters list and hash can not be requested at the same time")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if getList {
|
|
||||||
s.HandleGetList(w, req)
|
s.HandleGetList(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if getHash {
|
|
||||||
s.HandleGetHash(w, req)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
s.HandleGetFile(w, req)
|
s.HandleGetFile(w, req)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/swarm/testutil"
|
"github.com/ethereum/go-ethereum/swarm/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBzzrGetPath(t *testing.T) {
|
func TestBzzGetPath(t *testing.T) {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
@ -104,14 +104,13 @@ func TestBzzrGetPath(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// test hash requests
|
|
||||||
for k, v := range testrequests {
|
for k, v := range testrequests {
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
var respbody []byte
|
var respbody []byte
|
||||||
|
|
||||||
url := srv.URL + "/bzz:/"
|
url := srv.URL + "/bzzh:/"
|
||||||
if k[:] != "" {
|
if k[:] != "" {
|
||||||
url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?swarm.hash=true"
|
url += common.ToHex(key[0])[2:] + "/" + k[1:]
|
||||||
}
|
}
|
||||||
resp, err = http.Get(url)
|
resp, err = http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -134,23 +133,21 @@ func TestBzzrGetPath(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errorTests := []string{
|
nonhashtests := []string{
|
||||||
srv.URL + "/bzz:/name",
|
srv.URL + "/bzz:/name",
|
||||||
srv.URL + "/bzzi:/nonhash",
|
srv.URL + "/bzzi:/nonhash",
|
||||||
srv.URL + "/bzzr:/nonhash",
|
srv.URL + "/bzzr:/nonhash",
|
||||||
srv.URL + "/bzz:/nonhash?swarm.hash=true",
|
srv.URL + "/bzzh:/nonhash",
|
||||||
srv.URL + "/bzz:/a?swarm.hash=true&list=true",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorResponses := []string{
|
nonhashresponses := []string{
|
||||||
"error resolving name: no DNS to resolve name: "name"",
|
"error resolving name: no DNS to resolve name: "name"",
|
||||||
"error resolving nonhash: immutable address not a content hash: "nonhash"",
|
"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"",
|
"error resolving nonhash: no DNS to resolve name: "nonhash"",
|
||||||
"query parameters list and hash can not be requested at the same time",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, url := range errorTests {
|
for i, url := range nonhashtests {
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
var respbody []byte
|
var respbody []byte
|
||||||
|
|
||||||
|
|
@ -164,8 +161,8 @@ func TestBzzrGetPath(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ReadAll failed: %v", err)
|
t.Fatalf("ReadAll failed: %v", err)
|
||||||
}
|
}
|
||||||
if !strings.Contains(string(respbody), errorResponses[i]) {
|
if !strings.Contains(string(respbody), nonhashresponses[i]) {
|
||||||
t.Fatalf("Non-Hash response body does not match, expected: %v, got: %v", errorResponses[i], string(respbody))
|
t.Fatalf("Non-Hash response body does not match, expected: %v, got: %v", nonhashresponses[i], string(respbody))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,8 @@ type URI struct {
|
||||||
// * 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)
|
||||||
|
// * bzzh - hash of swarm content
|
||||||
|
//
|
||||||
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
|
||||||
|
|
@ -60,7 +62,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":
|
case "bzz", "bzzi", "bzzr", "bzzh":
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unknown scheme %q", u.Scheme)
|
return nil, fmt.Errorf("unknown scheme %q", u.Scheme)
|
||||||
}
|
}
|
||||||
|
|
@ -91,6 +93,10 @@ func (u *URI) Immutable() bool {
|
||||||
return u.Scheme == "bzzi"
|
return u.Scheme == "bzzi"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *URI) Hash() bool {
|
||||||
|
return u.Scheme == "bzzh"
|
||||||
|
}
|
||||||
|
|
||||||
func (u *URI) String() string {
|
func (u *URI) String() string {
|
||||||
return u.Scheme + ":/" + u.Addr + "/" + u.Path
|
return u.Scheme + ":/" + u.Addr + "/" + u.Path
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ func TestParseURI(t *testing.T) {
|
||||||
expectErr bool
|
expectErr bool
|
||||||
expectRaw bool
|
expectRaw bool
|
||||||
expectImmutable bool
|
expectImmutable bool
|
||||||
|
expectHash bool
|
||||||
}
|
}
|
||||||
tests := []test{
|
tests := []test{
|
||||||
{
|
{
|
||||||
|
|
@ -95,6 +96,16 @@ func TestParseURI(t *testing.T) {
|
||||||
uri: "bzz://abc123/path/to/entry",
|
uri: "bzz://abc123/path/to/entry",
|
||||||
expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
|
expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
uri: "bzzh:",
|
||||||
|
expectURI: &URI{Scheme: "bzzh"},
|
||||||
|
expectHash: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uri: "bzzh:/",
|
||||||
|
expectURI: &URI{Scheme: "bzzh"},
|
||||||
|
expectHash: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, x := range tests {
|
for _, x := range tests {
|
||||||
actual, err := Parse(x.uri)
|
actual, err := Parse(x.uri)
|
||||||
|
|
@ -116,5 +127,8 @@ func TestParseURI(t *testing.T) {
|
||||||
if actual.Immutable() != x.expectImmutable {
|
if actual.Immutable() != x.expectImmutable {
|
||||||
t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable())
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue