http.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 CoreOS, Inc.
  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 etcdhttp
  15. import (
  16. "errors"
  17. "math"
  18. "net/http"
  19. "strings"
  20. "time"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
  22. etcdErr "github.com/coreos/etcd/error"
  23. "github.com/coreos/etcd/etcdserver"
  24. "github.com/coreos/etcd/etcdserver/auth"
  25. "github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
  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", "etcdhttp")
  33. errClosed = errors.New("etcdhttp: client closed connection")
  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 an InternalServerError
  38. func writeError(w http.ResponseWriter, 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. e.WriteTo(w)
  47. case auth.Error:
  48. herr := httptypes.NewHTTPError(e.HTTPStatus(), e.Error())
  49. herr.WriteTo(w)
  50. default:
  51. switch err {
  52. case etcdserver.ErrTimeoutDueToLeaderFail, etcdserver.ErrTimeoutDueToConnectionLost:
  53. plog.Error(err)
  54. default:
  55. plog.Errorf("got unexpected response error (%v)", err)
  56. }
  57. herr := httptypes.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
  58. herr.WriteTo(w)
  59. }
  60. }
  61. // allowMethod verifies that the given method is one of the allowed methods,
  62. // and if not, it writes an error to w. A boolean is returned indicating
  63. // whether or not the method is allowed.
  64. func allowMethod(w http.ResponseWriter, m string, ms ...string) bool {
  65. for _, meth := range ms {
  66. if m == meth {
  67. return true
  68. }
  69. }
  70. w.Header().Set("Allow", strings.Join(ms, ","))
  71. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  72. return false
  73. }
  74. func requestLogger(handler http.Handler) http.Handler {
  75. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  76. plog.Debugf("[%s] %s remote:%s", r.Method, r.RequestURI, r.RemoteAddr)
  77. handler.ServeHTTP(w, r)
  78. })
  79. }