From 53eb4e0b0fffdc105fbe9f5eed671b96de6e2ba1 Mon Sep 17 00:00:00 2001
From: Anton Evangelatov <anton.evangelatov@gmail.com>
Date: Tue, 6 Nov 2018 12:34:34 +0100
Subject: swarm/api: unexport Respond methods (#18037)

---
 swarm/api/http/middleware.go |  2 +-
 swarm/api/http/response.go   | 25 +++++++--------
 swarm/api/http/server.go     | 76 ++++++++++++++++++++++----------------------
 3 files changed, 51 insertions(+), 52 deletions(-)

(limited to 'swarm')

diff --git a/swarm/api/http/middleware.go b/swarm/api/http/middleware.go
index 3b2dcc7d5..ccc040c54 100644
--- a/swarm/api/http/middleware.go
+++ b/swarm/api/http/middleware.go
@@ -50,7 +50,7 @@ func ParseURI(h http.Handler) http.Handler {
 		uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))
 		if err != nil {
 			w.WriteHeader(http.StatusBadRequest)
-			RespondError(w, r, fmt.Sprintf("invalid URI %q", r.URL.Path), http.StatusBadRequest)
+			respondError(w, r, fmt.Sprintf("invalid URI %q", r.URL.Path), http.StatusBadRequest)
 			return
 		}
 		if uri.Addr != "" && strings.HasPrefix(uri.Addr, "0x") {
diff --git a/swarm/api/http/response.go b/swarm/api/http/response.go
index c9fb9d285..d4e81d7f6 100644
--- a/swarm/api/http/response.go
+++ b/swarm/api/http/response.go
@@ -53,23 +53,23 @@ func ShowMultipleChoices(w http.ResponseWriter, r *http.Request, list api.Manife
 	log.Debug("ShowMultipleChoices", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
 	msg := ""
 	if list.Entries == nil {
-		RespondError(w, r, "Could not resolve", http.StatusInternalServerError)
+		respondError(w, r, "Could not resolve", http.StatusInternalServerError)
 		return
 	}
 	requestUri := strings.TrimPrefix(r.RequestURI, "/")
 
 	uri, err := api.Parse(requestUri)
 	if err != nil {
-		RespondError(w, r, "Bad Request", http.StatusBadRequest)
+		respondError(w, r, "Bad Request", http.StatusBadRequest)
 	}
 
 	uri.Scheme = "bzz-list"
 	msg += fmt.Sprintf("Disambiguation:<br/>Your request may refer to multiple choices.<br/>Click <a class=\"orange\" href='"+"/"+uri.String()+"'>here</a> if your browser does not redirect you within 5 seconds.<script>setTimeout(\"location.href='%s';\",5000);</script><br/>", "/"+uri.String())
-	RespondTemplate(w, r, "error", msg, http.StatusMultipleChoices)
+	respondTemplate(w, r, "error", msg, http.StatusMultipleChoices)
 }
 
-func RespondTemplate(w http.ResponseWriter, r *http.Request, templateName, msg string, code int) {
-	log.Debug("RespondTemplate", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
+func respondTemplate(w http.ResponseWriter, r *http.Request, templateName, msg string, code int) {
+	log.Debug("respondTemplate", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
 	respond(w, r, &ResponseParams{
 		Code:      code,
 		Msg:       template.HTML(msg),
@@ -78,13 +78,12 @@ func RespondTemplate(w http.ResponseWriter, r *http.Request, templateName, msg s
 	})
 }
 
-func RespondError(w http.ResponseWriter, r *http.Request, msg string, code int) {
-	log.Debug("RespondError", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()), "code", code)
-	RespondTemplate(w, r, "error", msg, code)
+func respondError(w http.ResponseWriter, r *http.Request, msg string, code int) {
+	log.Info("respondError", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()), "code", code)
+	respondTemplate(w, r, "error", msg, code)
 }
 
 func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
-
 	w.WriteHeader(params.Code)
 
 	if params.Code >= 400 {
@@ -96,7 +95,7 @@ func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
 	// this cannot be in a switch since an Accept header can have multiple values: "Accept: */*, text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8"
 	if strings.Contains(acceptHeader, "application/json") {
 		if err := respondJSON(w, r, params); err != nil {
-			RespondError(w, r, "Internal server error", http.StatusInternalServerError)
+			respondError(w, r, "Internal server error", http.StatusInternalServerError)
 		}
 	} else if strings.Contains(acceptHeader, "text/html") {
 		respondHTML(w, r, params)
@@ -107,7 +106,7 @@ func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
 
 func respondHTML(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
 	htmlCounter.Inc(1)
-	log.Debug("respondHTML", "ruid", GetRUID(r.Context()))
+	log.Info("respondHTML", "ruid", GetRUID(r.Context()), "code", params.Code)
 	err := params.template.Execute(w, params)
 	if err != nil {
 		log.Error(err.Error())
@@ -116,14 +115,14 @@ func respondHTML(w http.ResponseWriter, r *http.Request, params *ResponseParams)
 
 func respondJSON(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
 	jsonCounter.Inc(1)
-	log.Debug("respondJSON", "ruid", GetRUID(r.Context()))
+	log.Info("respondJSON", "ruid", GetRUID(r.Context()), "code", params.Code)
 	w.Header().Set("Content-Type", "application/json")
 	return json.NewEncoder(w).Encode(params)
 }
 
 func respondPlaintext(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
 	plaintextCounter.Inc(1)
-	log.Debug("respondPlaintext", "ruid", GetRUID(r.Context()))
+	log.Info("respondPlaintext", "ruid", GetRUID(r.Context()), "code", params.Code)
 	w.Header().Set("Content-Type", "text/plain")
 	strToWrite := "Code: " + fmt.Sprintf("%d", params.Code) + "\n"
 	strToWrite += "Message: " + string(params.Msg) + "\n"
diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go
index de1eb70dc..e9005104e 100644
--- a/swarm/api/http/server.go
+++ b/swarm/api/http/server.go
@@ -184,10 +184,10 @@ func (s *Server) HandleBzzGet(w http.ResponseWriter, r *http.Request) {
 		if err != nil {
 			if isDecryptError(err) {
 				w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", uri.Address().String()))
-				RespondError(w, r, err.Error(), http.StatusUnauthorized)
+				respondError(w, r, err.Error(), http.StatusUnauthorized)
 				return
 			}
-			RespondError(w, r, fmt.Sprintf("Had an error building the tarball: %v", err), http.StatusInternalServerError)
+			respondError(w, r, fmt.Sprintf("Had an error building the tarball: %v", err), http.StatusInternalServerError)
 			return
 		}
 		defer reader.Close()
@@ -211,7 +211,7 @@ func (s *Server) HandleBzzGet(w http.ResponseWriter, r *http.Request) {
 func (s *Server) HandleRootPaths(w http.ResponseWriter, r *http.Request) {
 	switch r.RequestURI {
 	case "/":
-		RespondTemplate(w, r, "landing-page", "Swarm: Please request a valid ENS or swarm hash with the appropriate bzz scheme", 200)
+		respondTemplate(w, r, "landing-page", "Swarm: Please request a valid ENS or swarm hash with the appropriate bzz scheme", 200)
 		return
 	case "/robots.txt":
 		w.Header().Set("Last-Modified", time.Now().Format(http.TimeFormat))
@@ -220,7 +220,7 @@ func (s *Server) HandleRootPaths(w http.ResponseWriter, r *http.Request) {
 		w.WriteHeader(http.StatusOK)
 		w.Write(faviconBytes)
 	default:
-		RespondError(w, r, "Not Found", http.StatusNotFound)
+		respondError(w, r, "Not Found", http.StatusNotFound)
 	}
 }
 
@@ -240,26 +240,26 @@ func (s *Server) HandlePostRaw(w http.ResponseWriter, r *http.Request) {
 
 	if uri.Path != "" {
 		postRawFail.Inc(1)
-		RespondError(w, r, "raw POST request cannot contain a path", http.StatusBadRequest)
+		respondError(w, r, "raw POST request cannot contain a path", http.StatusBadRequest)
 		return
 	}
 
 	if uri.Addr != "" && uri.Addr != "encrypt" {
 		postRawFail.Inc(1)
-		RespondError(w, r, "raw POST request addr can only be empty or \"encrypt\"", http.StatusBadRequest)
+		respondError(w, r, "raw POST request addr can only be empty or \"encrypt\"", http.StatusBadRequest)
 		return
 	}
 
 	if r.Header.Get("Content-Length") == "" {
 		postRawFail.Inc(1)
-		RespondError(w, r, "missing Content-Length header in request", http.StatusBadRequest)
+		respondError(w, r, "missing Content-Length header in request", http.StatusBadRequest)
 		return
 	}
 
 	addr, _, err := s.api.Store(r.Context(), r.Body, r.ContentLength, toEncrypt)
 	if err != nil {
 		postRawFail.Inc(1)
-		RespondError(w, r, err.Error(), http.StatusInternalServerError)
+		respondError(w, r, err.Error(), http.StatusInternalServerError)
 		return
 	}
 
@@ -283,7 +283,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *http.Request) {
 	contentType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
 	if err != nil {
 		postFilesFail.Inc(1)
-		RespondError(w, r, err.Error(), http.StatusBadRequest)
+		respondError(w, r, err.Error(), http.StatusBadRequest)
 		return
 	}
 
@@ -298,7 +298,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *http.Request) {
 		addr, err = s.api.Resolve(r.Context(), uri.Addr)
 		if err != nil {
 			postFilesFail.Inc(1)
-			RespondError(w, r, fmt.Sprintf("cannot resolve %s: %s", uri.Addr, err), http.StatusInternalServerError)
+			respondError(w, r, fmt.Sprintf("cannot resolve %s: %s", uri.Addr, err), http.StatusInternalServerError)
 			return
 		}
 		log.Debug("resolved key", "ruid", ruid, "key", addr)
@@ -306,7 +306,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *http.Request) {
 		addr, err = s.api.NewManifest(r.Context(), toEncrypt)
 		if err != nil {
 			postFilesFail.Inc(1)
-			RespondError(w, r, err.Error(), http.StatusInternalServerError)
+			respondError(w, r, err.Error(), http.StatusInternalServerError)
 			return
 		}
 		log.Debug("new manifest", "ruid", ruid, "key", addr)
@@ -317,7 +317,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *http.Request) {
 		case "application/x-tar":
 			_, err := s.handleTarUpload(r, mw)
 			if err != nil {
-				RespondError(w, r, fmt.Sprintf("error uploading tarball: %v", err), http.StatusInternalServerError)
+				respondError(w, r, fmt.Sprintf("error uploading tarball: %v", err), http.StatusInternalServerError)
 				return err
 			}
 			return nil
@@ -330,7 +330,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *http.Request) {
 	})
 	if err != nil {
 		postFilesFail.Inc(1)
-		RespondError(w, r, fmt.Sprintf("cannot create manifest: %s", err), http.StatusInternalServerError)
+		respondError(w, r, fmt.Sprintf("cannot create manifest: %s", err), http.StatusInternalServerError)
 		return
 	}
 
@@ -439,7 +439,7 @@ func (s *Server) HandleDelete(w http.ResponseWriter, r *http.Request) {
 	newKey, err := s.api.Delete(r.Context(), uri.Addr, uri.Path)
 	if err != nil {
 		deleteFail.Inc(1)
-		RespondError(w, r, fmt.Sprintf("could not delete from manifest: %v", err), http.StatusInternalServerError)
+		respondError(w, r, fmt.Sprintf("could not delete from manifest: %v", err), http.StatusInternalServerError)
 		return
 	}
 
@@ -460,7 +460,7 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
 	// Creation and update must send feed.updateRequestJSON JSON structure
 	body, err := ioutil.ReadAll(r.Body)
 	if err != nil {
-		RespondError(w, r, err.Error(), http.StatusInternalServerError)
+		respondError(w, r, err.Error(), http.StatusInternalServerError)
 		return
 	}
 
@@ -471,7 +471,7 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
 		if err == api.ErrCannotLoadFeedManifest || err == api.ErrCannotResolveFeedURI {
 			httpStatus = http.StatusNotFound
 		}
-		RespondError(w, r, fmt.Sprintf("cannot retrieve feed from manifest: %s", err), httpStatus)
+		respondError(w, r, fmt.Sprintf("cannot retrieve feed from manifest: %s", err), httpStatus)
 		return
 	}
 
@@ -480,7 +480,7 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
 	query := r.URL.Query()
 
 	if err := updateRequest.FromValues(query, body); err != nil { // decodes request from query parameters
-		RespondError(w, r, err.Error(), http.StatusBadRequest)
+		respondError(w, r, err.Error(), http.StatusBadRequest)
 		return
 	}
 
@@ -489,12 +489,12 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
 		// to update this feed
 		// Check this early, to avoid creating a feed and then not being able to set its first update.
 		if err = updateRequest.Verify(); err != nil {
-			RespondError(w, r, err.Error(), http.StatusForbidden)
+			respondError(w, r, err.Error(), http.StatusForbidden)
 			return
 		}
 		_, err = s.api.FeedsUpdate(r.Context(), &updateRequest)
 		if err != nil {
-			RespondError(w, r, err.Error(), http.StatusInternalServerError)
+			respondError(w, r, err.Error(), http.StatusInternalServerError)
 			return
 		}
 	}
@@ -505,7 +505,7 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
 		// feed identification used to retrieve feed updates later
 		m, err := s.api.NewFeedManifest(r.Context(), &updateRequest.Feed)
 		if err != nil {
-			RespondError(w, r, fmt.Sprintf("failed to create feed manifest: %v", err), http.StatusInternalServerError)
+			respondError(w, r, fmt.Sprintf("failed to create feed manifest: %v", err), http.StatusInternalServerError)
 			return
 		}
 		// the key to the manifest will be passed back to the client
@@ -513,7 +513,7 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
 		// the manifest key can be set as content in the resolver of the ENS name
 		outdata, err := json.Marshal(m)
 		if err != nil {
-			RespondError(w, r, fmt.Sprintf("failed to create json response: %s", err), http.StatusInternalServerError)
+			respondError(w, r, fmt.Sprintf("failed to create json response: %s", err), http.StatusInternalServerError)
 			return
 		}
 		fmt.Fprint(w, string(outdata))
@@ -550,7 +550,7 @@ func (s *Server) HandleGetFeed(w http.ResponseWriter, r *http.Request) {
 		if err == api.ErrCannotLoadFeedManifest || err == api.ErrCannotResolveFeedURI {
 			httpStatus = http.StatusNotFound
 		}
-		RespondError(w, r, fmt.Sprintf("cannot retrieve feed information from manifest: %s", err), httpStatus)
+		respondError(w, r, fmt.Sprintf("cannot retrieve feed information from manifest: %s", err), httpStatus)
 		return
 	}
 
@@ -559,12 +559,12 @@ func (s *Server) HandleGetFeed(w http.ResponseWriter, r *http.Request) {
 		unsignedUpdateRequest, err := s.api.FeedsNewRequest(r.Context(), fd)
 		if err != nil {
 			getFail.Inc(1)
-			RespondError(w, r, fmt.Sprintf("cannot retrieve feed metadata for feed=%s: %s", fd.Hex(), err), http.StatusNotFound)
+			respondError(w, r, fmt.Sprintf("cannot retrieve feed metadata for feed=%s: %s", fd.Hex(), err), http.StatusNotFound)
 			return
 		}
 		rawResponse, err := unsignedUpdateRequest.MarshalJSON()
 		if err != nil {
-			RespondError(w, r, fmt.Sprintf("cannot encode unsigned feed update request: %v", err), http.StatusInternalServerError)
+			respondError(w, r, fmt.Sprintf("cannot encode unsigned feed update request: %v", err), http.StatusInternalServerError)
 			return
 		}
 		w.Header().Add("Content-type", "application/json")
@@ -575,7 +575,7 @@ func (s *Server) HandleGetFeed(w http.ResponseWriter, r *http.Request) {
 
 	lookupParams := &feed.Query{Feed: *fd}
 	if err = lookupParams.FromValues(r.URL.Query()); err != nil { // parse period, version
-		RespondError(w, r, fmt.Sprintf("invalid feed update request:%s", err), http.StatusBadRequest)
+		respondError(w, r, fmt.Sprintf("invalid feed update request:%s", err), http.StatusBadRequest)
 		return
 	}
 
@@ -584,7 +584,7 @@ func (s *Server) HandleGetFeed(w http.ResponseWriter, r *http.Request) {
 	// any error from the switch statement will end up here
 	if err != nil {
 		code, err2 := s.translateFeedError(w, r, "feed lookup fail", err)
-		RespondError(w, r, err2.Error(), code)
+		respondError(w, r, err2.Error(), code)
 		return
 	}
 
@@ -630,7 +630,7 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *http.Request) {
 	addr, err := s.api.ResolveURI(r.Context(), uri, pass)
 	if err != nil {
 		getFail.Inc(1)
-		RespondError(w, r, fmt.Sprintf("cannot resolve %s: %s", uri.Addr, err), http.StatusNotFound)
+		respondError(w, r, fmt.Sprintf("cannot resolve %s: %s", uri.Addr, err), http.StatusNotFound)
 		return
 	}
 	w.Header().Set("Cache-Control", "max-age=2147483648, immutable") // url was of type bzz://<hex key>/path, so we are sure it is immutable.
@@ -654,7 +654,7 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *http.Request) {
 	reader, isEncrypted := s.api.Retrieve(r.Context(), addr)
 	if _, err := reader.Size(r.Context(), nil); err != nil {
 		getFail.Inc(1)
-		RespondError(w, r, fmt.Sprintf("root chunk not found %s: %s", addr, err), http.StatusNotFound)
+		respondError(w, r, fmt.Sprintf("root chunk not found %s: %s", addr, err), http.StatusNotFound)
 		return
 	}
 
@@ -694,7 +694,7 @@ func (s *Server) HandleGetList(w http.ResponseWriter, r *http.Request) {
 	addr, err := s.api.Resolve(r.Context(), uri.Addr)
 	if err != nil {
 		getListFail.Inc(1)
-		RespondError(w, r, fmt.Sprintf("cannot resolve %s: %s", uri.Addr, err), http.StatusNotFound)
+		respondError(w, r, fmt.Sprintf("cannot resolve %s: %s", uri.Addr, err), http.StatusNotFound)
 		return
 	}
 	log.Debug("handle.get.list: resolved", "ruid", ruid, "key", addr)
@@ -704,10 +704,10 @@ func (s *Server) HandleGetList(w http.ResponseWriter, r *http.Request) {
 		getListFail.Inc(1)
 		if isDecryptError(err) {
 			w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", addr.String()))
-			RespondError(w, r, err.Error(), http.StatusUnauthorized)
+			respondError(w, r, err.Error(), http.StatusUnauthorized)
 			return
 		}
-		RespondError(w, r, err.Error(), http.StatusInternalServerError)
+		respondError(w, r, err.Error(), http.StatusInternalServerError)
 		return
 	}
 
@@ -755,7 +755,7 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *http.Request) {
 		manifestAddr, err = s.api.Resolve(r.Context(), uri.Addr)
 		if err != nil {
 			getFileFail.Inc(1)
-			RespondError(w, r, fmt.Sprintf("cannot resolve %s: %s", uri.Addr, err), http.StatusNotFound)
+			respondError(w, r, fmt.Sprintf("cannot resolve %s: %s", uri.Addr, err), http.StatusNotFound)
 			return
 		}
 	} else {
@@ -779,17 +779,17 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *http.Request) {
 	if err != nil {
 		if isDecryptError(err) {
 			w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", manifestAddr))
-			RespondError(w, r, err.Error(), http.StatusUnauthorized)
+			respondError(w, r, err.Error(), http.StatusUnauthorized)
 			return
 		}
 
 		switch status {
 		case http.StatusNotFound:
 			getFileNotFound.Inc(1)
-			RespondError(w, r, err.Error(), http.StatusNotFound)
+			respondError(w, r, err.Error(), http.StatusNotFound)
 		default:
 			getFileFail.Inc(1)
-			RespondError(w, r, err.Error(), http.StatusInternalServerError)
+			respondError(w, r, err.Error(), http.StatusInternalServerError)
 		}
 		return
 	}
@@ -802,10 +802,10 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *http.Request) {
 			getFileFail.Inc(1)
 			if isDecryptError(err) {
 				w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", manifestAddr))
-				RespondError(w, r, err.Error(), http.StatusUnauthorized)
+				respondError(w, r, err.Error(), http.StatusUnauthorized)
 				return
 			}
-			RespondError(w, r, err.Error(), http.StatusInternalServerError)
+			respondError(w, r, err.Error(), http.StatusInternalServerError)
 			return
 		}
 
@@ -818,7 +818,7 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *http.Request) {
 	// check the root chunk exists by retrieving the file's size
 	if _, err := reader.Size(r.Context(), nil); err != nil {
 		getFileNotFound.Inc(1)
-		RespondError(w, r, fmt.Sprintf("file not found %s: %s", uri, err), http.StatusNotFound)
+		respondError(w, r, fmt.Sprintf("file not found %s: %s", uri, err), http.StatusNotFound)
 		return
 	}
 
-- 
cgit v1.2.3