github.com/codingfuture/orig-energi3@v0.8.4/swarm/api/http/middleware.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package http
    19  
    20  import (
    21  	"fmt"
    22  	"net/http"
    23  	"runtime/debug"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/ethereum/go-ethereum/metrics"
    28  	"github.com/ethereum/go-ethereum/swarm/api"
    29  	"github.com/ethereum/go-ethereum/swarm/log"
    30  	"github.com/ethereum/go-ethereum/swarm/sctx"
    31  	"github.com/ethereum/go-ethereum/swarm/spancontext"
    32  	"github.com/pborman/uuid"
    33  )
    34  
    35  // Adapt chains h (main request handler) main handler to adapters (middleware handlers)
    36  // Please note that the order of execution for `adapters` is FIFO (adapters[0] will be executed first)
    37  func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
    38  	for i := range adapters {
    39  		adapter := adapters[len(adapters)-1-i]
    40  		h = adapter(h)
    41  	}
    42  	return h
    43  }
    44  
    45  type Adapter func(http.Handler) http.Handler
    46  
    47  func SetRequestID(h http.Handler) http.Handler {
    48  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    49  		r = r.WithContext(SetRUID(r.Context(), uuid.New()[:8]))
    50  		metrics.GetOrRegisterCounter(fmt.Sprintf("http.request.%s", r.Method), nil).Inc(1)
    51  		log.Info("created ruid for request", "ruid", GetRUID(r.Context()), "method", r.Method, "url", r.RequestURI)
    52  
    53  		h.ServeHTTP(w, r)
    54  	})
    55  }
    56  
    57  func SetRequestHost(h http.Handler) http.Handler {
    58  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    59  		r = r.WithContext(sctx.SetHost(r.Context(), r.Host))
    60  		log.Info("setting request host", "ruid", GetRUID(r.Context()), "host", sctx.GetHost(r.Context()))
    61  
    62  		h.ServeHTTP(w, r)
    63  	})
    64  }
    65  
    66  func ParseURI(h http.Handler) http.Handler {
    67  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    68  		uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))
    69  		if err != nil {
    70  			w.WriteHeader(http.StatusBadRequest)
    71  			respondError(w, r, fmt.Sprintf("invalid URI %q", r.URL.Path), http.StatusBadRequest)
    72  			return
    73  		}
    74  		if uri.Addr != "" && strings.HasPrefix(uri.Addr, "0x") {
    75  			uri.Addr = strings.TrimPrefix(uri.Addr, "0x")
    76  
    77  			msg := fmt.Sprintf(`The requested hash seems to be prefixed with '0x'. You will be redirected to the correct URL within 5 seconds.<br/>
    78  			Please click <a href='%[1]s'>here</a> if your browser does not redirect you within 5 seconds.<script>setTimeout("location.href='%[1]s';",5000);</script>`, "/"+uri.String())
    79  			w.WriteHeader(http.StatusNotFound)
    80  			w.Write([]byte(msg))
    81  			return
    82  		}
    83  
    84  		ctx := r.Context()
    85  		r = r.WithContext(SetURI(ctx, uri))
    86  		log.Debug("parsed request path", "ruid", GetRUID(r.Context()), "method", r.Method, "uri.Addr", uri.Addr, "uri.Path", uri.Path, "uri.Scheme", uri.Scheme)
    87  
    88  		h.ServeHTTP(w, r)
    89  	})
    90  }
    91  
    92  func InitLoggingResponseWriter(h http.Handler) http.Handler {
    93  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    94  		tn := time.Now()
    95  
    96  		writer := newLoggingResponseWriter(w)
    97  		h.ServeHTTP(writer, r)
    98  
    99  		ts := time.Since(tn)
   100  		log.Info("request served", "ruid", GetRUID(r.Context()), "code", writer.statusCode, "time", ts)
   101  		metrics.GetOrRegisterResettingTimer(fmt.Sprintf("http.request.%s.time", r.Method), nil).Update(ts)
   102  		metrics.GetOrRegisterResettingTimer(fmt.Sprintf("http.request.%s.%d.time", r.Method, writer.statusCode), nil).Update(ts)
   103  	})
   104  }
   105  
   106  func InstrumentOpenTracing(h http.Handler) http.Handler {
   107  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   108  		uri := GetURI(r.Context())
   109  		if uri == nil || r.Method == "" || (uri != nil && uri.Scheme == "") {
   110  			h.ServeHTTP(w, r) // soft fail
   111  			return
   112  		}
   113  		spanName := fmt.Sprintf("http.%s.%s", r.Method, uri.Scheme)
   114  		ctx, sp := spancontext.StartSpan(r.Context(), spanName)
   115  
   116  		defer sp.Finish()
   117  		h.ServeHTTP(w, r.WithContext(ctx))
   118  	})
   119  }
   120  
   121  func RecoverPanic(h http.Handler) http.Handler {
   122  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   123  		defer func() {
   124  			if err := recover(); err != nil {
   125  				log.Error("panic recovery!", "stack trace", string(debug.Stack()), "url", r.URL.String(), "headers", r.Header)
   126  			}
   127  		}()
   128  		h.ServeHTTP(w, r)
   129  	})
   130  }