github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/api/http/response.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package http
    26  
    27  import (
    28  	"encoding/json"
    29  	"fmt"
    30  	"html/template"
    31  	"net/http"
    32  	"strings"
    33  	"time"
    34  
    35  	"github.com/ethereum/go-ethereum/log"
    36  	"github.com/ethereum/go-ethereum/metrics"
    37  	"github.com/ethereum/go-ethereum/swarm/api"
    38  )
    39  
    40  var (
    41  	htmlCounter      = metrics.NewRegisteredCounter("api.http.errorpage.html.count", nil)
    42  	jsonCounter      = metrics.NewRegisteredCounter("api.http.errorpage.json.count", nil)
    43  	plaintextCounter = metrics.NewRegisteredCounter("api.http.errorpage.plaintext.count", nil)
    44  )
    45  
    46  type ResponseParams struct {
    47  	Msg       template.HTML
    48  	Code      int
    49  	Timestamp string
    50  	template  *template.Template
    51  	Details   template.HTML
    52  }
    53  
    54  //
    55  //
    56  //
    57  //
    58  //
    59  //
    60  func ShowMultipleChoices(w http.ResponseWriter, r *http.Request, list api.ManifestList) {
    61  	log.Debug("ShowMultipleChoices", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
    62  	msg := ""
    63  	if list.Entries == nil {
    64  		RespondError(w, r, "Could not resolve", http.StatusInternalServerError)
    65  		return
    66  	}
    67  	requestUri := strings.TrimPrefix(r.RequestURI, "/")
    68  
    69  	uri, err := api.Parse(requestUri)
    70  	if err != nil {
    71  		RespondError(w, r, "Bad Request", http.StatusBadRequest)
    72  	}
    73  
    74  	uri.Scheme = "bzz-list"
    75  	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())
    76  	RespondTemplate(w, r, "error", msg, http.StatusMultipleChoices)
    77  }
    78  
    79  func RespondTemplate(w http.ResponseWriter, r *http.Request, templateName, msg string, code int) {
    80  	log.Debug("RespondTemplate", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
    81  	respond(w, r, &ResponseParams{
    82  		Code:      code,
    83  		Msg:       template.HTML(msg),
    84  		Timestamp: time.Now().Format(time.RFC1123),
    85  		template:  TemplatesMap[templateName],
    86  	})
    87  }
    88  
    89  func RespondError(w http.ResponseWriter, r *http.Request, msg string, code int) {
    90  	log.Debug("RespondError", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()), "code", code)
    91  	RespondTemplate(w, r, "error", msg, code)
    92  }
    93  
    94  func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
    95  
    96  	w.WriteHeader(params.Code)
    97  
    98  	if params.Code >= 400 {
    99  		w.Header().Del("Cache-Control")
   100  		w.Header().Del("ETag")
   101  	}
   102  
   103  	acceptHeader := r.Header.Get("Accept")
   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   
   140   
   141