forked from cerc-io/plugeth
swarm/api: unexport Respond methods (#18037)
This commit is contained in:
parent
baee850471
commit
53eb4e0b0f
@ -50,7 +50,7 @@ func ParseURI(h http.Handler) http.Handler {
|
|||||||
uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))
|
uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
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
|
return
|
||||||
}
|
}
|
||||||
if uri.Addr != "" && strings.HasPrefix(uri.Addr, "0x") {
|
if uri.Addr != "" && strings.HasPrefix(uri.Addr, "0x") {
|
||||||
|
@ -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()))
|
log.Debug("ShowMultipleChoices", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
|
||||||
msg := ""
|
msg := ""
|
||||||
if list.Entries == nil {
|
if list.Entries == nil {
|
||||||
RespondError(w, r, "Could not resolve", http.StatusInternalServerError)
|
respondError(w, r, "Could not resolve", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
requestUri := strings.TrimPrefix(r.RequestURI, "/")
|
requestUri := strings.TrimPrefix(r.RequestURI, "/")
|
||||||
|
|
||||||
uri, err := api.Parse(requestUri)
|
uri, err := api.Parse(requestUri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespondError(w, r, "Bad Request", http.StatusBadRequest)
|
respondError(w, r, "Bad Request", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
uri.Scheme = "bzz-list"
|
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())
|
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) {
|
func respondTemplate(w http.ResponseWriter, r *http.Request, templateName, msg string, code int) {
|
||||||
log.Debug("RespondTemplate", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
|
log.Debug("respondTemplate", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
|
||||||
respond(w, r, &ResponseParams{
|
respond(w, r, &ResponseParams{
|
||||||
Code: code,
|
Code: code,
|
||||||
Msg: template.HTML(msg),
|
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) {
|
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)
|
log.Info("respondError", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()), "code", code)
|
||||||
RespondTemplate(w, r, "error", msg, code)
|
respondTemplate(w, r, "error", msg, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
|
func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
|
||||||
|
|
||||||
w.WriteHeader(params.Code)
|
w.WriteHeader(params.Code)
|
||||||
|
|
||||||
if params.Code >= 400 {
|
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"
|
// 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 strings.Contains(acceptHeader, "application/json") {
|
||||||
if err := respondJSON(w, r, params); err != nil {
|
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") {
|
} else if strings.Contains(acceptHeader, "text/html") {
|
||||||
respondHTML(w, r, params)
|
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) {
|
func respondHTML(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
|
||||||
htmlCounter.Inc(1)
|
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)
|
err := params.template.Execute(w, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
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 {
|
func respondJSON(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
|
||||||
jsonCounter.Inc(1)
|
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")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
return json.NewEncoder(w).Encode(params)
|
return json.NewEncoder(w).Encode(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
func respondPlaintext(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
|
func respondPlaintext(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
|
||||||
plaintextCounter.Inc(1)
|
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")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
strToWrite := "Code: " + fmt.Sprintf("%d", params.Code) + "\n"
|
strToWrite := "Code: " + fmt.Sprintf("%d", params.Code) + "\n"
|
||||||
strToWrite += "Message: " + string(params.Msg) + "\n"
|
strToWrite += "Message: " + string(params.Msg) + "\n"
|
||||||
|
@ -184,10 +184,10 @@ func (s *Server) HandleBzzGet(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if isDecryptError(err) {
|
if isDecryptError(err) {
|
||||||
w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", uri.Address().String()))
|
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
|
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
|
return
|
||||||
}
|
}
|
||||||
defer reader.Close()
|
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) {
|
func (s *Server) HandleRootPaths(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.RequestURI {
|
switch r.RequestURI {
|
||||||
case "/":
|
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
|
return
|
||||||
case "/robots.txt":
|
case "/robots.txt":
|
||||||
w.Header().Set("Last-Modified", time.Now().Format(http.TimeFormat))
|
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.WriteHeader(http.StatusOK)
|
||||||
w.Write(faviconBytes)
|
w.Write(faviconBytes)
|
||||||
default:
|
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 != "" {
|
if uri.Path != "" {
|
||||||
postRawFail.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if uri.Addr != "" && uri.Addr != "encrypt" {
|
if uri.Addr != "" && uri.Addr != "encrypt" {
|
||||||
postRawFail.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Header.Get("Content-Length") == "" {
|
if r.Header.Get("Content-Length") == "" {
|
||||||
postRawFail.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, _, err := s.api.Store(r.Context(), r.Body, r.ContentLength, toEncrypt)
|
addr, _, err := s.api.Store(r.Context(), r.Body, r.ContentLength, toEncrypt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postRawFail.Inc(1)
|
postRawFail.Inc(1)
|
||||||
RespondError(w, r, err.Error(), http.StatusInternalServerError)
|
respondError(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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"))
|
contentType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postFilesFail.Inc(1)
|
postFilesFail.Inc(1)
|
||||||
RespondError(w, r, err.Error(), http.StatusBadRequest)
|
respondError(w, r, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,7 +298,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *http.Request) {
|
|||||||
addr, err = s.api.Resolve(r.Context(), uri.Addr)
|
addr, err = s.api.Resolve(r.Context(), uri.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postFilesFail.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
log.Debug("resolved key", "ruid", ruid, "key", addr)
|
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)
|
addr, err = s.api.NewManifest(r.Context(), toEncrypt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postFilesFail.Inc(1)
|
postFilesFail.Inc(1)
|
||||||
RespondError(w, r, err.Error(), http.StatusInternalServerError)
|
respondError(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("new manifest", "ruid", ruid, "key", addr)
|
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":
|
case "application/x-tar":
|
||||||
_, err := s.handleTarUpload(r, mw)
|
_, err := s.handleTarUpload(r, mw)
|
||||||
if err != nil {
|
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 err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -330,7 +330,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *http.Request) {
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postFilesFail.Inc(1)
|
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
|
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)
|
newKey, err := s.api.Delete(r.Context(), uri.Addr, uri.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
deleteFail.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,7 +460,7 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Creation and update must send feed.updateRequestJSON JSON structure
|
// Creation and update must send feed.updateRequestJSON JSON structure
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespondError(w, r, err.Error(), http.StatusInternalServerError)
|
respondError(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -471,7 +471,7 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err == api.ErrCannotLoadFeedManifest || err == api.ErrCannotResolveFeedURI {
|
if err == api.ErrCannotLoadFeedManifest || err == api.ErrCannotResolveFeedURI {
|
||||||
httpStatus = http.StatusNotFound
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,7 +480,7 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
|
|||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
|
|
||||||
if err := updateRequest.FromValues(query, body); err != nil { // decodes request from query parameters
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -489,12 +489,12 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
|
|||||||
// to update this feed
|
// to update this feed
|
||||||
// Check this early, to avoid creating a feed and then not being able to set its first update.
|
// Check this early, to avoid creating a feed and then not being able to set its first update.
|
||||||
if err = updateRequest.Verify(); err != nil {
|
if err = updateRequest.Verify(); err != nil {
|
||||||
RespondError(w, r, err.Error(), http.StatusForbidden)
|
respondError(w, r, err.Error(), http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = s.api.FeedsUpdate(r.Context(), &updateRequest)
|
_, err = s.api.FeedsUpdate(r.Context(), &updateRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespondError(w, r, err.Error(), http.StatusInternalServerError)
|
respondError(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -505,7 +505,7 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
|
|||||||
// feed identification used to retrieve feed updates later
|
// feed identification used to retrieve feed updates later
|
||||||
m, err := s.api.NewFeedManifest(r.Context(), &updateRequest.Feed)
|
m, err := s.api.NewFeedManifest(r.Context(), &updateRequest.Feed)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
// the key to the manifest will be passed back to the client
|
// 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
|
// the manifest key can be set as content in the resolver of the ENS name
|
||||||
outdata, err := json.Marshal(m)
|
outdata, err := json.Marshal(m)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
fmt.Fprint(w, string(outdata))
|
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 {
|
if err == api.ErrCannotLoadFeedManifest || err == api.ErrCannotResolveFeedURI {
|
||||||
httpStatus = http.StatusNotFound
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -559,12 +559,12 @@ func (s *Server) HandleGetFeed(w http.ResponseWriter, r *http.Request) {
|
|||||||
unsignedUpdateRequest, err := s.api.FeedsNewRequest(r.Context(), fd)
|
unsignedUpdateRequest, err := s.api.FeedsNewRequest(r.Context(), fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFail.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
rawResponse, err := unsignedUpdateRequest.MarshalJSON()
|
rawResponse, err := unsignedUpdateRequest.MarshalJSON()
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
w.Header().Add("Content-type", "application/json")
|
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}
|
lookupParams := &feed.Query{Feed: *fd}
|
||||||
if err = lookupParams.FromValues(r.URL.Query()); err != nil { // parse period, version
|
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
|
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
|
// any error from the switch statement will end up here
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code, err2 := s.translateFeedError(w, r, "feed lookup fail", err)
|
code, err2 := s.translateFeedError(w, r, "feed lookup fail", err)
|
||||||
RespondError(w, r, err2.Error(), code)
|
respondError(w, r, err2.Error(), code)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -630,7 +630,7 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *http.Request) {
|
|||||||
addr, err := s.api.ResolveURI(r.Context(), uri, pass)
|
addr, err := s.api.ResolveURI(r.Context(), uri, pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFail.Inc(1)
|
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
|
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.
|
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)
|
reader, isEncrypted := s.api.Retrieve(r.Context(), addr)
|
||||||
if _, err := reader.Size(r.Context(), nil); err != nil {
|
if _, err := reader.Size(r.Context(), nil); err != nil {
|
||||||
getFail.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -694,7 +694,7 @@ func (s *Server) HandleGetList(w http.ResponseWriter, r *http.Request) {
|
|||||||
addr, err := s.api.Resolve(r.Context(), uri.Addr)
|
addr, err := s.api.Resolve(r.Context(), uri.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getListFail.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
log.Debug("handle.get.list: resolved", "ruid", ruid, "key", addr)
|
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)
|
getListFail.Inc(1)
|
||||||
if isDecryptError(err) {
|
if isDecryptError(err) {
|
||||||
w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", addr.String()))
|
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
|
return
|
||||||
}
|
}
|
||||||
RespondError(w, r, err.Error(), http.StatusInternalServerError)
|
respondError(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -755,7 +755,7 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *http.Request) {
|
|||||||
manifestAddr, err = s.api.Resolve(r.Context(), uri.Addr)
|
manifestAddr, err = s.api.Resolve(r.Context(), uri.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
getFileFail.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -779,17 +779,17 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if isDecryptError(err) {
|
if isDecryptError(err) {
|
||||||
w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", manifestAddr))
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch status {
|
switch status {
|
||||||
case http.StatusNotFound:
|
case http.StatusNotFound:
|
||||||
getFileNotFound.Inc(1)
|
getFileNotFound.Inc(1)
|
||||||
RespondError(w, r, err.Error(), http.StatusNotFound)
|
respondError(w, r, err.Error(), http.StatusNotFound)
|
||||||
default:
|
default:
|
||||||
getFileFail.Inc(1)
|
getFileFail.Inc(1)
|
||||||
RespondError(w, r, err.Error(), http.StatusInternalServerError)
|
respondError(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -802,10 +802,10 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *http.Request) {
|
|||||||
getFileFail.Inc(1)
|
getFileFail.Inc(1)
|
||||||
if isDecryptError(err) {
|
if isDecryptError(err) {
|
||||||
w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", manifestAddr))
|
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
|
return
|
||||||
}
|
}
|
||||||
RespondError(w, r, err.Error(), http.StatusInternalServerError)
|
respondError(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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
|
// check the root chunk exists by retrieving the file's size
|
||||||
if _, err := reader.Size(r.Context(), nil); err != nil {
|
if _, err := reader.Size(r.Context(), nil); err != nil {
|
||||||
getFileNotFound.Inc(1)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user