aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/http/response.go
diff options
context:
space:
mode:
authorElad <theman@elad.im>2018-08-07 17:56:55 +0800
committerBalint Gabor <balint.g@gmail.com>2018-08-07 17:56:55 +0800
commit93fe16b0a5162a158ee74d9f398107f31074a8f1 (patch)
tree5838389dd827c05e380c85a4e0dae04189bf07c6 /swarm/api/http/response.go
parent64a4e89504b683074077b54518633b76c59ab507 (diff)
downloadgo-tangerine-93fe16b0a5162a158ee74d9f398107f31074a8f1.tar
go-tangerine-93fe16b0a5162a158ee74d9f398107f31074a8f1.tar.gz
go-tangerine-93fe16b0a5162a158ee74d9f398107f31074a8f1.tar.bz2
go-tangerine-93fe16b0a5162a158ee74d9f398107f31074a8f1.tar.lz
go-tangerine-93fe16b0a5162a158ee74d9f398107f31074a8f1.tar.xz
go-tangerine-93fe16b0a5162a158ee74d9f398107f31074a8f1.tar.zst
go-tangerine-93fe16b0a5162a158ee74d9f398107f31074a8f1.zip
swarm/api/http: refactored http package (#17309)
Diffstat (limited to 'swarm/api/http/response.go')
-rw-r--r--swarm/api/http/response.go139
1 files changed, 139 insertions, 0 deletions
diff --git a/swarm/api/http/response.go b/swarm/api/http/response.go
new file mode 100644
index 000000000..32c09b1f5
--- /dev/null
+++ b/swarm/api/http/response.go
@@ -0,0 +1,139 @@
+// Copyright 2017 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/>.
+
+package http
+
+import (
+ "encoding/json"
+ "fmt"
+ "html/template"
+ "net/http"
+ "strings"
+ "time"
+
+ "github.com/ethereum/go-ethereum/log"
+ "github.com/ethereum/go-ethereum/metrics"
+ "github.com/ethereum/go-ethereum/swarm/api"
+)
+
+//metrics variables
+var (
+ htmlCounter = metrics.NewRegisteredCounter("api.http.errorpage.html.count", nil)
+ jsonCounter = metrics.NewRegisteredCounter("api.http.errorpage.json.count", nil)
+ plaintextCounter = metrics.NewRegisteredCounter("api.http.errorpage.plaintext.count", nil)
+)
+
+//parameters needed for formatting the correct HTML page
+type ResponseParams struct {
+ Msg template.HTML
+ Code int
+ Timestamp string
+ template *template.Template
+ Details template.HTML
+}
+
+//ShowMultipeChoices is used when a user requests a resource in a manifest which results
+//in ambiguous results. It returns a HTML page with clickable links of each of the entry
+//in the manifest which fits the request URI ambiguity.
+//For example, if the user requests bzz:/<hash>/read and that manifest contains entries
+//"readme.md" and "readinglist.txt", a HTML page is returned with this two links.
+//This only applies if the manifest has no default entry
+func ShowMultipleChoices(w http.ResponseWriter, r *http.Request, list api.ManifestList) {
+ log.Debug("ShowMultipleChoices", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
+ msg := ""
+ if list.Entries == nil {
+ 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)
+ }
+
+ uri.Scheme = "bzz-list"
+ //request the same url just with 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)
+}
+
+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),
+ Timestamp: time.Now().Format(time.RFC1123),
+ template: TemplatesMap[templateName],
+ })
+}
+
+func RespondError(w http.ResponseWriter, r *http.Request, msg string, code int) {
+ log.Debug("RespondError", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
+ RespondTemplate(w, r, "error", msg, code)
+}
+
+//evaluate if client accepts html or json response
+func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
+ w.WriteHeader(params.Code)
+
+ if params.Code >= 400 {
+ w.Header().Del("Cache-Control") //avoid sending cache headers for errors!
+ w.Header().Del("ETag")
+ }
+
+ acceptHeader := r.Header.Get("Accept")
+ // this cannot be in a switch form since an Accept header can be in the form of "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)
+ }
+ } else if strings.Contains(acceptHeader, "text/html") {
+ respondHTML(w, r, params)
+ } else {
+ respondPlaintext(w, r, params) //returns nice errors for curl
+ }
+}
+
+//return a HTML page
+func respondHTML(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
+ htmlCounter.Inc(1)
+ log.Debug("respondHTML", "ruid", GetRUID(r.Context()))
+ err := params.template.Execute(w, params)
+ if err != nil {
+ log.Error(err.Error())
+ }
+}
+
+//return JSON
+func respondJSON(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
+ jsonCounter.Inc(1)
+ log.Debug("respondJSON", "ruid", GetRUID(r.Context()))
+ w.Header().Set("Content-Type", "application/json")
+ return json.NewEncoder(w).Encode(params)
+}
+
+//return plaintext
+func respondPlaintext(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
+ plaintextCounter.Inc(1)
+ log.Debug("respondPlaintext", "ruid", GetRUID(r.Context()))
+ w.Header().Set("Content-Type", "text/plain")
+ strToWrite := "Code: " + fmt.Sprintf("%d", params.Code) + "\n"
+ strToWrite += "Message: " + string(params.Msg) + "\n"
+ strToWrite += "Timestamp: " + params.Timestamp + "\n"
+ _, err := w.Write([]byte(strToWrite))
+ return err
+}