response.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package http
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "html/template"
  21. "net/http"
  22. "strings"
  23. "time"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/metrics"
  26. "github.com/ethereum/go-ethereum/swarm/api"
  27. )
  28. var (
  29. htmlCounter = metrics.NewRegisteredCounter("api.http.errorpage.html.count", nil)
  30. jsonCounter = metrics.NewRegisteredCounter("api.http.errorpage.json.count", nil)
  31. plaintextCounter = metrics.NewRegisteredCounter("api.http.errorpage.plaintext.count", nil)
  32. )
  33. type ResponseParams struct {
  34. Msg template.HTML
  35. Code int
  36. Timestamp string
  37. template *template.Template
  38. Details template.HTML
  39. }
  40. // ShowMultipleChoices is used when a user requests a resource in a manifest which results
  41. // in ambiguous results. It returns a HTML page with clickable links of each of the entry
  42. // in the manifest which fits the request URI ambiguity.
  43. // For example, if the user requests bzz:/<hash>/read and that manifest contains entries
  44. // "readme.md" and "readinglist.txt", a HTML page is returned with this two links.
  45. // This only applies if the manifest has no default entry
  46. func ShowMultipleChoices(w http.ResponseWriter, r *http.Request, list api.ManifestList) {
  47. log.Debug("ShowMultipleChoices", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
  48. msg := ""
  49. if list.Entries == nil {
  50. respondError(w, r, "Could not resolve", http.StatusInternalServerError)
  51. return
  52. }
  53. requestUri := strings.TrimPrefix(r.RequestURI, "/")
  54. uri, err := api.Parse(requestUri)
  55. if err != nil {
  56. respondError(w, r, "Bad Request", http.StatusBadRequest)
  57. }
  58. uri.Scheme = "bzz-list"
  59. 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())
  60. respondTemplate(w, r, "error", msg, http.StatusMultipleChoices)
  61. }
  62. func respondTemplate(w http.ResponseWriter, r *http.Request, templateName, msg string, code int) {
  63. log.Debug("respondTemplate", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
  64. respond(w, r, &ResponseParams{
  65. Code: code,
  66. Msg: template.HTML(msg),
  67. Timestamp: time.Now().Format(time.RFC1123),
  68. template: TemplatesMap[templateName],
  69. })
  70. }
  71. func respondError(w http.ResponseWriter, r *http.Request, msg string, code int) {
  72. log.Info("respondError", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()), "code", code, "msg", msg)
  73. respondTemplate(w, r, "error", msg, code)
  74. }
  75. func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
  76. w.WriteHeader(params.Code)
  77. if params.Code >= 400 {
  78. w.Header().Del("Cache-Control")
  79. w.Header().Del("ETag")
  80. }
  81. acceptHeader := r.Header.Get("Accept")
  82. // 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"
  83. if strings.Contains(acceptHeader, "application/json") {
  84. if err := respondJSON(w, r, params); err != nil {
  85. respondError(w, r, "Internal server error", http.StatusInternalServerError)
  86. }
  87. } else if strings.Contains(acceptHeader, "text/html") {
  88. respondHTML(w, r, params)
  89. } else {
  90. respondPlaintext(w, r, params) //returns nice errors for curl
  91. }
  92. }
  93. func respondHTML(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
  94. htmlCounter.Inc(1)
  95. log.Info("respondHTML", "ruid", GetRUID(r.Context()), "code", params.Code)
  96. err := params.template.Execute(w, params)
  97. if err != nil {
  98. log.Error(err.Error())
  99. }
  100. }
  101. func respondJSON(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
  102. jsonCounter.Inc(1)
  103. log.Info("respondJSON", "ruid", GetRUID(r.Context()), "code", params.Code)
  104. w.Header().Set("Content-Type", "application/json")
  105. return json.NewEncoder(w).Encode(params)
  106. }
  107. func respondPlaintext(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
  108. plaintextCounter.Inc(1)
  109. log.Info("respondPlaintext", "ruid", GetRUID(r.Context()), "code", params.Code)
  110. w.Header().Set("Content-Type", "text/plain")
  111. strToWrite := "Code: " + fmt.Sprintf("%d", params.Code) + "\n"
  112. strToWrite += "Message: " + string(params.Msg) + "\n"
  113. strToWrite += "Timestamp: " + params.Timestamp + "\n"
  114. _, err := w.Write([]byte(strToWrite))
  115. return err
  116. }