aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/http/server.go
diff options
context:
space:
mode:
Diffstat (limited to 'swarm/api/http/server.go')
-rw-r--r--swarm/api/http/server.go565
1 files changed, 207 insertions, 358 deletions
diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go
index 5897a1cb9..22c091026 100644
--- a/swarm/api/http/server.go
+++ b/swarm/api/http/server.go
@@ -20,10 +20,8 @@ A simple http server interface to Swarm
package http
import (
- "archive/tar"
"bufio"
"bytes"
- "context"
"encoding/json"
"errors"
"fmt"
@@ -68,46 +66,185 @@ var (
getFileCount = metrics.NewRegisteredCounter("api.http.get.file.count", nil)
getFileNotFound = metrics.NewRegisteredCounter("api.http.get.file.notfound", nil)
getFileFail = metrics.NewRegisteredCounter("api.http.get.file.fail", nil)
- getFilesCount = metrics.NewRegisteredCounter("api.http.get.files.count", nil)
- getFilesFail = metrics.NewRegisteredCounter("api.http.get.files.fail", nil)
getListCount = metrics.NewRegisteredCounter("api.http.get.list.count", nil)
getListFail = metrics.NewRegisteredCounter("api.http.get.list.fail", nil)
)
-// ServerConfig is the basic configuration needed for the HTTP server and also
-// includes CORS settings.
-type ServerConfig struct {
- Addr string
- CorsString string
-}
-
-// browser API for registering bzz url scheme handlers:
-// https://developer.mozilla.org/en/docs/Web-based_protocol_handlers
-// electron (chromium) api for registering bzz url scheme handlers:
-// https://github.com/atom/electron/blob/master/docs/api/protocol.md
-
-// starts up http server
-func StartHTTPServer(api *api.API, config *ServerConfig) {
+func NewServer(api *api.API, corsString string) *Server {
var allowedOrigins []string
- for _, domain := range strings.Split(config.CorsString, ",") {
+ for _, domain := range strings.Split(corsString, ",") {
allowedOrigins = append(allowedOrigins, strings.TrimSpace(domain))
}
c := cors.New(cors.Options{
AllowedOrigins: allowedOrigins,
- AllowedMethods: []string{"POST", "GET", "DELETE", "PATCH", "PUT"},
+ AllowedMethods: []string{http.MethodPost, http.MethodGet, http.MethodDelete, http.MethodPatch, http.MethodPut},
MaxAge: 600,
AllowedHeaders: []string{"*"},
})
- hdlr := c.Handler(NewServer(api))
- go http.ListenAndServe(config.Addr, hdlr)
+ mux := http.NewServeMux()
+ server := &Server{api: api}
+ mux.HandleFunc("/bzz:/", server.WrapHandler(true, server.HandleBzz))
+ mux.HandleFunc("/bzz-raw:/", server.WrapHandler(true, server.HandleBzzRaw))
+ mux.HandleFunc("/bzz-immutable:/", server.WrapHandler(true, server.HandleBzzImmutable))
+ mux.HandleFunc("/bzz-hash:/", server.WrapHandler(true, server.HandleBzzHash))
+ mux.HandleFunc("/bzz-list:/", server.WrapHandler(true, server.HandleBzzList))
+ mux.HandleFunc("/bzz-resource:/", server.WrapHandler(true, server.HandleBzzResource))
+
+ mux.HandleFunc("/", server.WrapHandler(false, server.HandleRootPaths))
+ mux.HandleFunc("/robots.txt", server.WrapHandler(false, server.HandleRootPaths))
+ mux.HandleFunc("/favicon.ico", server.WrapHandler(false, server.HandleRootPaths))
+
+ server.Handler = c.Handler(mux)
+ return server
+}
+func (s *Server) ListenAndServe(addr string) error {
+ return http.ListenAndServe(addr, s)
+}
+func (s *Server) HandleRootPaths(w http.ResponseWriter, r *Request) {
+ switch r.Method {
+ case http.MethodGet:
+ if r.RequestURI == "/" {
+ if strings.Contains(r.Header.Get("Accept"), "text/html") {
+ err := landingPageTemplate.Execute(w, nil)
+ if err != nil {
+ log.Error(fmt.Sprintf("error rendering landing page: %s", err))
+ }
+ return
+ }
+ if strings.Contains(r.Header.Get("Accept"), "application/json") {
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ json.NewEncoder(w).Encode("Welcome to Swarm!")
+ return
+ }
+ }
+
+ if r.URL.Path == "/robots.txt" {
+ w.Header().Set("Last-Modified", time.Now().Format(http.TimeFormat))
+ fmt.Fprintf(w, "User-agent: *\nDisallow: /")
+ return
+ }
+ Respond(w, r, "Bad Request", http.StatusBadRequest)
+ default:
+ Respond(w, r, "Not Found", http.StatusNotFound)
+ }
+}
+func (s *Server) HandleBzz(w http.ResponseWriter, r *Request) {
+ switch r.Method {
+ case http.MethodGet:
+ log.Debug("handleGetBzz")
+ if r.Header.Get("Accept") == "application/x-tar" {
+ reader, err := s.api.GetDirectoryTar(r.Context(), r.uri)
+ if err != nil {
+ Respond(w, r, fmt.Sprintf("Had an error building the tarball: %v", err), http.StatusInternalServerError)
+ }
+ defer reader.Close()
+
+ w.Header().Set("Content-Type", "application/x-tar")
+ w.WriteHeader(http.StatusOK)
+ io.Copy(w, reader)
+ return
+ }
+ s.HandleGetFile(w, r)
+ case http.MethodPost:
+ log.Debug("handlePostFiles")
+ s.HandlePostFiles(w, r)
+ case http.MethodDelete:
+ log.Debug("handleBzzDelete")
+ s.HandleDelete(w, r)
+ default:
+ Respond(w, r, "Method not allowed", http.StatusMethodNotAllowed)
+ }
+}
+func (s *Server) HandleBzzRaw(w http.ResponseWriter, r *Request) {
+ switch r.Method {
+ case http.MethodGet:
+ log.Debug("handleGetRaw")
+ s.HandleGet(w, r)
+ case http.MethodPost:
+ log.Debug("handlePostRaw")
+ s.HandlePostRaw(w, r)
+ default:
+ Respond(w, r, "Method not allowed", http.StatusMethodNotAllowed)
+ }
+}
+func (s *Server) HandleBzzImmutable(w http.ResponseWriter, r *Request) {
+ switch r.Method {
+ case http.MethodGet:
+ log.Debug("handleGetHash")
+ s.HandleGetList(w, r)
+ default:
+ Respond(w, r, "Method not allowed", http.StatusMethodNotAllowed)
+ }
+}
+func (s *Server) HandleBzzHash(w http.ResponseWriter, r *Request) {
+ switch r.Method {
+ case http.MethodGet:
+ log.Debug("handleGetHash")
+ s.HandleGet(w, r)
+ default:
+ Respond(w, r, "Method not allowed", http.StatusMethodNotAllowed)
+ }
}
+func (s *Server) HandleBzzList(w http.ResponseWriter, r *Request) {
+ switch r.Method {
+ case http.MethodGet:
+ log.Debug("handleGetHash")
+ s.HandleGetList(w, r)
+ default:
+ Respond(w, r, "Method not allowed", http.StatusMethodNotAllowed)
+ }
+}
+func (s *Server) HandleBzzResource(w http.ResponseWriter, r *Request) {
+ switch r.Method {
+ case http.MethodGet:
+ log.Debug("handleGetResource")
+ s.HandleGetResource(w, r)
+ case http.MethodPost:
+ log.Debug("handlePostResource")
+ s.HandlePostResource(w, r)
+ default:
+ Respond(w, r, "Method not allowed", http.StatusMethodNotAllowed)
+ }
+}
+func (s *Server) WrapHandler(parseBzzUri bool, h func(http.ResponseWriter, *Request)) http.HandlerFunc {
+ return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+ defer metrics.GetOrRegisterResettingTimer(fmt.Sprintf("http.request.%s.time", r.Method), nil).UpdateSince(time.Now())
+ req := &Request{Request: *r, ruid: uuid.New()[:8]}
+ metrics.GetOrRegisterCounter(fmt.Sprintf("http.request.%s", r.Method), nil).Inc(1)
+ log.Info("serving request", "ruid", req.ruid, "method", r.Method, "url", r.RequestURI)
+
+ // wrapping the ResponseWriter, so that we get the response code set by http.ServeContent
+ w := newLoggingResponseWriter(rw)
+ if parseBzzUri {
+ uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))
+ if err != nil {
+ Respond(w, req, fmt.Sprintf("invalid URI %q", r.URL.Path), http.StatusBadRequest)
+ return
+ }
+ req.uri = uri
-func NewServer(api *api.API) *Server {
- return &Server{api}
+ 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)
+ }
+
+ h(w, req) // call original
+ log.Info("served response", "ruid", req.ruid, "code", w.statusCode)
+ })
}
+// browser API for registering bzz url scheme handlers:
+// https://developer.mozilla.org/en/docs/Web-based_protocol_handlers
+// electron (chromium) api for registering bzz url scheme handlers:
+// https://github.com/atom/electron/blob/master/docs/api/protocol.md
+
+// browser API for registering bzz url scheme handlers:
+// https://developer.mozilla.org/en/docs/Web-based_protocol_handlers
+// electron (chromium) api for registering bzz url scheme handlers:
+// https://github.com/atom/electron/blob/master/docs/api/protocol.md
+
type Server struct {
+ http.Handler
api *api.API
}
@@ -121,7 +258,7 @@ type Request struct {
// HandlePostRaw handles a POST request to a raw bzz-raw:/ URI, stores the request
// body in swarm and returns the resulting storage address as a text/plain response
-func (s *Server) HandlePostRaw(ctx context.Context, w http.ResponseWriter, r *Request) {
+func (s *Server) HandlePostRaw(w http.ResponseWriter, r *Request) {
log.Debug("handle.post.raw", "ruid", r.ruid)
postRawCount.Inc(1)
@@ -148,7 +285,8 @@ func (s *Server) HandlePostRaw(ctx context.Context, w http.ResponseWriter, r *Re
Respond(w, r, "missing Content-Length header in request", http.StatusBadRequest)
return
}
- addr, _, err := s.api.Store(ctx, r.Body, r.ContentLength, toEncrypt)
+
+ addr, _, err := s.api.Store(r.Context(), r.Body, r.ContentLength, toEncrypt)
if err != nil {
postRawFail.Inc(1)
Respond(w, r, err.Error(), http.StatusInternalServerError)
@@ -167,7 +305,7 @@ func (s *Server) HandlePostRaw(ctx context.Context, w http.ResponseWriter, r *Re
// (either a tar archive or multipart form), adds those files either to an
// existing manifest or to a new manifest under <path> and returns the
// resulting manifest hash as a text/plain response
-func (s *Server) HandlePostFiles(ctx context.Context, w http.ResponseWriter, r *Request) {
+func (s *Server) HandlePostFiles(w http.ResponseWriter, r *Request) {
log.Debug("handle.post.files", "ruid", r.ruid)
postFilesCount.Inc(1)
@@ -185,7 +323,7 @@ func (s *Server) HandlePostFiles(ctx context.Context, w http.ResponseWriter, r *
var addr storage.Address
if r.uri.Addr != "" && r.uri.Addr != "encrypt" {
- addr, err = s.api.Resolve(ctx, r.uri)
+ addr, err = s.api.Resolve(r.Context(), r.uri)
if err != nil {
postFilesFail.Inc(1)
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.uri.Addr, err), http.StatusInternalServerError)
@@ -193,7 +331,7 @@ func (s *Server) HandlePostFiles(ctx context.Context, w http.ResponseWriter, r *
}
log.Debug("resolved key", "ruid", r.ruid, "key", addr)
} else {
- addr, err = s.api.NewManifest(ctx, toEncrypt)
+ addr, err = s.api.NewManifest(r.Context(), toEncrypt)
if err != nil {
postFilesFail.Inc(1)
Respond(w, r, err.Error(), http.StatusInternalServerError)
@@ -202,17 +340,21 @@ func (s *Server) HandlePostFiles(ctx context.Context, w http.ResponseWriter, r *
log.Debug("new manifest", "ruid", r.ruid, "key", addr)
}
- newAddr, err := s.updateManifest(ctx, addr, func(mw *api.ManifestWriter) error {
+ newAddr, err := s.api.UpdateManifest(r.Context(), addr, func(mw *api.ManifestWriter) error {
switch contentType {
case "application/x-tar":
- return s.handleTarUpload(ctx, r, mw)
-
+ _, err := s.handleTarUpload(r, mw)
+ if err != nil {
+ Respond(w, r, fmt.Sprintf("error uploading tarball: %v", err), http.StatusInternalServerError)
+ return err
+ }
+ return nil
case "multipart/form-data":
- return s.handleMultipartUpload(ctx, r, params["boundary"], mw)
+ return s.handleMultipartUpload(r, params["boundary"], mw)
default:
- return s.handleDirectUpload(ctx, r, mw)
+ return s.handleDirectUpload(r, mw)
}
})
if err != nil {
@@ -228,41 +370,17 @@ func (s *Server) HandlePostFiles(ctx context.Context, w http.ResponseWriter, r *
fmt.Fprint(w, newAddr)
}
-func (s *Server) handleTarUpload(ctx context.Context, req *Request, mw *api.ManifestWriter) error {
- log.Debug("handle.tar.upload", "ruid", req.ruid)
- tr := tar.NewReader(req.Body)
- for {
- hdr, err := tr.Next()
- if err == io.EOF {
- return nil
- } else if err != nil {
- return fmt.Errorf("error reading tar stream: %s", err)
- }
-
- // only store regular files
- if !hdr.FileInfo().Mode().IsRegular() {
- continue
- }
+func (s *Server) handleTarUpload(r *Request, mw *api.ManifestWriter) (storage.Address, error) {
+ log.Debug("handle.tar.upload", "ruid", r.ruid)
- // add the entry under the path from the request
- path := path.Join(req.uri.Path, hdr.Name)
- entry := &api.ManifestEntry{
- Path: path,
- ContentType: hdr.Xattrs["user.swarm.content-type"],
- Mode: hdr.Mode,
- Size: hdr.Size,
- ModTime: hdr.ModTime,
- }
- log.Debug("adding path to new manifest", "ruid", req.ruid, "bytes", entry.Size, "path", entry.Path)
- contentKey, err := mw.AddEntry(ctx, tr, entry)
- if err != nil {
- return fmt.Errorf("error adding manifest entry from tar stream: %s", err)
- }
- log.Debug("stored content", "ruid", req.ruid, "key", contentKey)
+ key, err := s.api.UploadTar(r.Context(), r.Body, r.uri.Path, mw)
+ if err != nil {
+ return nil, err
}
+ return key, nil
}
-func (s *Server) handleMultipartUpload(ctx context.Context, req *Request, boundary string, mw *api.ManifestWriter) error {
+func (s *Server) handleMultipartUpload(req *Request, boundary string, mw *api.ManifestWriter) error {
log.Debug("handle.multipart.upload", "ruid", req.ruid)
mr := multipart.NewReader(req.Body, boundary)
for {
@@ -312,7 +430,7 @@ func (s *Server) handleMultipartUpload(ctx context.Context, req *Request, bounda
ModTime: time.Now(),
}
log.Debug("adding path to new manifest", "ruid", req.ruid, "bytes", entry.Size, "path", entry.Path)
- contentKey, err := mw.AddEntry(ctx, reader, entry)
+ contentKey, err := mw.AddEntry(req.Context(), reader, entry)
if err != nil {
return fmt.Errorf("error adding manifest entry from multipart form: %s", err)
}
@@ -320,9 +438,9 @@ func (s *Server) handleMultipartUpload(ctx context.Context, req *Request, bounda
}
}
-func (s *Server) handleDirectUpload(ctx context.Context, req *Request, mw *api.ManifestWriter) error {
+func (s *Server) handleDirectUpload(req *Request, mw *api.ManifestWriter) error {
log.Debug("handle.direct.upload", "ruid", req.ruid)
- key, err := mw.AddEntry(ctx, req.Body, &api.ManifestEntry{
+ key, err := mw.AddEntry(req.Context(), req.Body, &api.ManifestEntry{
Path: req.uri.Path,
ContentType: req.Header.Get("Content-Type"),
Mode: 0644,
@@ -339,24 +457,13 @@ func (s *Server) handleDirectUpload(ctx context.Context, req *Request, mw *api.M
// HandleDelete handles a DELETE request to bzz:/<manifest>/<path>, removes
// <path> from <manifest> and returns the resulting manifest hash as a
// text/plain response
-func (s *Server) HandleDelete(ctx context.Context, w http.ResponseWriter, r *Request) {
+func (s *Server) HandleDelete(w http.ResponseWriter, r *Request) {
log.Debug("handle.delete", "ruid", r.ruid)
-
deleteCount.Inc(1)
- key, err := s.api.Resolve(ctx, r.uri)
- if err != nil {
- deleteFail.Inc(1)
- Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.uri.Addr, err), http.StatusInternalServerError)
- return
- }
-
- newKey, err := s.updateManifest(ctx, key, func(mw *api.ManifestWriter) error {
- log.Debug(fmt.Sprintf("removing %s from manifest %s", r.uri.Path, key.Log()), "ruid", r.ruid)
- return mw.RemoveEntry(r.uri.Path)
- })
+ newKey, err := s.api.Delete(r.Context(), r.uri.Addr, r.uri.Path)
if err != nil {
deleteFail.Inc(1)
- Respond(w, r, fmt.Sprintf("cannot update manifest: %s", err), http.StatusInternalServerError)
+ Respond(w, r, fmt.Sprintf("could not delete from manifest: %v", err), http.StatusInternalServerError)
return
}
@@ -400,7 +507,7 @@ func resourcePostMode(path string) (isRaw bool, frequency uint64, err error) {
// The resource name will be verbatim what is passed as the address part of the url.
// For example, if a POST is made to /bzz-resource:/foo.eth/raw/13 a new resource with frequency 13
// and name "foo.eth" will be created
-func (s *Server) HandlePostResource(ctx context.Context, w http.ResponseWriter, r *Request) {
+func (s *Server) HandlePostResource(w http.ResponseWriter, r *Request) {
log.Debug("handle.post.resource", "ruid", r.ruid)
var err error
var addr storage.Address
@@ -429,7 +536,7 @@ func (s *Server) HandlePostResource(ctx context.Context, w http.ResponseWriter,
// we create a manifest so we can retrieve the resource with bzz:// later
// this manifest has a special "resource type" manifest, and its hash is the key of the mutable resource
// root chunk
- m, err := s.api.NewResourceManifest(ctx, addr.Hex())
+ m, err := s.api.NewResourceManifest(r.Context(), addr.Hex())
if err != nil {
Respond(w, r, fmt.Sprintf("failed to create resource manifest: %v", err), http.StatusInternalServerError)
return
@@ -449,7 +556,7 @@ func (s *Server) HandlePostResource(ctx context.Context, w http.ResponseWriter,
// that means that we retrieve the manifest and inspect its Hash member.
manifestAddr := r.uri.Address()
if manifestAddr == nil {
- manifestAddr, err = s.api.Resolve(ctx, r.uri)
+ manifestAddr, err = s.api.Resolve(r.Context(), r.uri)
if err != nil {
getFail.Inc(1)
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.uri.Addr, err), http.StatusNotFound)
@@ -460,7 +567,7 @@ func (s *Server) HandlePostResource(ctx context.Context, w http.ResponseWriter,
}
// get the root chunk key from the manifest
- addr, err = s.api.ResolveResourceManifest(ctx, manifestAddr)
+ addr, err = s.api.ResolveResourceManifest(r.Context(), manifestAddr)
if err != nil {
getFail.Inc(1)
Respond(w, r, fmt.Sprintf("error resolving resource root chunk for %s: %s", r.uri.Addr, err), http.StatusNotFound)
@@ -519,19 +626,15 @@ func (s *Server) HandlePostResource(ctx context.Context, w http.ResponseWriter,
// bzz-resource://<id>/<n> - get latest update on period n
// bzz-resource://<id>/<n>/<m> - get update version m of period n
// <id> = ens name or hash
-func (s *Server) HandleGetResource(ctx context.Context, w http.ResponseWriter, r *Request) {
- s.handleGetResource(ctx, w, r)
-}
-
// TODO: Enable pass maxPeriod parameter
-func (s *Server) handleGetResource(ctx context.Context, w http.ResponseWriter, r *Request) {
+func (s *Server) HandleGetResource(w http.ResponseWriter, r *Request) {
log.Debug("handle.get.resource", "ruid", r.ruid)
var err error
// resolve the content key.
manifestAddr := r.uri.Address()
if manifestAddr == nil {
- manifestAddr, err = s.api.Resolve(ctx, r.uri)
+ manifestAddr, err = s.api.Resolve(r.Context(), r.uri)
if err != nil {
getFail.Inc(1)
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.uri.Addr, err), http.StatusNotFound)
@@ -542,7 +645,7 @@ func (s *Server) handleGetResource(ctx context.Context, w http.ResponseWriter, r
}
// get the root chunk key from the manifest
- key, err := s.api.ResolveResourceManifest(ctx, manifestAddr)
+ key, err := s.api.ResolveResourceManifest(r.Context(), manifestAddr)
if err != nil {
getFail.Inc(1)
Respond(w, r, fmt.Sprintf("error resolving resource root chunk for %s: %s", r.uri.Addr, err), http.StatusNotFound)
@@ -624,13 +727,13 @@ func (s *Server) translateResourceError(w http.ResponseWriter, r *Request, supEr
// 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(ctx context.Context, w http.ResponseWriter, r *Request) {
+func (s *Server) HandleGet(w http.ResponseWriter, r *Request) {
log.Debug("handle.get", "ruid", r.ruid, "uri", r.uri)
getCount.Inc(1)
var err error
addr := r.uri.Address()
if addr == nil {
- addr, err = s.api.Resolve(ctx, r.uri)
+ addr, err = s.api.Resolve(r.Context(), r.uri)
if err != nil {
getFail.Inc(1)
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.uri.Addr, err), http.StatusNotFound)
@@ -645,7 +748,7 @@ func (s *Server) HandleGet(ctx context.Context, w http.ResponseWriter, r *Reques
// if path is set, interpret <key> as a manifest and return the
// raw entry at the given path
if r.uri.Path != "" {
- walker, err := s.api.NewManifestWalker(ctx, addr, nil)
+ walker, err := s.api.NewManifestWalker(r.Context(), addr, nil)
if err != nil {
getFail.Inc(1)
Respond(w, r, fmt.Sprintf("%s is not a manifest", addr), http.StatusBadRequest)
@@ -693,7 +796,7 @@ func (s *Server) HandleGet(ctx context.Context, w http.ResponseWriter, r *Reques
}
// check the root chunk exists by retrieving the file's size
- reader, isEncrypted := s.api.Retrieve(ctx, addr)
+ reader, isEncrypted := s.api.Retrieve(r.Context(), addr)
if _, err := reader.Size(nil); err != nil {
getFail.Inc(1)
Respond(w, r, fmt.Sprintf("root chunk not found %s: %s", addr, err), http.StatusNotFound)
@@ -719,86 +822,10 @@ func (s *Server) HandleGet(ctx context.Context, w http.ResponseWriter, r *Reques
}
}
-// HandleGetFiles handles a GET request to bzz:/<manifest> with an Accept
-// header of "application/x-tar" and returns a tar stream of all files
-// contained in the manifest
-func (s *Server) HandleGetFiles(ctx context.Context, w http.ResponseWriter, r *Request) {
- log.Debug("handle.get.files", "ruid", r.ruid, "uri", r.uri)
- getFilesCount.Inc(1)
- if r.uri.Path != "" {
- getFilesFail.Inc(1)
- Respond(w, r, "files request cannot contain a path", http.StatusBadRequest)
- return
- }
-
- addr, err := s.api.Resolve(ctx, r.uri)
- if err != nil {
- getFilesFail.Inc(1)
- Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.uri.Addr, err), http.StatusNotFound)
- return
- }
- log.Debug("handle.get.files: resolved", "ruid", r.ruid, "key", addr)
-
- walker, err := s.api.NewManifestWalker(ctx, addr, nil)
- if err != nil {
- getFilesFail.Inc(1)
- Respond(w, r, err.Error(), http.StatusInternalServerError)
- return
- }
-
- tw := tar.NewWriter(w)
- defer tw.Close()
- w.Header().Set("Content-Type", "application/x-tar")
- w.WriteHeader(http.StatusOK)
-
- err = walker.Walk(func(entry *api.ManifestEntry) error {
- // ignore manifests (walk will recurse into them)
- if entry.ContentType == api.ManifestType {
- return nil
- }
-
- // retrieve the entry's key and size
- reader, isEncrypted := s.api.Retrieve(ctx, storage.Address(common.Hex2Bytes(entry.Hash)))
- size, err := reader.Size(nil)
- if err != nil {
- return err
- }
- w.Header().Set("X-Decrypted", fmt.Sprintf("%v", isEncrypted))
-
- // write a tar header for the entry
- hdr := &tar.Header{
- Name: entry.Path,
- Mode: entry.Mode,
- Size: size,
- ModTime: entry.ModTime,
- Xattrs: map[string]string{
- "user.swarm.content-type": entry.ContentType,
- },
- }
- if err := tw.WriteHeader(hdr); err != nil {
- return err
- }
-
- // copy the file into the tar stream
- n, err := io.Copy(tw, io.LimitReader(reader, hdr.Size))
- if err != nil {
- return err
- } else if n != size {
- return fmt.Errorf("error writing %s: expected %d bytes but sent %d", entry.Path, size, n)
- }
-
- return nil
- })
- if err != nil {
- getFilesFail.Inc(1)
- log.Error(fmt.Sprintf("error generating tar stream: %s", err))
- }
-}
-
// HandleGetList handles a GET request to bzz-list:/<manifest>/<path> and returns
// a list of all files contained in <manifest> under <path> grouped into
// common prefixes using "/" as a delimiter
-func (s *Server) HandleGetList(ctx context.Context, w http.ResponseWriter, r *Request) {
+func (s *Server) HandleGetList(w http.ResponseWriter, r *Request) {
log.Debug("handle.get.list", "ruid", r.ruid, "uri", r.uri)
getListCount.Inc(1)
// ensure the root path has a trailing slash so that relative URLs work
@@ -807,7 +834,7 @@ func (s *Server) HandleGetList(ctx context.Context, w http.ResponseWriter, r *Re
return
}
- addr, err := s.api.Resolve(ctx, r.uri)
+ addr, err := s.api.Resolve(r.Context(), r.uri)
if err != nil {
getListFail.Inc(1)
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.uri.Addr, err), http.StatusNotFound)
@@ -815,8 +842,7 @@ func (s *Server) HandleGetList(ctx context.Context, w http.ResponseWriter, r *Re
}
log.Debug("handle.get.list: resolved", "ruid", r.ruid, "key", addr)
- list, err := s.getManifestList(ctx, addr, r.uri.Path)
-
+ list, err := s.api.GetManifestList(r.Context(), addr, r.uri.Path)
if err != nil {
getListFail.Inc(1)
Respond(w, r, err.Error(), http.StatusInternalServerError)
@@ -846,65 +872,9 @@ func (s *Server) HandleGetList(ctx context.Context, w http.ResponseWriter, r *Re
json.NewEncoder(w).Encode(&list)
}
-func (s *Server) getManifestList(ctx context.Context, addr storage.Address, prefix string) (list api.ManifestList, err error) {
- walker, err := s.api.NewManifestWalker(ctx, addr, nil)
- if err != nil {
- return
- }
-
- err = walker.Walk(func(entry *api.ManifestEntry) error {
- // handle non-manifest files
- if entry.ContentType != api.ManifestType {
- // ignore the file if it doesn't have the specified prefix
- if !strings.HasPrefix(entry.Path, prefix) {
- return nil
- }
-
- // if the path after the prefix contains a slash, add a
- // common prefix to the list, otherwise add the entry
- suffix := strings.TrimPrefix(entry.Path, prefix)
- if index := strings.Index(suffix, "/"); index > -1 {
- list.CommonPrefixes = append(list.CommonPrefixes, prefix+suffix[:index+1])
- return nil
- }
- if entry.Path == "" {
- entry.Path = "/"
- }
- list.Entries = append(list.Entries, entry)
- return nil
- }
-
- // if the manifest's path is a prefix of the specified prefix
- // then just recurse into the manifest by returning nil and
- // continuing the walk
- if strings.HasPrefix(prefix, entry.Path) {
- return nil
- }
-
- // if the manifest's path has the specified prefix, then if the
- // path after the prefix contains a slash, add a common prefix
- // to the list and skip the manifest, otherwise recurse into
- // the manifest by returning nil and continuing the walk
- if strings.HasPrefix(entry.Path, prefix) {
- suffix := strings.TrimPrefix(entry.Path, prefix)
- if index := strings.Index(suffix, "/"); index > -1 {
- list.CommonPrefixes = append(list.CommonPrefixes, prefix+suffix[:index+1])
- return api.ErrSkipManifest
- }
- return nil
- }
-
- // the manifest neither has the prefix or needs recursing in to
- // so just skip it
- return api.ErrSkipManifest
- })
-
- return list, nil
-}
-
// HandleGetFile handles a GET request to bzz://<manifest>/<path> and responds
// with the content of the file at <path> from the given <manifest>
-func (s *Server) HandleGetFile(ctx context.Context, w http.ResponseWriter, r *Request) {
+func (s *Server) HandleGetFile(w http.ResponseWriter, r *Request) {
log.Debug("handle.get.file", "ruid", r.ruid)
getFileCount.Inc(1)
// ensure the root path has a trailing slash so that relative URLs work
@@ -916,7 +886,7 @@ func (s *Server) HandleGetFile(ctx context.Context, w http.ResponseWriter, r *Re
manifestAddr := r.uri.Address()
if manifestAddr == nil {
- manifestAddr, err = s.api.Resolve(ctx, r.uri)
+ manifestAddr, err = s.api.Resolve(r.Context(), r.uri)
if err != nil {
getFileFail.Inc(1)
Respond(w, r, fmt.Sprintf("cannot resolve %s: %s", r.uri.Addr, err), http.StatusNotFound)
@@ -927,8 +897,7 @@ func (s *Server) HandleGetFile(ctx context.Context, w http.ResponseWriter, r *Re
}
log.Debug("handle.get.file: resolved", "ruid", r.ruid, "key", manifestAddr)
-
- reader, contentType, status, contentKey, err := s.api.Get(ctx, manifestAddr, r.uri.Path)
+ reader, contentType, status, contentKey, err := s.api.Get(r.Context(), manifestAddr, r.uri.Path)
etag := common.Bytes2Hex(contentKey)
noneMatchEtag := r.Header.Get("If-None-Match")
@@ -955,8 +924,7 @@ func (s *Server) HandleGetFile(ctx context.Context, w http.ResponseWriter, r *Re
//the request results in ambiguous files
//e.g. /read with readme.md and readinglist.txt available in manifest
if status == http.StatusMultipleChoices {
- list, err := s.getManifestList(ctx, manifestAddr, r.uri.Path)
-
+ list, err := s.api.GetManifestList(r.Context(), manifestAddr, r.uri.Path)
if err != nil {
getFileFail.Inc(1)
Respond(w, r, err.Error(), http.StatusInternalServerError)
@@ -1011,125 +979,6 @@ func (b bufferedReadSeeker) Seek(offset int64, whence int) (int64, error) {
return b.s.Seek(offset, whence)
}
-func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
- ctx := context.TODO()
-
- defer metrics.GetOrRegisterResettingTimer(fmt.Sprintf("http.request.%s.time", r.Method), nil).UpdateSince(time.Now())
- req := &Request{Request: *r, ruid: uuid.New()[:8]}
- metrics.GetOrRegisterCounter(fmt.Sprintf("http.request.%s", r.Method), nil).Inc(1)
- log.Info("serving request", "ruid", req.ruid, "method", r.Method, "url", r.RequestURI)
-
- // wrapping the ResponseWriter, so that we get the response code set by http.ServeContent
- w := newLoggingResponseWriter(rw)
-
- if r.RequestURI == "/" && strings.Contains(r.Header.Get("Accept"), "text/html") {
-
- err := landingPageTemplate.Execute(w, nil)
- if err != nil {
- log.Error(fmt.Sprintf("error rendering landing page: %s", err))
- }
- return
- }
-
- if r.URL.Path == "/robots.txt" {
- w.Header().Set("Last-Modified", time.Now().Format(http.TimeFormat))
- fmt.Fprintf(w, "User-agent: *\nDisallow: /")
- return
- }
-
- if r.RequestURI == "/" && strings.Contains(r.Header.Get("Accept"), "application/json") {
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
- json.NewEncoder(w).Encode("Welcome to Swarm!")
- return
- }
-
- uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))
- if err != nil {
- Respond(w, req, fmt.Sprintf("invalid URI %q", r.URL.Path), http.StatusBadRequest)
- return
- }
-
- 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)
-
- switch r.Method {
- case "POST":
- if uri.Raw() {
- log.Debug("handlePostRaw")
- s.HandlePostRaw(ctx, w, req)
- } else if uri.Resource() {
- log.Debug("handlePostResource")
- s.HandlePostResource(ctx, w, req)
- } else if uri.Immutable() || uri.List() || uri.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)
- } else {
- log.Debug("handlePostFiles")
- s.HandlePostFiles(ctx, w, req)
- }
-
- case "PUT":
- Respond(w, req, fmt.Sprintf("PUT method to %s not allowed", uri), http.StatusBadRequest)
- return
-
- case "DELETE":
- if uri.Raw() {
- Respond(w, req, fmt.Sprintf("DELETE method to %s not allowed", uri), http.StatusBadRequest)
- return
- }
- s.HandleDelete(ctx, w, req)
-
- case "GET":
-
- if uri.Resource() {
- s.HandleGetResource(ctx, w, req)
- return
- }
-
- if uri.Raw() || uri.Hash() {
- s.HandleGet(ctx, w, req)
- return
- }
-
- if uri.List() {
- s.HandleGetList(ctx, w, req)
- return
- }
-
- if r.Header.Get("Accept") == "application/x-tar" {
- s.HandleGetFiles(ctx, w, req)
- return
- }
-
- s.HandleGetFile(ctx, w, req)
-
- default:
- Respond(w, req, fmt.Sprintf("%s method is not supported", r.Method), http.StatusMethodNotAllowed)
- }
-
- log.Info("served response", "ruid", req.ruid, "code", w.statusCode)
-}
-
-func (s *Server) updateManifest(ctx context.Context, addr storage.Address, update func(mw *api.ManifestWriter) error) (storage.Address, error) {
- mw, err := s.api.NewManifestWriter(ctx, addr, nil)
- if err != nil {
- return nil, err
- }
-
- if err := update(mw); err != nil {
- return nil, err
- }
-
- addr, err = mw.Store()
- if err != nil {
- return nil, err
- }
- log.Debug(fmt.Sprintf("generated manifest %s", addr))
- return addr, nil
-}
-
type loggingResponseWriter struct {
http.ResponseWriter
statusCode int