http.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. package v2http
  15. import (
  16. "math"
  17. "net/http"
  18. "strings"
  19. "time"
  20. etcdErr "github.com/coreos/etcd/error"
  21. "github.com/coreos/etcd/etcdserver"
  22. "github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
  23. "github.com/coreos/etcd/etcdserver/auth"
  24. "github.com/coreos/etcd/pkg/logutil"
  25. "github.com/coreos/pkg/capnslog"
  26. )
  27. const (
  28. // time to wait for a Watch request
  29. defaultWatchTimeout = time.Duration(math.MaxInt64)
  30. )
  31. var (
  32. plog = capnslog.NewPackageLogger("github.com/coreos/etcd/etcdserver/api", "v2http")
  33. mlog = logutil.NewMergeLogger(plog)
  34. )
  35. // writeError logs and writes the given Error to the ResponseWriter
  36. // If Error is an etcdErr, it is rendered to the ResponseWriter
  37. // Otherwise, it is assumed to be a StatusInternalServerError
  38. func writeError(w http.ResponseWriter, r *http.Request, err error) {
  39. if err == nil {
  40. return
  41. }
  42. switch e := err.(type) {
  43. case *etcdErr.Error:
  44. e.WriteTo(w)
  45. case *httptypes.HTTPError:
  46. if et := e.WriteTo(w); et != nil {
  47. plog.Debugf("error writing HTTPError (%v) to %s", et, r.RemoteAddr)
  48. }
  49. case auth.Error:
  50. herr := httptypes.NewHTTPError(e.HTTPStatus(), e.Error())
  51. if et := herr.WriteTo(w); et != nil {
  52. plog.Debugf("error writing HTTPError (%v) to %s", et, r.RemoteAddr)
  53. }
  54. default:
  55. switch err {
  56. case etcdserver.ErrTimeoutDueToLeaderFail, etcdserver.ErrTimeoutDueToConnectionLost, etcdserver.ErrNotEnoughStartedMembers:
  57. mlog.MergeError(err)
  58. default:
  59. mlog.MergeErrorf("got unexpected response error (%v)", err)
  60. }
  61. herr := httptypes.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
  62. if et := herr.WriteTo(w); et != nil {
  63. plog.Debugf("error writing HTTPError (%v) to %s", et, r.RemoteAddr)
  64. }
  65. }
  66. }
  67. // allowMethod verifies that the given method is one of the allowed methods,
  68. // and if not, it writes an error to w. A boolean is returned indicating
  69. // whether or not the method is allowed.
  70. func allowMethod(w http.ResponseWriter, m string, ms ...string) bool {
  71. for _, meth := range ms {
  72. if m == meth {
  73. return true
  74. }
  75. }
  76. w.Header().Set("Allow", strings.Join(ms, ","))
  77. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  78. return false
  79. }
  80. func requestLogger(handler http.Handler) http.Handler {
  81. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  82. plog.Debugf("[%s] %s remote:%s", r.Method, r.RequestURI, r.RemoteAddr)
  83. handler.ServeHTTP(w, r)
  84. })
  85. }