2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:41:22 +04:00
|
|
|
|
2014-08-28 00:37:22 +04:00
|
|
|
package etcdhttp
|
|
|
|
|
|
|
|
import (
|
2014-08-28 03:34:43 +04:00
|
|
|
"errors"
|
2015-01-02 21:56:01 +03:00
|
|
|
"math"
|
2014-08-28 00:37:22 +04:00
|
|
|
"net/http"
|
2014-08-28 04:53:36 +04:00
|
|
|
"strings"
|
2014-08-28 00:37:22 +04:00
|
|
|
"time"
|
|
|
|
|
2015-06-09 00:11:21 +03:00
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
|
2014-09-10 23:00:20 +04:00
|
|
|
etcdErr "github.com/coreos/etcd/error"
|
2015-08-12 23:38:43 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver"
|
2015-06-16 01:15:51 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver/auth"
|
2014-10-27 23:25:03 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
|
2015-12-30 03:48:40 +03:00
|
|
|
"github.com/coreos/etcd/pkg/logutil"
|
2014-08-28 00:37:22 +04:00
|
|
|
)
|
|
|
|
|
2014-09-09 03:34:29 +04:00
|
|
|
const (
|
2014-09-24 01:08:18 +04:00
|
|
|
// time to wait for a Watch request
|
2015-01-02 21:56:01 +03:00
|
|
|
defaultWatchTimeout = time.Duration(math.MaxInt64)
|
2014-09-12 03:13:45 +04:00
|
|
|
)
|
2014-09-11 02:22:50 +04:00
|
|
|
|
2015-06-09 00:11:21 +03:00
|
|
|
var (
|
|
|
|
plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdhttp")
|
2015-12-30 03:48:40 +03:00
|
|
|
mlog = logutil.NewMergeLogger(plog)
|
2015-06-09 00:11:21 +03:00
|
|
|
errClosed = errors.New("etcdhttp: client closed connection")
|
|
|
|
)
|
2014-08-28 03:34:43 +04:00
|
|
|
|
2014-09-11 02:46:13 +04:00
|
|
|
// writeError logs and writes the given Error to the ResponseWriter
|
2014-09-10 23:00:20 +04:00
|
|
|
// If Error is an etcdErr, it is rendered to the ResponseWriter
|
2014-09-11 02:46:13 +04:00
|
|
|
// Otherwise, it is assumed to be an InternalServerError
|
2015-09-25 18:06:26 +03:00
|
|
|
func writeError(w http.ResponseWriter, r *http.Request, err error) {
|
2014-09-10 23:00:20 +04:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-10-27 23:25:03 +03:00
|
|
|
switch e := err.(type) {
|
|
|
|
case *etcdErr.Error:
|
2014-10-28 01:32:36 +03:00
|
|
|
e.WriteTo(w)
|
2014-10-27 23:25:03 +03:00
|
|
|
case *httptypes.HTTPError:
|
2015-09-25 18:06:26 +03:00
|
|
|
if et := e.WriteTo(w); et != nil {
|
|
|
|
plog.Debugf("error writing HTTPError (%v) to %s", et, r.RemoteAddr)
|
|
|
|
}
|
2015-06-16 01:15:51 +03:00
|
|
|
case auth.Error:
|
2015-06-20 01:00:26 +03:00
|
|
|
herr := httptypes.NewHTTPError(e.HTTPStatus(), e.Error())
|
2015-09-25 18:06:26 +03:00
|
|
|
if et := herr.WriteTo(w); et != nil {
|
|
|
|
plog.Debugf("error writing HTTPError (%v) to %s", et, r.RemoteAddr)
|
|
|
|
}
|
2014-10-27 23:25:03 +03:00
|
|
|
default:
|
2015-08-26 01:14:41 +03:00
|
|
|
switch err {
|
2015-11-05 07:54:55 +03:00
|
|
|
case etcdserver.ErrTimeoutDueToLeaderFail, etcdserver.ErrTimeoutDueToConnectionLost, etcdserver.ErrNotEnoughStartedMembers:
|
2015-12-30 03:48:40 +03:00
|
|
|
mlog.MergeError(err)
|
2015-08-26 01:14:41 +03:00
|
|
|
default:
|
2015-12-30 03:48:40 +03:00
|
|
|
mlog.MergeErrorf("got unexpected response error (%v)", err)
|
2015-08-12 23:38:43 +03:00
|
|
|
}
|
2014-10-31 05:50:33 +03:00
|
|
|
herr := httptypes.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
|
2015-09-25 18:06:26 +03:00
|
|
|
if et := herr.WriteTo(w); et != nil {
|
|
|
|
plog.Debugf("error writing HTTPError (%v) to %s", et, r.RemoteAddr)
|
|
|
|
}
|
2014-08-28 01:47:11 +04:00
|
|
|
}
|
2014-09-10 23:00:20 +04:00
|
|
|
}
|
2014-08-28 01:47:11 +04:00
|
|
|
|
2014-09-13 02:16:29 +04:00
|
|
|
// allowMethod verifies that the given method is one of the allowed methods,
|
|
|
|
// and if not, it writes an error to w. A boolean is returned indicating
|
|
|
|
// whether or not the method is allowed.
|
|
|
|
func allowMethod(w http.ResponseWriter, m string, ms ...string) bool {
|
|
|
|
for _, meth := range ms {
|
|
|
|
if m == meth {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Header().Set("Allow", strings.Join(ms, ","))
|
2014-09-10 02:44:34 +04:00
|
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
2014-09-13 02:16:29 +04:00
|
|
|
return false
|
2014-09-10 02:44:34 +04:00
|
|
|
}
|
2015-06-09 20:40:37 +03:00
|
|
|
|
|
|
|
func requestLogger(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
plog.Debugf("[%s] %s remote:%s", r.Method, r.RequestURI, r.RemoteAddr)
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|