| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | // Copyright 2016 The go-ethereum Authors | 
					
						
							|  |  |  | // This file is part of the go-ethereum library. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // 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-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-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | A simple http server interface to Swarm | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | package http | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	"archive/tar" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"mime" | 
					
						
							|  |  |  | 	"mime/multipart" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"path" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2017-01-05 11:57:41 +01:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/log" | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/metrics" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/swarm/api" | 
					
						
							| 
									
										
										
										
											2017-01-26 20:33:39 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/swarm/storage" | 
					
						
							| 
									
										
										
										
											2017-01-05 11:57:41 +01:00
										 |  |  | 	"github.com/rs/cors" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | //setup metrics | 
					
						
							|  |  |  | var ( | 
					
						
							|  |  |  | 	postRawCount     = metrics.NewRegisteredCounter("api.http.post.raw.count", nil) | 
					
						
							|  |  |  | 	postRawFail      = metrics.NewRegisteredCounter("api.http.post.raw.fail", nil) | 
					
						
							|  |  |  | 	postFilesCount   = metrics.NewRegisteredCounter("api.http.post.files.count", nil) | 
					
						
							|  |  |  | 	postFilesFail    = metrics.NewRegisteredCounter("api.http.post.files.fail", nil) | 
					
						
							|  |  |  | 	deleteCount      = metrics.NewRegisteredCounter("api.http.delete.count", nil) | 
					
						
							|  |  |  | 	deleteFail       = metrics.NewRegisteredCounter("api.http.delete.fail", nil) | 
					
						
							|  |  |  | 	getCount         = metrics.NewRegisteredCounter("api.http.get.count", nil) | 
					
						
							|  |  |  | 	getFail          = metrics.NewRegisteredCounter("api.http.get.fail", nil) | 
					
						
							|  |  |  | 	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) | 
					
						
							|  |  |  | 	requestCount     = metrics.NewRegisteredCounter("http.request.count", nil) | 
					
						
							|  |  |  | 	htmlRequestCount = metrics.NewRegisteredCounter("http.request.html.count", nil) | 
					
						
							|  |  |  | 	jsonRequestCount = metrics.NewRegisteredCounter("http.request.json.count", nil) | 
					
						
							|  |  |  | 	requestTimer     = metrics.NewRegisteredResettingTimer("http.request.time", nil) | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | // ServerConfig is the basic configuration needed for the HTTP server and also | 
					
						
							| 
									
										
										
										
											2017-01-05 11:57:41 +01:00
										 |  |  | // includes CORS settings. | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | type ServerConfig struct { | 
					
						
							| 
									
										
										
										
											2017-01-05 11:57:41 +01:00
										 |  |  | 	Addr       string | 
					
						
							|  |  |  | 	CorsString string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | // 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 | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | func StartHttpServer(api *api.Api, config *ServerConfig) { | 
					
						
							| 
									
										
										
										
											2017-01-05 11:57:41 +01:00
										 |  |  | 	var allowedOrigins []string | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	for _, domain := range strings.Split(config.CorsString, ",") { | 
					
						
							| 
									
										
										
										
											2017-01-05 11:57:41 +01:00
										 |  |  | 		allowedOrigins = append(allowedOrigins, strings.TrimSpace(domain)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	c := cors.New(cors.Options{ | 
					
						
							|  |  |  | 		AllowedOrigins: allowedOrigins, | 
					
						
							|  |  |  | 		AllowedMethods: []string{"POST", "GET", "DELETE", "PATCH", "PUT"}, | 
					
						
							|  |  |  | 		MaxAge:         600, | 
					
						
							| 
									
										
										
										
											2017-03-15 20:34:08 -07:00
										 |  |  | 		AllowedHeaders: []string{"*"}, | 
					
						
							| 
									
										
										
										
											2017-01-05 11:57:41 +01:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	hdlr := c.Handler(NewServer(api)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go http.ListenAndServe(config.Addr, hdlr) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-05 11:57:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | func NewServer(api *api.Api) *Server { | 
					
						
							|  |  |  | 	return &Server{api} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Server struct { | 
					
						
							|  |  |  | 	api *api.Api | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | // Request wraps http.Request and also includes the parsed bzz URI | 
					
						
							|  |  |  | type Request struct { | 
					
						
							|  |  |  | 	http.Request | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uri *api.URI | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 09:49:30 +01:00
										 |  |  | // HandlePostRaw handles a POST request to a raw bzz-raw:/ URI, stores the request | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | // body in swarm and returns the resulting storage key as a text/plain response | 
					
						
							|  |  |  | func (s *Server) HandlePostRaw(w http.ResponseWriter, r *Request) { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 	postRawCount.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	if r.uri.Path != "" { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		postRawFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.BadRequest(w, r, "raw POST request cannot contain a path") | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if r.Header.Get("Content-Length") == "" { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		postRawFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.BadRequest(w, r, "missing Content-Length header in request") | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	key, err := s.api.Store(r.Body, r.ContentLength, nil) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		postRawFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.Error(w, r, err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	s.logDebug("content for %s stored", key.Log()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	w.Header().Set("Content-Type", "text/plain") | 
					
						
							|  |  |  | 	w.WriteHeader(http.StatusOK) | 
					
						
							|  |  |  | 	fmt.Fprint(w, key) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // HandlePostFiles handles a POST request (or deprecated PUT request) to | 
					
						
							|  |  |  | // bzz:/<hash>/<path> which contains either a single file or multiple files | 
					
						
							|  |  |  | // (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(w http.ResponseWriter, r *Request) { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 	postFilesCount.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	contentType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		postFilesFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.BadRequest(w, r, err.Error()) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	var key storage.Key | 
					
						
							|  |  |  | 	if r.uri.Addr != "" { | 
					
						
							|  |  |  | 		key, err = s.api.Resolve(r.uri) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 			postFilesFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			s.Error(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err)) | 
					
						
							| 
									
										
										
										
											2016-11-10 21:14:17 +01:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		key, err = s.api.NewManifest() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 			postFilesFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			s.Error(w, r, err) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newKey, err := s.updateManifest(key, func(mw *api.ManifestWriter) error { | 
					
						
							|  |  |  | 		switch contentType { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case "application/x-tar": | 
					
						
							|  |  |  | 			return s.handleTarUpload(r, mw) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case "multipart/form-data": | 
					
						
							|  |  |  | 			return s.handleMultipartUpload(r, params["boundary"], mw) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return s.handleDirectUpload(r, mw) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		postFilesFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.Error(w, r, fmt.Errorf("error creating manifest: %s", err)) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	w.Header().Set("Content-Type", "text/plain") | 
					
						
							|  |  |  | 	w.WriteHeader(http.StatusOK) | 
					
						
							|  |  |  | 	fmt.Fprint(w, newKey) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) handleTarUpload(req *Request, mw *api.ManifestWriter) error { | 
					
						
							|  |  |  | 	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 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// 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, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		s.logDebug("adding %s (%d bytes) to new manifest", entry.Path, entry.Size) | 
					
						
							|  |  |  | 		contentKey, err := mw.AddEntry(tr, entry) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("error adding manifest entry from tar stream: %s", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		s.logDebug("content for %s stored", contentKey.Log()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) handleMultipartUpload(req *Request, boundary string, mw *api.ManifestWriter) error { | 
					
						
							|  |  |  | 	mr := multipart.NewReader(req.Body, boundary) | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		part, err := mr.NextPart() | 
					
						
							|  |  |  | 		if err == io.EOF { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} else if err != nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("error reading multipart form: %s", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var size int64 | 
					
						
							|  |  |  | 		var reader io.Reader = part | 
					
						
							|  |  |  | 		if contentLength := part.Header.Get("Content-Length"); contentLength != "" { | 
					
						
							|  |  |  | 			size, err = strconv.ParseInt(contentLength, 10, 64) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return fmt.Errorf("error parsing multipart content length: %s", err) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			reader = part | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			// copy the part to a tmp file to get its size | 
					
						
							|  |  |  | 			tmp, err := ioutil.TempFile("", "swarm-multipart") | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			defer os.Remove(tmp.Name()) | 
					
						
							|  |  |  | 			defer tmp.Close() | 
					
						
							|  |  |  | 			size, err = io.Copy(tmp, part) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return fmt.Errorf("error copying multipart content: %s", err) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-08-08 20:34:35 +03:00
										 |  |  | 			if _, err := tmp.Seek(0, io.SeekStart); err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 				return fmt.Errorf("error copying multipart content: %s", err) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			reader = tmp | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// add the entry under the path from the request | 
					
						
							|  |  |  | 		name := part.FileName() | 
					
						
							|  |  |  | 		if name == "" { | 
					
						
							|  |  |  | 			name = part.FormName() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		path := path.Join(req.uri.Path, name) | 
					
						
							|  |  |  | 		entry := &api.ManifestEntry{ | 
					
						
							|  |  |  | 			Path:        path, | 
					
						
							|  |  |  | 			ContentType: part.Header.Get("Content-Type"), | 
					
						
							|  |  |  | 			Size:        size, | 
					
						
							|  |  |  | 			ModTime:     time.Now(), | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.logDebug("adding %s (%d bytes) to new manifest", entry.Path, entry.Size) | 
					
						
							|  |  |  | 		contentKey, err := mw.AddEntry(reader, entry) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("error adding manifest entry from multipart form: %s", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		s.logDebug("content for %s stored", contentKey.Log()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) handleDirectUpload(req *Request, mw *api.ManifestWriter) error { | 
					
						
							|  |  |  | 	key, err := mw.AddEntry(req.Body, &api.ManifestEntry{ | 
					
						
							|  |  |  | 		Path:        req.uri.Path, | 
					
						
							|  |  |  | 		ContentType: req.Header.Get("Content-Type"), | 
					
						
							|  |  |  | 		Mode:        0644, | 
					
						
							|  |  |  | 		Size:        req.ContentLength, | 
					
						
							|  |  |  | 		ModTime:     time.Now(), | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	s.logDebug("content for %s stored", key.Log()) | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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(w http.ResponseWriter, r *Request) { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 	deleteCount.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	key, err := s.api.Resolve(r.uri) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		deleteFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.Error(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err)) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newKey, err := s.updateManifest(key, func(mw *api.ManifestWriter) error { | 
					
						
							|  |  |  | 		s.logDebug("removing %s from manifest %s", r.uri.Path, key.Log()) | 
					
						
							|  |  |  | 		return mw.RemoveEntry(r.uri.Path) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		deleteFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.Error(w, r, fmt.Errorf("error updating manifest: %s", err)) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	w.Header().Set("Content-Type", "text/plain") | 
					
						
							|  |  |  | 	w.WriteHeader(http.StatusOK) | 
					
						
							|  |  |  | 	fmt.Fprint(w, newKey) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-21 13:47:10 +01:00
										 |  |  | // 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) { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 	getCount.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	key, err := s.api.Resolve(r.uri) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 		s.NotFound(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err)) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 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(key, nil) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 			getFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			s.BadRequest(w, r, fmt.Sprintf("%s is not a manifest", key)) | 
					
						
							| 
									
										
										
										
											2017-01-26 20:33:39 +01:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		var entry *api.ManifestEntry | 
					
						
							|  |  |  | 		walker.Walk(func(e *api.ManifestEntry) error { | 
					
						
							|  |  |  | 			// if the entry matches the path, set entry and stop | 
					
						
							|  |  |  | 			// the walk | 
					
						
							|  |  |  | 			if e.Path == r.uri.Path { | 
					
						
							|  |  |  | 				entry = e | 
					
						
							|  |  |  | 				// return an error to cancel the walk | 
					
						
							|  |  |  | 				return errors.New("found") | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			// ignore non-manifest files | 
					
						
							|  |  |  | 			if e.ContentType != api.ManifestType { | 
					
						
							|  |  |  | 				return nil | 
					
						
							| 
									
										
										
										
											2016-12-22 00:35:05 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			// if the manifest's path is a prefix of the | 
					
						
							|  |  |  | 			// requested path, recurse into it by returning | 
					
						
							|  |  |  | 			// nil and continuing the walk | 
					
						
							|  |  |  | 			if strings.HasPrefix(r.uri.Path, e.Path) { | 
					
						
							|  |  |  | 				return nil | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			return api.SkipManifest | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		if entry == nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 			getFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | 			s.NotFound(w, r, fmt.Errorf("Manifest entry could not be loaded")) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		key = storage.Key(common.Hex2Bytes(entry.Hash)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	// check the root chunk exists by retrieving the file's size | 
					
						
							|  |  |  | 	reader := s.api.Retrieve(key) | 
					
						
							|  |  |  | 	if _, err := reader.Size(nil); err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | 		s.NotFound(w, r, fmt.Errorf("Root chunk not found %s: %s", key, err)) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-21 13:47:10 +01:00
										 |  |  | 	switch { | 
					
						
							| 
									
										
										
										
											2018-02-23 13:09:01 +00:00
										 |  |  | 	case r.uri.Raw() || r.uri.DeprecatedRaw(): | 
					
						
							| 
									
										
										
										
											2017-12-21 13:47:10 +01:00
										 |  |  | 		// 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) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-21 13:47:10 +01:00
										 |  |  | 		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) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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(w http.ResponseWriter, r *Request) { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 	getFilesCount.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	if r.uri.Path != "" { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getFilesFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.BadRequest(w, r, "files request cannot contain a path") | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	key, err := s.api.Resolve(r.uri) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getFilesFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 		s.NotFound(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err)) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	walker, err := s.api.NewManifestWalker(key, nil) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getFilesFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.Error(w, r, err) | 
					
						
							|  |  |  | 		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 := s.api.Retrieve(storage.Key(common.Hex2Bytes(entry.Hash))) | 
					
						
							|  |  |  | 		size, err := reader.Size(nil) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// 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 { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getFilesFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.logError("error generating tar stream: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 09:49:30 +01:00
										 |  |  | // 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 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | func (s *Server) HandleGetList(w http.ResponseWriter, r *Request) { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 	getListCount.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	// ensure the root path has a trailing slash so that relative URLs work | 
					
						
							|  |  |  | 	if r.uri.Path == "" && !strings.HasSuffix(r.URL.Path, "/") { | 
					
						
							| 
									
										
										
										
											2017-12-19 09:49:30 +01:00
										 |  |  | 		http.Redirect(w, &r.Request, r.URL.Path+"/", http.StatusMovedPermanently) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	key, err := s.api.Resolve(r.uri) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getListFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 		s.NotFound(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err)) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 08:45:54 -05:00
										 |  |  | 	list, err := s.getManifestList(key, r.uri.Path) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getListFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		s.Error(w, r, err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 08:45:54 -05:00
										 |  |  | 	// if the client wants HTML (e.g. a browser) then render the list as a | 
					
						
							|  |  |  | 	// HTML index with relative URLs | 
					
						
							|  |  |  | 	if strings.Contains(r.Header.Get("Accept"), "text/html") { | 
					
						
							|  |  |  | 		w.Header().Set("Content-Type", "text/html") | 
					
						
							|  |  |  | 		err := htmlListTemplate.Execute(w, &htmlListData{ | 
					
						
							| 
									
										
										
										
											2017-12-19 09:49:30 +01:00
										 |  |  | 			URI: &api.URI{ | 
					
						
							|  |  |  | 				Scheme: "bzz", | 
					
						
							|  |  |  | 				Addr:   r.uri.Addr, | 
					
						
							|  |  |  | 				Path:   r.uri.Path, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-10-06 08:45:54 -05:00
										 |  |  | 			List: &list, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 			getListFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-10-06 08:45:54 -05:00
										 |  |  | 			s.logError("error rendering list HTML: %s", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	w.Header().Set("Content-Type", "application/json") | 
					
						
							|  |  |  | 	json.NewEncoder(w).Encode(&list) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) getManifestList(key storage.Key, prefix string) (list api.ManifestList, err error) { | 
					
						
							|  |  |  | 	walker, err := s.api.NewManifestWalker(key, nil) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	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 | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// 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 | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			if entry.Path == "" { | 
					
						
							|  |  |  | 				entry.Path = "/" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			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.SkipManifest | 
					
						
							| 
									
										
										
										
											2016-12-22 00:35:05 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		// the manifest neither has the prefix or needs recursing in to | 
					
						
							|  |  |  | 		// so just skip it | 
					
						
							|  |  |  | 		return api.SkipManifest | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 08:45:54 -05:00
										 |  |  | 	return list, nil | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | // 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(w http.ResponseWriter, r *Request) { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 	getFileCount.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-07-25 10:51:26 +01:00
										 |  |  | 	// ensure the root path has a trailing slash so that relative URLs work | 
					
						
							|  |  |  | 	if r.uri.Path == "" && !strings.HasSuffix(r.URL.Path, "/") { | 
					
						
							|  |  |  | 		http.Redirect(w, &r.Request, r.URL.Path+"/", http.StatusMovedPermanently) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	key, err := s.api.Resolve(r.uri) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getFileFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 		s.NotFound(w, r, fmt.Errorf("error resolving %s: %s", r.uri.Addr, err)) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | 	reader, contentType, status, err := s.api.Get(key, r.uri.Path) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | 		switch status { | 
					
						
							|  |  |  | 		case http.StatusNotFound: | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 			getFileNotFound.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | 			s.NotFound(w, r, err) | 
					
						
							|  |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 			getFileFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | 			s.Error(w, r, err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 08:45:54 -05:00
										 |  |  | 	//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(key, r.uri.Path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 			getFileFail.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-10-06 08:45:54 -05:00
										 |  |  | 			s.Error(w, r, err) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		s.logDebug(fmt.Sprintf("Multiple choices! -->  %v", list)) | 
					
						
							|  |  |  | 		//show a nice page links to available entries | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 		ShowMultipleChoices(w, r, list) | 
					
						
							| 
									
										
										
										
											2017-10-06 08:45:54 -05:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	// check the root chunk exists by retrieving the file's size | 
					
						
							|  |  |  | 	if _, err := reader.Size(nil); err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 		getFileNotFound.Inc(1) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | 		s.NotFound(w, r, fmt.Errorf("File not found %s: %s", r.uri, err)) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	w.Header().Set("Content-Type", contentType) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	http.ServeContent(w, &r.Request, "", time.Now(), reader) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 	if metrics.Enabled { | 
					
						
							|  |  |  | 		//The increment for request count and request timer themselves have a flag check | 
					
						
							|  |  |  | 		//for metrics.Enabled. Nevertheless, we introduce the if here because we | 
					
						
							|  |  |  | 		//are looking into the header just to see what request type it is (json/html). | 
					
						
							|  |  |  | 		//So let's take advantage and add all metrics related stuff here | 
					
						
							|  |  |  | 		requestCount.Inc(1) | 
					
						
							|  |  |  | 		defer requestTimer.UpdateSince(time.Now()) | 
					
						
							|  |  |  | 		if r.Header.Get("Accept") == "application/json" { | 
					
						
							|  |  |  | 			jsonRequestCount.Inc(1) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			htmlRequestCount.Inc(1) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	s.logDebug("HTTP %s request URL: '%s', Host: '%s', Path: '%s', Referer: '%s', Accept: '%s'", r.Method, r.RequestURI, r.URL.Host, r.URL.Path, r.Referer(), r.Header.Get("Accept")) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 02:56:40 -06:00
										 |  |  | 	if r.RequestURI == "/" && strings.Contains(r.Header.Get("Accept"), "text/html") { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		err := landingPageTemplate.Execute(w, nil) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			s.logError("error rendering landing page: %s", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/")) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | 	req := &Request{Request: *r, uri: uri} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		s.logError("Invalid URI %q: %s", r.URL.Path, err) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | 		s.BadRequest(w, req, fmt.Sprintf("Invalid URI %q: %s", r.URL.Path, err)) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	s.logDebug("%s request received for %s", r.Method, uri) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch r.Method { | 
					
						
							|  |  |  | 	case "POST": | 
					
						
							| 
									
										
										
										
											2017-12-19 09:49:30 +01:00
										 |  |  | 		if uri.Raw() || uri.DeprecatedRaw() { | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			s.HandlePostRaw(w, req) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			s.HandlePostFiles(w, req) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case "PUT": | 
					
						
							|  |  |  | 		// DEPRECATED: | 
					
						
							|  |  |  | 		//   clients should send a POST request (the request creates a | 
					
						
							|  |  |  | 		//   new manifest leaving the existing one intact, so it isn't | 
					
						
							|  |  |  | 		//   strictly a traditional PUT request which replaces content | 
					
						
							|  |  |  | 		//   at a URI, and POST is more ubiquitous) | 
					
						
							| 
									
										
										
										
											2017-12-19 09:49:30 +01:00
										 |  |  | 		if uri.Raw() || uri.DeprecatedRaw() { | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 			ShowError(w, req, fmt.Sprintf("No PUT to %s allowed.", uri), http.StatusBadRequest) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			s.HandlePostFiles(w, req) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	case "DELETE": | 
					
						
							| 
									
										
										
										
											2017-12-19 09:49:30 +01:00
										 |  |  | 		if uri.Raw() || uri.DeprecatedRaw() { | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 			ShowError(w, req, fmt.Sprintf("No DELETE to %s allowed.", uri), http.StatusBadRequest) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		s.HandleDelete(w, req) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case "GET": | 
					
						
							| 
									
										
										
										
											2017-12-21 13:47:10 +01:00
										 |  |  | 		if uri.Raw() || uri.Hash() || uri.DeprecatedRaw() { | 
					
						
							|  |  |  | 			s.HandleGet(w, req) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 09:49:30 +01:00
										 |  |  | 		if uri.List() { | 
					
						
							|  |  |  | 			s.HandleGetList(w, req) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 09:49:30 +01:00
										 |  |  | 		if r.Header.Get("Accept") == "application/x-tar" { | 
					
						
							|  |  |  | 			s.HandleGetFiles(w, req) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		s.HandleGetFile(w, req) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 		ShowError(w, req, fmt.Sprintf("Method "+r.Method+" is not supported.", uri), http.StatusMethodNotAllowed) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) updateManifest(key storage.Key, update func(mw *api.ManifestWriter) error) (storage.Key, error) { | 
					
						
							|  |  |  | 	mw, err := s.api.NewManifestWriter(key, nil) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := update(mw); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	key, err = mw.Store() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	s.logDebug("generated manifest %s", key) | 
					
						
							|  |  |  | 	return key, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) logDebug(format string, v ...interface{}) { | 
					
						
							|  |  |  | 	log.Debug(fmt.Sprintf("[BZZ] HTTP: "+format, v...)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) logError(format string, v ...interface{}) { | 
					
						
							|  |  |  | 	log.Error(fmt.Sprintf("[BZZ] HTTP: "+format, v...)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) BadRequest(w http.ResponseWriter, r *Request, reason string) { | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 	ShowError(w, r, fmt.Sprintf("Bad request %s %s: %s", r.Request.Method, r.uri, reason), http.StatusBadRequest) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) Error(w http.ResponseWriter, r *Request, err error) { | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 	ShowError(w, r, fmt.Sprintf("Error serving %s %s: %s", r.Request.Method, r.uri, err), http.StatusInternalServerError) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:29:09 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *Server) NotFound(w http.ResponseWriter, r *Request, err error) { | 
					
						
							| 
									
										
										
										
											2018-02-27 14:32:38 +01:00
										 |  |  | 	ShowError(w, r, fmt.Sprintf("NOT FOUND error serving %s %s: %s", r.Request.Method, r.uri, err), http.StatusNotFound) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } |