...

Source file src/go.etcd.io/etcd/server/v3/etcdserver/api/v2http/http.go

Documentation: go.etcd.io/etcd/server/v3/etcdserver/api/v2http

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v2http
    16  
    17  import (
    18  	"math"
    19  	"net/http"
    20  	"strings"
    21  	"time"
    22  
    23  	"go.etcd.io/etcd/server/v3/etcdserver/api/etcdhttp"
    24  	"go.etcd.io/etcd/server/v3/etcdserver/api/v2auth"
    25  	"go.etcd.io/etcd/server/v3/etcdserver/api/v2http/httptypes"
    26  
    27  	"go.uber.org/zap"
    28  )
    29  
    30  const (
    31  	// time to wait for a Watch request
    32  	defaultWatchTimeout = time.Duration(math.MaxInt64)
    33  )
    34  
    35  func writeError(lg *zap.Logger, w http.ResponseWriter, r *http.Request, err error) {
    36  	if err == nil {
    37  		return
    38  	}
    39  	if e, ok := err.(v2auth.Error); ok {
    40  		herr := httptypes.NewHTTPError(e.HTTPStatus(), e.Error())
    41  		if et := herr.WriteTo(w); et != nil {
    42  			if lg != nil {
    43  				lg.Debug(
    44  					"failed to write v2 HTTP error",
    45  					zap.String("remote-addr", r.RemoteAddr),
    46  					zap.String("v2auth-error", e.Error()),
    47  					zap.Error(et),
    48  				)
    49  			}
    50  		}
    51  		return
    52  	}
    53  	etcdhttp.WriteError(lg, w, r, err)
    54  }
    55  
    56  // allowMethod verifies that the given method is one of the allowed methods,
    57  // and if not, it writes an error to w.  A boolean is returned indicating
    58  // whether or not the method is allowed.
    59  func allowMethod(w http.ResponseWriter, m string, ms ...string) bool {
    60  	for _, meth := range ms {
    61  		if m == meth {
    62  			return true
    63  		}
    64  	}
    65  	w.Header().Set("Allow", strings.Join(ms, ","))
    66  	http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
    67  	return false
    68  }
    69  
    70  func requestLogger(lg *zap.Logger, handler http.Handler) http.Handler {
    71  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    72  		if lg != nil {
    73  			lg.Debug(
    74  				"handling HTTP request",
    75  				zap.String("method", r.Method),
    76  				zap.String("request-uri", r.RequestURI),
    77  				zap.String("remote-addr", r.RemoteAddr),
    78  			)
    79  		}
    80  		handler.ServeHTTP(w, r)
    81  	})
    82  }
    83  

View as plain text