diff options
author | Janoš Guljaš <janos@users.noreply.github.com> | 2017-12-21 20:47:10 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-12-21 20:47:10 +0800 |
commit | 542d51895f54b9f869379cf4ad5549c82e525711 (patch) | |
tree | 4f485778b2c28de2d7c4ab446407e7f01eab9b20 /swarm/api/http/server.go | |
parent | 5258785c81959109138ebeca613f12c277188abc (diff) | |
download | go-tangerine-542d51895f54b9f869379cf4ad5549c82e525711.tar go-tangerine-542d51895f54b9f869379cf4ad5549c82e525711.tar.gz go-tangerine-542d51895f54b9f869379cf4ad5549c82e525711.tar.bz2 go-tangerine-542d51895f54b9f869379cf4ad5549c82e525711.tar.lz go-tangerine-542d51895f54b9f869379cf4ad5549c82e525711.tar.xz go-tangerine-542d51895f54b9f869379cf4ad5549c82e525711.tar.zst go-tangerine-542d51895f54b9f869379cf4ad5549c82e525711.zip |
swarm/api: url scheme bzz-hash to get hashes of swarm content (#15238) (#15715)
* swarm/api: url scheme bzz-hash to get hashes of swarm content (#15238)
Update URI to support bzz-hash scheme and handle such HTTP requests by
responding with hash of the content as a text/plain response.
* swarm/api: return hash of the content for bzz-hash:// requests
* swarm/api: revert "return hash of the content for bzz-hash:// requests"
Return hashes of the content that would be returned by bzz-raw
request.
* swarm/api/http: handle error in TestBzzGetPath
* swarm/api: remove extra blank line in comment
Diffstat (limited to 'swarm/api/http/server.go')
-rw-r--r-- | swarm/api/http/server.go | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go index 3872cbc4f..74341899d 100644 --- a/swarm/api/http/server.go +++ b/swarm/api/http/server.go @@ -290,9 +290,12 @@ func (s *Server) HandleDelete(w http.ResponseWriter, r *Request) { fmt.Fprint(w, newKey) } -// 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) { +// 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) { key, err := s.api.Resolve(r.uri) if err != nil { s.Error(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err)) @@ -345,15 +348,22 @@ func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) { return } - // 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) + 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) + 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 @@ -619,8 +629,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.HandleDelete(w, req) case "GET": - if uri.Raw() || uri.DeprecatedRaw() { - s.HandleGetRaw(w, req) + if uri.Raw() || uri.Hash() || uri.DeprecatedRaw() { + s.HandleGet(w, req) return } |