http.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. if err == etcdserver.ErrTimeoutDueToLeaderFail {
  52. plog.Error(err)
  53. } else {
  54. plog.Errorf("got unexpected response error (%v)", err)
  55. }
  56. herr := httptypes.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
  57. herr.WriteTo(w)
  58. }
  59. }
  60. // allowMethod verifies that the given method is one of the allowed methods,
  61. // and if not, it writes an error to w. A boolean is returned indicating
  62. // whether or not the method is allowed.
  63. func allowMethod(w http.ResponseWriter, m string, ms ...string) bool {
  64. for _, meth := range ms {
  65. if m == meth {
  66. return true
  67. }
  68. }
  69. w.Header().Set("Allow", strings.Join(ms, ","))
  70. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  71. return false
  72. }
  73. func requestLogger(handler http.Handler) http.Handler {
  74. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  75. plog.Debugf("[%s] %s remote:%s", r.Method, r.RequestURI, r.RemoteAddr)
  76. handler.ServeHTTP(w, r)
  77. })
  78. }