github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/api/http/response.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:46</date> 10 //</624342669153275904> 11 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 // 25 // 26 // 27 28 package http 29 30 import ( 31 "encoding/json" 32 "fmt" 33 "html/template" 34 "net/http" 35 "strings" 36 "time" 37 38 "github.com/ethereum/go-ethereum/log" 39 "github.com/ethereum/go-ethereum/metrics" 40 "github.com/ethereum/go-ethereum/swarm/api" 41 ) 42 43 var ( 44 htmlCounter = metrics.NewRegisteredCounter("api.http.errorpage.html.count", nil) 45 jsonCounter = metrics.NewRegisteredCounter("api.http.errorpage.json.count", nil) 46 plaintextCounter = metrics.NewRegisteredCounter("api.http.errorpage.plaintext.count", nil) 47 ) 48 49 type ResponseParams struct { 50 Msg template.HTML 51 Code int 52 Timestamp string 53 template *template.Template 54 Details template.HTML 55 } 56 57 // 58 // 59 // 60 // 61 // 62 // 63 func ShowMultipleChoices(w http.ResponseWriter, r *http.Request, list api.ManifestList) { 64 log.Debug("ShowMultipleChoices", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context())) 65 msg := "" 66 if list.Entries == nil { 67 RespondError(w, r, "Could not resolve", http.StatusInternalServerError) 68 return 69 } 70 requestUri := strings.TrimPrefix(r.RequestURI, "/") 71 72 uri, err := api.Parse(requestUri) 73 if err != nil { 74 RespondError(w, r, "Bad Request", http.StatusBadRequest) 75 } 76 77 uri.Scheme = "bzz-list" 78 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()) 79 RespondTemplate(w, r, "error", msg, http.StatusMultipleChoices) 80 } 81 82 func RespondTemplate(w http.ResponseWriter, r *http.Request, templateName, msg string, code int) { 83 log.Debug("RespondTemplate", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context())) 84 respond(w, r, &ResponseParams{ 85 Code: code, 86 Msg: template.HTML(msg), 87 Timestamp: time.Now().Format(time.RFC1123), 88 template: TemplatesMap[templateName], 89 }) 90 } 91 92 func RespondError(w http.ResponseWriter, r *http.Request, msg string, code int) { 93 log.Debug("RespondError", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()), "code", code) 94 RespondTemplate(w, r, "error", msg, code) 95 } 96 97 func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) { 98 99 w.WriteHeader(params.Code) 100 101 if params.Code >= 400 { 102 w.Header().Del("Cache-Control") 103 w.Header().Del("ETag") 104 } 105 106 acceptHeader := r.Header.Get("Accept") 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 140 141 142 143 144 145