mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
bzz-list pulled out, tests green
This commit is contained in:
parent
0fa647053b
commit
3eb948bca6
4 changed files with 77 additions and 74 deletions
|
|
@ -29,6 +29,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type BzzListController struct {
|
type BzzListController struct {
|
||||||
|
Api *api.API
|
||||||
|
|
||||||
*Controller
|
*Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,7 +46,7 @@ func (controller *BzzListController) Get(w http.ResponseWriter, r *messages.Requ
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, err := controller.api.Resolve(r.Uri)
|
addr, err := controller.Api.Resolve(r.Uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//getListFail.Inc(1)
|
//getListFail.Inc(1)
|
||||||
controller.Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
controller.Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
||||||
|
|
@ -52,7 +54,7 @@ func (controller *BzzListController) Get(w http.ResponseWriter, r *messages.Requ
|
||||||
}
|
}
|
||||||
log.Debug("handle.get.list: resolved", "ruid", r.Ruid, "key", addr)
|
log.Debug("handle.get.list: resolved", "ruid", r.Ruid, "key", addr)
|
||||||
|
|
||||||
list, err := controller.getManifestList(addr, r.Uri.Path)
|
list, err := controller.GetManifestList(addr, r.Uri.Path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// getListFail.Inc(1)
|
// getListFail.Inc(1)
|
||||||
|
|
@ -83,8 +85,8 @@ func (controller *BzzListController) Get(w http.ResponseWriter, r *messages.Requ
|
||||||
json.NewEncoder(w).Encode(&list)
|
json.NewEncoder(w).Encode(&list)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (controller *BzzListController) getManifestList(addr storage.Address, prefix string) (list api.ManifestList, err error) {
|
func (controller *BzzListController) GetManifestList(addr storage.Address, prefix string) (list api.ManifestList, err error) {
|
||||||
walker, err := controller.api.NewManifestWalker(addr, nil)
|
walker, err := controller.Api.NewManifestWalker(addr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/swarm/api"
|
|
||||||
"github.com/ethereum/go-ethereum/swarm/api/http/messages"
|
"github.com/ethereum/go-ethereum/swarm/api/http/messages"
|
||||||
"github.com/ethereum/go-ethereum/swarm/api/http/views"
|
"github.com/ethereum/go-ethereum/swarm/api/http/views"
|
||||||
l "github.com/ethereum/go-ethereum/swarm/log"
|
l "github.com/ethereum/go-ethereum/swarm/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
api *api.API
|
|
||||||
|
|
||||||
ControllerHandler
|
ControllerHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -57,8 +54,8 @@ func (controller *Controller) Respond(w http.ResponseWriter, req *messages.Reque
|
||||||
func respond(w http.ResponseWriter, r *http.Request, params *messages.ResponseParams) {
|
func respond(w http.ResponseWriter, r *http.Request, params *messages.ResponseParams) {
|
||||||
w.WriteHeader(params.Code)
|
w.WriteHeader(params.Code)
|
||||||
if r.Header.Get("Accept") == "application/json" {
|
if r.Header.Get("Accept") == "application/json" {
|
||||||
// respondJSON(w, params)
|
views.RespondJSON(w, params)
|
||||||
} else {
|
} else {
|
||||||
// respondHTML(w, params)
|
views.RespondHTML(w, params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/swarm/api"
|
"github.com/ethereum/go-ethereum/swarm/api"
|
||||||
"github.com/ethereum/go-ethereum/swarm/api/http/controllers"
|
"github.com/ethereum/go-ethereum/swarm/api/http/controllers"
|
||||||
"github.com/ethereum/go-ethereum/swarm/api/http/messages"
|
"github.com/ethereum/go-ethereum/swarm/api/http/messages"
|
||||||
|
"github.com/ethereum/go-ethereum/swarm/api/http/views"
|
||||||
"github.com/ethereum/go-ethereum/swarm/log"
|
"github.com/ethereum/go-ethereum/swarm/log"
|
||||||
"github.com/ethereum/go-ethereum/swarm/storage"
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||||
"github.com/ethereum/go-ethereum/swarm/storage/mru"
|
"github.com/ethereum/go-ethereum/swarm/storage/mru"
|
||||||
|
|
@ -105,10 +106,11 @@ func StartHTTPServer(api *api.API, config *ServerConfig) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(api *api.API) *Server {
|
func NewServer(api *api.API) *Server {
|
||||||
return &Server{api}
|
return &Server{api: api}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
|
controllers.Controller
|
||||||
api *api.API
|
api *api.API
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,25 +128,25 @@ func (s *Server) HandlePostRaw(w http.ResponseWriter, r *messages.Request) {
|
||||||
|
|
||||||
if r.Uri.Path != "" {
|
if r.Uri.Path != "" {
|
||||||
postRawFail.Inc(1)
|
postRawFail.Inc(1)
|
||||||
Respond(w, r, "raw POST request cannot contain a path", http.StatusBadRequest)
|
s.Respond(w, r, "raw POST request cannot contain a path", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Uri.Addr != "" && r.Uri.Addr != "encrypt" {
|
if r.Uri.Addr != "" && r.Uri.Addr != "encrypt" {
|
||||||
postRawFail.Inc(1)
|
postRawFail.Inc(1)
|
||||||
Respond(w, r, "raw POST request addr can only be empty or \"encrypt\"", http.StatusBadRequest)
|
s.Respond(w, r, "raw POST request addr can only be empty or \"encrypt\"", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Header.Get("Content-Length") == "" {
|
if r.Header.Get("Content-Length") == "" {
|
||||||
postRawFail.Inc(1)
|
postRawFail.Inc(1)
|
||||||
Respond(w, r, "missing Content-Length header in request", http.StatusBadRequest)
|
s.Respond(w, r, "missing Content-Length header in request", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
addr, _, err := s.api.Store(r.Body, r.ContentLength, toEncrypt)
|
addr, _, err := s.api.Store(r.Body, r.ContentLength, toEncrypt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postRawFail.Inc(1)
|
postRawFail.Inc(1)
|
||||||
Respond(w, r, err.Error(), http.StatusInternalServerError)
|
s.Respond(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,7 +169,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *messages.Request) {
|
||||||
contentType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
contentType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postFilesFail.Inc(1)
|
postFilesFail.Inc(1)
|
||||||
Respond(w, r, err.Error(), http.StatusBadRequest)
|
s.Respond(w, r, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,10 +180,10 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *messages.Request) {
|
||||||
|
|
||||||
var addr storage.Address
|
var addr storage.Address
|
||||||
if r.Uri.Addr != "" && r.Uri.Addr != "encrypt" {
|
if r.Uri.Addr != "" && r.Uri.Addr != "encrypt" {
|
||||||
addr, err = s.api.Resolve(r.uri)
|
addr, err = s.api.Resolve(r.Uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postFilesFail.Inc(1)
|
postFilesFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusInternalServerError)
|
s.Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("resolved key", "ruid", r.Ruid, "key", addr)
|
log.Debug("resolved key", "ruid", r.Ruid, "key", addr)
|
||||||
|
|
@ -189,7 +191,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *messages.Request) {
|
||||||
addr, err = s.api.NewManifest(toEncrypt)
|
addr, err = s.api.NewManifest(toEncrypt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postFilesFail.Inc(1)
|
postFilesFail.Inc(1)
|
||||||
Respond(w, r, err.Error(), http.StatusInternalServerError)
|
s.Respond(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("new manifest", "ruid", r.Ruid, "key", addr)
|
log.Debug("new manifest", "ruid", r.Ruid, "key", addr)
|
||||||
|
|
@ -210,7 +212,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *messages.Request) {
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postFilesFail.Inc(1)
|
postFilesFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("cannot create manifest: %s", err), http.StatusInternalServerError)
|
s.Respond(w, r, fmt.Sprintf("cannot create manifest: %s", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -336,10 +338,10 @@ func (s *Server) HandleDelete(w http.ResponseWriter, r *messages.Request) {
|
||||||
log.Debug("handle.delete", "ruid", r.Ruid)
|
log.Debug("handle.delete", "ruid", r.Ruid)
|
||||||
|
|
||||||
deleteCount.Inc(1)
|
deleteCount.Inc(1)
|
||||||
key, err := s.api.Resolve(r.uri)
|
key, err := s.api.Resolve(r.Uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
deleteFail.Inc(1)
|
deleteFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusInternalServerError)
|
s.Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -349,7 +351,7 @@ func (s *Server) HandleDelete(w http.ResponseWriter, r *messages.Request) {
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
deleteFail.Inc(1)
|
deleteFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("cannot update manifest: %s", err), http.StatusInternalServerError)
|
s.Respond(w, r, fmt.Sprintf("cannot update manifest: %s", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -401,7 +403,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *messages.Request)
|
||||||
var outdata []byte
|
var outdata []byte
|
||||||
isRaw, frequency, err := resourcePostMode(r.Uri.Path)
|
isRaw, frequency, err := resourcePostMode(r.Uri.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Respond(w, r, err.Error(), http.StatusBadRequest)
|
s.Respond(w, r, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -415,7 +417,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *messages.Request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code, err2 := s.translateResourceError(w, r, "resource creation fail", err)
|
code, err2 := s.translateResourceError(w, r, "resource creation fail", err)
|
||||||
|
|
||||||
Respond(w, r, err2.Error(), code)
|
s.Respond(w, r, err2.Error(), code)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -424,7 +426,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *messages.Request)
|
||||||
// root chunk
|
// root chunk
|
||||||
m, err := s.api.NewResourceManifest(addr.Hex())
|
m, err := s.api.NewResourceManifest(addr.Hex())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Respond(w, r, fmt.Sprintf("failed to create resource manifest: %v", err), http.StatusInternalServerError)
|
s.Respond(w, r, fmt.Sprintf("failed to create resource manifest: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -434,7 +436,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *messages.Request)
|
||||||
// \TODO update manifest key automatically in ENS
|
// \TODO update manifest key automatically in ENS
|
||||||
outdata, err = json.Marshal(m)
|
outdata, err = json.Marshal(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Respond(w, r, fmt.Sprintf("failed to create json response: %s", err), http.StatusInternalServerError)
|
s.Respond(w, r, fmt.Sprintf("failed to create json response: %s", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -442,10 +444,10 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *messages.Request)
|
||||||
// that means that we retrieve the manifest and inspect its Hash member.
|
// that means that we retrieve the manifest and inspect its Hash member.
|
||||||
manifestAddr := r.Uri.Address()
|
manifestAddr := r.Uri.Address()
|
||||||
if manifestAddr == nil {
|
if manifestAddr == nil {
|
||||||
manifestAddr, err = s.api.Resolve(r.uri)
|
manifestAddr, err = s.api.Resolve(r.Uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFail.Inc(1)
|
getFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -456,7 +458,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *messages.Request)
|
||||||
addr, err = s.api.ResolveResourceManifest(manifestAddr)
|
addr, err = s.api.ResolveResourceManifest(manifestAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFail.Inc(1)
|
getFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("error resolving resource root chunk for %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("error resolving resource root chunk for %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -464,7 +466,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *messages.Request)
|
||||||
|
|
||||||
name, _, err = s.api.ResourceLookup(r.Context(), addr, 0, 0, &mru.LookupParams{})
|
name, _, err = s.api.ResourceLookup(r.Context(), addr, 0, 0, &mru.LookupParams{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Respond(w, r, err.Error(), http.StatusNotFound)
|
s.Respond(w, r, err.Error(), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -472,7 +474,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *messages.Request)
|
||||||
// Creation and update must send data aswell. This data constitutes the update data itself.
|
// Creation and update must send data aswell. This data constitutes the update data itself.
|
||||||
data, err := ioutil.ReadAll(r.Body)
|
data, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Respond(w, r, err.Error(), http.StatusInternalServerError)
|
s.Respond(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -480,18 +482,18 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *messages.Request)
|
||||||
if isRaw {
|
if isRaw {
|
||||||
_, _, _, err = s.api.ResourceUpdate(r.Context(), name, data)
|
_, _, _, err = s.api.ResourceUpdate(r.Context(), name, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Respond(w, r, err.Error(), http.StatusBadRequest)
|
s.Respond(w, r, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bytesdata, err := hexutil.Decode(string(data))
|
bytesdata, err := hexutil.Decode(string(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Respond(w, r, err.Error(), http.StatusBadRequest)
|
s.Respond(w, r, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, _, _, err = s.api.ResourceUpdateMultihash(r.Context(), name, bytesdata)
|
_, _, _, err = s.api.ResourceUpdateMultihash(r.Context(), name, bytesdata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Respond(w, r, err.Error(), http.StatusBadRequest)
|
s.Respond(w, r, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -524,10 +526,10 @@ func (s *Server) handleGetResource(w http.ResponseWriter, r *messages.Request) {
|
||||||
// resolve the content key.
|
// resolve the content key.
|
||||||
manifestAddr := r.Uri.Address()
|
manifestAddr := r.Uri.Address()
|
||||||
if manifestAddr == nil {
|
if manifestAddr == nil {
|
||||||
manifestAddr, err = s.api.Resolve(r.uri)
|
manifestAddr, err = s.api.Resolve(r.Uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFail.Inc(1)
|
getFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -538,7 +540,7 @@ func (s *Server) handleGetResource(w http.ResponseWriter, r *messages.Request) {
|
||||||
key, err := s.api.ResolveResourceManifest(manifestAddr)
|
key, err := s.api.ResolveResourceManifest(manifestAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFail.Inc(1)
|
getFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("error resolving resource root chunk for %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("error resolving resource root chunk for %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -581,7 +583,7 @@ func (s *Server) handleGetResource(w http.ResponseWriter, r *messages.Request) {
|
||||||
// any error from the switch statement will end up here
|
// any error from the switch statement will end up here
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code, err2 := s.translateResourceError(w, r, "mutable resource lookup fail", err)
|
code, err2 := s.translateResourceError(w, r, "mutable resource lookup fail", err)
|
||||||
Respond(w, r, err2.Error(), code)
|
s.Respond(w, r, err2.Error(), code)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -618,15 +620,15 @@ func (s *Server) translateResourceError(w http.ResponseWriter, r *messages.Reque
|
||||||
// - bzz-hash://<key> and responds with the hash of the content stored
|
// - bzz-hash://<key> and responds with the hash of the content stored
|
||||||
// at the given storage key as a text/plain response
|
// at the given storage key as a text/plain response
|
||||||
func (s *Server) HandleGet(w http.ResponseWriter, r *messages.Request) {
|
func (s *Server) HandleGet(w http.ResponseWriter, r *messages.Request) {
|
||||||
log.Debug("handle.get", "ruid", r.Ruid, "uri", r.uri)
|
log.Debug("handle.get", "ruid", r.Ruid, "uri", r.Uri)
|
||||||
getCount.Inc(1)
|
getCount.Inc(1)
|
||||||
var err error
|
var err error
|
||||||
addr := r.Uri.Address()
|
addr := r.Uri.Address()
|
||||||
if addr == nil {
|
if addr == nil {
|
||||||
addr, err = s.api.Resolve(r.uri)
|
addr, err = s.api.Resolve(r.Uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFail.Inc(1)
|
getFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -641,7 +643,7 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *messages.Request) {
|
||||||
walker, err := s.api.NewManifestWalker(addr, nil)
|
walker, err := s.api.NewManifestWalker(addr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFail.Inc(1)
|
getFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("%s is not a manifest", addr), http.StatusBadRequest)
|
s.Respond(w, r, fmt.Sprintf("%s is not a manifest", addr), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var entry *api.ManifestEntry
|
var entry *api.ManifestEntry
|
||||||
|
|
@ -670,7 +672,7 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *messages.Request) {
|
||||||
})
|
})
|
||||||
if entry == nil {
|
if entry == nil {
|
||||||
getFail.Inc(1)
|
getFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("manifest entry could not be loaded"), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("manifest entry could not be loaded"), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
addr = storage.Address(common.Hex2Bytes(entry.Hash))
|
addr = storage.Address(common.Hex2Bytes(entry.Hash))
|
||||||
|
|
@ -680,7 +682,7 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *messages.Request) {
|
||||||
w.Header().Set("ETag", fmt.Sprintf("%q", etag)) // set etag to manifest key or raw entry key.
|
w.Header().Set("ETag", fmt.Sprintf("%q", etag)) // set etag to manifest key or raw entry key.
|
||||||
if noneMatchEtag != "" {
|
if noneMatchEtag != "" {
|
||||||
if bytes.Equal(storage.Address(common.Hex2Bytes(noneMatchEtag)), addr) {
|
if bytes.Equal(storage.Address(common.Hex2Bytes(noneMatchEtag)), addr) {
|
||||||
Respond(w, r, "Not Modified", http.StatusNotModified)
|
s.Respond(w, r, "Not Modified", http.StatusNotModified)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -689,7 +691,7 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *messages.Request) {
|
||||||
reader, isEncrypted := s.api.Retrieve(addr)
|
reader, isEncrypted := s.api.Retrieve(addr)
|
||||||
if _, err := reader.Size(nil); err != nil {
|
if _, err := reader.Size(nil); err != nil {
|
||||||
getFail.Inc(1)
|
getFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("root chunk not found %s: %s", addr, err), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("root chunk not found %s: %s", addr, err), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -716,18 +718,18 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *messages.Request) {
|
||||||
// header of "application/x-tar" and returns a tar stream of all files
|
// header of "application/x-tar" and returns a tar stream of all files
|
||||||
// contained in the manifest
|
// contained in the manifest
|
||||||
func (s *Server) HandleGetFiles(w http.ResponseWriter, r *messages.Request) {
|
func (s *Server) HandleGetFiles(w http.ResponseWriter, r *messages.Request) {
|
||||||
log.Debug("handle.get.files", "ruid", r.Ruid, "uri", r.uri)
|
log.Debug("handle.get.files", "ruid", r.Ruid, "uri", r.Uri)
|
||||||
getFilesCount.Inc(1)
|
getFilesCount.Inc(1)
|
||||||
if r.Uri.Path != "" {
|
if r.Uri.Path != "" {
|
||||||
getFilesFail.Inc(1)
|
getFilesFail.Inc(1)
|
||||||
Respond(w, r, "files request cannot contain a path", http.StatusBadRequest)
|
s.Respond(w, r, "files request cannot contain a path", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, err := s.api.Resolve(r.uri)
|
addr, err := s.api.Resolve(r.Uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFilesFail.Inc(1)
|
getFilesFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("handle.get.files: resolved", "ruid", r.Ruid, "key", addr)
|
log.Debug("handle.get.files: resolved", "ruid", r.Ruid, "key", addr)
|
||||||
|
|
@ -735,7 +737,7 @@ func (s *Server) HandleGetFiles(w http.ResponseWriter, r *messages.Request) {
|
||||||
walker, err := s.api.NewManifestWalker(addr, nil)
|
walker, err := s.api.NewManifestWalker(addr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFilesFail.Inc(1)
|
getFilesFail.Inc(1)
|
||||||
Respond(w, r, err.Error(), http.StatusInternalServerError)
|
s.Respond(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -802,10 +804,10 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *messages.Request) {
|
||||||
manifestAddr := r.Uri.Address()
|
manifestAddr := r.Uri.Address()
|
||||||
|
|
||||||
if manifestAddr == nil {
|
if manifestAddr == nil {
|
||||||
manifestAddr, err = s.api.Resolve(r.uri)
|
manifestAddr, err = s.api.Resolve(r.Uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFileFail.Inc(1)
|
getFileFail.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.Uri.Addr, err), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -821,7 +823,7 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *messages.Request) {
|
||||||
w.Header().Set("ETag", fmt.Sprintf("%q", etag)) // set etag to actual content key.
|
w.Header().Set("ETag", fmt.Sprintf("%q", etag)) // set etag to actual content key.
|
||||||
if noneMatchEtag != "" {
|
if noneMatchEtag != "" {
|
||||||
if bytes.Equal(storage.Address(common.Hex2Bytes(noneMatchEtag)), contentKey) {
|
if bytes.Equal(storage.Address(common.Hex2Bytes(noneMatchEtag)), contentKey) {
|
||||||
Respond(w, r, "Not Modified", http.StatusNotModified)
|
s.Respond(w, r, "Not Modified", http.StatusNotModified)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -830,10 +832,10 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *messages.Request) {
|
||||||
switch status {
|
switch status {
|
||||||
case http.StatusNotFound:
|
case http.StatusNotFound:
|
||||||
getFileNotFound.Inc(1)
|
getFileNotFound.Inc(1)
|
||||||
Respond(w, r, err.Error(), http.StatusNotFound)
|
s.Respond(w, r, err.Error(), http.StatusNotFound)
|
||||||
default:
|
default:
|
||||||
getFileFail.Inc(1)
|
getFileFail.Inc(1)
|
||||||
Respond(w, r, err.Error(), http.StatusInternalServerError)
|
s.Respond(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -841,24 +843,26 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *messages.Request) {
|
||||||
//the request results in ambiguous files
|
//the request results in ambiguous files
|
||||||
//e.g. /read with readme.md and readinglist.txt available in manifest
|
//e.g. /read with readme.md and readinglist.txt available in manifest
|
||||||
if status == http.StatusMultipleChoices {
|
if status == http.StatusMultipleChoices {
|
||||||
list, err := s.getManifestList(manifestAddr, r.Uri.Path)
|
listCtrl := controllers.BzzListController{Api: s.api}
|
||||||
|
|
||||||
|
list, err := listCtrl.GetManifestList(manifestAddr, r.Uri.Path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFileFail.Inc(1)
|
getFileFail.Inc(1)
|
||||||
Respond(w, r, err.Error(), http.StatusInternalServerError)
|
s.Respond(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug(fmt.Sprintf("Multiple choices! --> %v", list), "ruid", r.Ruid)
|
log.Debug(fmt.Sprintf("Multiple choices! --> %v", list), "ruid", r.Ruid)
|
||||||
//show a nice page links to available entries
|
//show a nice page links to available entries
|
||||||
ShowMultipleChoices(w, r, list)
|
views.ShowMultipleChoices(w, r, list)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the root chunk exists by retrieving the file's size
|
// check the root chunk exists by retrieving the file's size
|
||||||
if _, err := reader.Size(nil); err != nil {
|
if _, err := reader.Size(nil); err != nil {
|
||||||
getFileNotFound.Inc(1)
|
getFileNotFound.Inc(1)
|
||||||
Respond(w, r, fmt.Sprintf("file not found %s: %s", r.uri, err), http.StatusNotFound)
|
s.Respond(w, r, fmt.Sprintf("file not found %s: %s", r.Uri, err), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -930,11 +934,11 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))
|
uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Respond(w, req, fmt.Sprintf("invalid URI %q", r.URL.Path), http.StatusBadRequest)
|
s.Respond(w, req, fmt.Sprintf("invalid URI %q", r.URL.Path), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req.uri = uri
|
req.Uri = uri
|
||||||
|
|
||||||
log.Debug("parsed request path", "ruid", req.Ruid, "method", req.Method, "uri.Addr", req.Uri.Addr, "uri.Path", req.Uri.Path, "uri.Scheme", req.Uri.Scheme)
|
log.Debug("parsed request path", "ruid", req.Ruid, "method", req.Method, "uri.Addr", req.Uri.Addr, "uri.Path", req.Uri.Path, "uri.Scheme", req.Uri.Scheme)
|
||||||
|
|
||||||
|
|
@ -948,19 +952,19 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
s.HandlePostResource(w, req)
|
s.HandlePostResource(w, req)
|
||||||
} else if uri.Immutable() || uri.List() || uri.Hash() {
|
} else if uri.Immutable() || uri.List() || uri.Hash() {
|
||||||
log.Debug("POST not allowed on immutable, list or hash")
|
log.Debug("POST not allowed on immutable, list or hash")
|
||||||
Respond(w, req, fmt.Sprintf("POST method on scheme %s not allowed", uri.Scheme), http.StatusMethodNotAllowed)
|
s.Respond(w, req, fmt.Sprintf("POST method on scheme %s not allowed", uri.Scheme), http.StatusMethodNotAllowed)
|
||||||
} else {
|
} else {
|
||||||
log.Debug("handlePostFiles")
|
log.Debug("handlePostFiles")
|
||||||
s.HandlePostFiles(w, req)
|
s.HandlePostFiles(w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "PUT":
|
case "PUT":
|
||||||
Respond(w, req, fmt.Sprintf("PUT method to %s not allowed", uri), http.StatusBadRequest)
|
s.Respond(w, req, fmt.Sprintf("PUT method to %s not allowed", uri), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
if uri.Raw() {
|
if uri.Raw() {
|
||||||
Respond(w, req, fmt.Sprintf("DELETE method to %s not allowed", uri), http.StatusBadRequest)
|
s.Respond(w, req, fmt.Sprintf("DELETE method to %s not allowed", uri), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.HandleDelete(w, req)
|
s.HandleDelete(w, req)
|
||||||
|
|
@ -978,7 +982,7 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if uri.List() {
|
if uri.List() {
|
||||||
list := &controllers.BzzListController{api: s.api}
|
list := &controllers.BzzListController{Api: s.api}
|
||||||
list.Get(w, req)
|
list.Get(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -991,7 +995,7 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
s.HandleGetFile(w, req)
|
s.HandleGetFile(w, req)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
Respond(w, req, fmt.Sprintf("%s method is not supported", r.Method), http.StatusMethodNotAllowed)
|
s.Respond(w, req, fmt.Sprintf("%s method is not supported", r.Method), http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("served response", "ruid", req.Ruid, "code", w.statusCode)
|
log.Info("served response", "ruid", req.Ruid, "code", w.statusCode)
|
||||||
|
|
|
||||||
|
|
@ -159,36 +159,36 @@ func Respond(w http.ResponseWriter, req *messages.Request, msg string, code int)
|
||||||
w.Header().Del("ETag")
|
w.Header().Del("ETag")
|
||||||
}
|
}
|
||||||
|
|
||||||
respond(w, &req.Request, &ResponseParams{
|
respond(w, &req.Request, &messages.ResponseParams{
|
||||||
Code: code,
|
Code: code,
|
||||||
Msg: msg,
|
Msg: msg,
|
||||||
Details: template.HTML(additionalMessage),
|
Details: template.HTML(additionalMessage),
|
||||||
Timestamp: time.Now().Format(time.RFC1123),
|
Timestamp: time.Now().Format(time.RFC1123),
|
||||||
template: GetTemplate(code),
|
Template: GetTemplate(code),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//evaluate if client accepts html or json response
|
//evaluate if client accepts html or json response
|
||||||
func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
|
func respond(w http.ResponseWriter, r *http.Request, params *messages.ResponseParams) {
|
||||||
w.WriteHeader(params.Code)
|
w.WriteHeader(params.Code)
|
||||||
if r.Header.Get("Accept") == "application/json" {
|
if r.Header.Get("Accept") == "application/json" {
|
||||||
respondJSON(w, params)
|
RespondJSON(w, params)
|
||||||
} else {
|
} else {
|
||||||
respondHTML(w, params)
|
RespondHTML(w, params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//return a HTML page
|
//return a HTML page
|
||||||
func respondHTML(w http.ResponseWriter, params *ResponseParams) {
|
func RespondHTML(w http.ResponseWriter, params *messages.ResponseParams) {
|
||||||
htmlCounter.Inc(1)
|
htmlCounter.Inc(1)
|
||||||
err := params.template.Execute(w, params)
|
err := params.Template.Execute(w, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//return JSON
|
//return JSON
|
||||||
func respondJSON(w http.ResponseWriter, params *ResponseParams) {
|
func RespondJSON(w http.ResponseWriter, params *messages.ResponseParams) {
|
||||||
jsonCounter.Inc(1)
|
jsonCounter.Inc(1)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(params)
|
json.NewEncoder(w).Encode(params)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue