aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/http/response.go
blob: 32c09b1f572973eaeaaeb8dc39d80fd1a2cb6a02 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
}